Loading...
Searching...
No Matches
Alias.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Text.RegularExpressions;
6using System.Diagnostics;
7using System.IO;
8using GAMS;
9using System.Runtime.InteropServices;
10
11namespace Alias
12{
25 class Alias
26 {
27 static int Main(string[] args)
28 {
29 try
30 {
32 if (Environment.GetCommandLineArgs().Length > 1)
33 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
34 else
35 ws = new GAMSWorkspace();
36
37 // Create initial data containing a GAMS Alias
38 // The OO API does not know about Aliases and will retrieve it as a set
39 GAMSJob j1 = ws.AddJobFromString(GetDataText());
40 j1.Run();
41 CheckAliasLogic("j1.OutDB ", j1.OutDB);
42 j1.OutDB.Export("outdb.gdx");
43 MyAssert(SameGdxDump(ws, "outdb.gdx", gdxdump1()), "Unexpected result of gdxdump outdb.gdx");
44
45 // Copy constructor should preserve aliases and other
46 GAMSDatabase db = ws.AddDatabase(j1.OutDB);
47 CheckAliasLogic("db ", db);
48 db.Export("db.gdx");
49 MyAssert(SameGdxDump(ws, "db.gdx", gdxdump1()), "Unexpected result of gdxdump db.gdx");
50
51 GAMSDatabase db2 = ws.AddDatabase();
52 GAMSSet ii = db2.AddSet(db.GetSet("ii").Name, db.GetSet("ii").Text, "*");
53 db.GetSet("ii").CopySymbol(ii);
54
55 GAMSParameter aaOrig = db.GetParameter("aa");
56 GAMSParameter aa = db2.AddParameter(db.GetParameter("aa").Name, db.GetParameter("aa").Text, ii);
57 aaOrig.CopySymbol(aa);
58 db2.Export("db2.gdx");
59 MyAssert(SameGdxDump(ws, "db2.gdx", gdxdump2()), "Unexpected result of gdxdump db2.gdx");
60
61 // If the domain is an Alias, Domains should return the aliased Set,
62 // but DomainsAsStrings should return the name of the Alias
63 MyAssert(((GAMSSet)aaOrig.Domains[0]).Name == "i", "The domain set should be the original set");
64 MyAssert(aaOrig.DomainsAsStrings[0] == "ii", "The domain as string should be the alias name");
65 }
66 catch (Exception e)
67 {
68 Console.WriteLine("###");
69 Console.WriteLine("### Exception caught:" + e.Message);
70 Console.WriteLine("###");
71 return 1;
72 }
73 return 0;
74 }
75
76 static void MyAssert(bool test, string msg)
77 {
78 if (!test)
79 throw new System.Exception(msg);
80 }
81
82 static void CheckAliasLogic(string prefix, GAMSDatabase aliasDB)
83 {
84 // Check number of symbols
85 MyAssert(aliasDB.NrSymbols == 5, prefix + "aliasDB should have NrSymbols=5: i,j,ij,a,aa.");
86
87 int cntSymbols = 0;
88 foreach (GAMSSymbol sym in aliasDB) cntSymbols++;
89 MyAssert(cntSymbols == 5, prefix + "foreach (GAMSSymbol sym in aliasDB) should result in 5 symbols: i,j,ij,a,aa.");
90
91 // See if we can retrieve alias sets
92 MyAssert(aliasDB.GetSet("ii").Name == "i", prefix + "We should get set i when asking for alias ii.");
93 MyAssert(aliasDB.GetSet("jj").Name == "j", prefix + "We should get set j when asking for alias jj.");
94 MyAssert(aliasDB.GetSet("iijj").Name == "ij", prefix + "We should get set ij when asking for alias iijj.");
95
96 // Check domain logic
97 MyAssert(aliasDB.CheckDomains() == true, prefix + "Check domains should be true");
98 MyAssert(aliasDB.GetParameter("aa").Domains[0] is GAMSSet, prefix + "domain[0] of aa should be set");
99 MyAssert(((GAMSSet)aliasDB.GetParameter("aa").Domains[0]).Name == "i", prefix + "domain[0] of aa should point to i");
100
101 aliasDB.GetSet("ii").DeleteRecord("i1");
102 MyAssert(aliasDB.CheckDomains() == false, prefix + "Check domains should be false after removal of i1");
103 aliasDB.GetSet("ii").AddRecord("i1");
104 MyAssert(aliasDB.CheckDomains() == true, prefix + "Check domains should be true after adding i1 again");
105 }
106
107 static bool SameGdxDump(GAMSWorkspace ws, string gdxfile, string expectedResult)
108 {
109 string result = string.Empty;
110 ProcessStartInfo start = new ProcessStartInfo();
111
112 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
113 {
114 start.FileName = ws.SystemDirectory + Path.DirectorySeparatorChar + "gdxdump.exe";
115 }
116 else
117 {
118 start.FileName = ws.SystemDirectory + Path.DirectorySeparatorChar + "gdxdump";
119 }
120
121 start.Arguments = ws.WorkingDirectory + Path.DirectorySeparatorChar + gdxfile;
122 start.UseShellExecute = false;
123 start.RedirectStandardOutput = true;
124 using (Process process = Process.Start(start))
125 {
126 //
127 // Read in all the text from the process with the StreamReader.
128 //
129 using (StreamReader reader = process.StandardOutput)
130 {
131 result = Regex.Replace(reader.ReadToEnd(), @"\s", ""); // remove white space
132 }
133 }
134 return String.Equals(result, expectedResult, StringComparison.OrdinalIgnoreCase);
135 }
136
137 static String GetDataText()
138 {
139 String data = @"
140set i / i1*i3 /
141 j / j1*j3 /
142 ij / #i:#j /
143alias (i,ii), (j,jj), (ij,iijj);
144parameter
145 a(i) / #i 1 /, aa(ii) / #ii 2 /;
146";
147 return data;
148 }
149
150 static String gdxdump1()
151 {
152 String data = @"
153$onempty
154
155Set i(*) /
156'i1',
157'i2',
158'i3' /;
159
160Set j(*) /
161'j1',
162'j2',
163'j3' /;
164
165Set ij(*,*) /
166'i1'.'j1',
167'i2'.'j2',
168'i3'.'j3' /;
169
170Alias (ii, i);
171
172Alias (jj, j);
173
174Alias (iijj, ij);
175
176Parameter a(i) /
177'i1' 1,
178'i2' 1,
179'i3' 1 /;
180
181Parameter aa(ii) /
182'i1' 2,
183'i2' 2,
184'i3' 2 /;
185
186$offempty
187";
188 return Regex.Replace(data, @"\s", ""); // remove white space
189 }
190
191 static String gdxdump2()
192 {
193 String data = @"
194$onempty
195
196Set i(*) /
197'i1',
198'i2',
199'i3' /;
200
201Parameter aa(i) /
202'i1' 2,
203'i2' 2,
204'i3' 2 /;
205
206$offempty
207";
208 return Regex.Replace(data, @"\s", ""); // remove white space
209 }
210
211 }
212}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSet GetSet(string setIdentifier)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Export(string filePath=null)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
Definition Alias.cs:12