2using System.Collections.Generic;
7using System.Reflection;
23 static void Main(
string[] args)
26 if (Environment.GetCommandLineArgs().Length > 1)
27 ws =
new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
31 int cutsPerRound = 10;
43 List<string> n =
new List<String>();
49 List<string> subTour =
null;
56 cMax = curCut + cutsPerRound;
63 opt.
Defines.Add(
"nrcities",
"20");
64 opt.
Defines.Add(
"cmax", (cMax - 1).ToString());
87 Dictionary<string, string> graph =
new Dictionary<string, string>();
88 List<string> notVisited =
new List<string>(n);
89 foreach (
string i
in n)
90 foreach (
string j
in n)
95 while (notVisited.Count != 0)
97 string ii = notVisited[0];
98 subTour =
new List<string>();
100 while (graph[ii] != notVisited[0])
105 notVisited = notVisited.Where(x => !subTour.Contains(x)).ToList<
string>();
108 foreach (
string i
in subTour)
109 foreach (
string j
in subTour)
121 while (subTour.Count < n.Count);
124 Console.WriteLine(
"sub_tour: ");
125 foreach (
string i
in subTour)
126 Console.Write(i +
" -> ");
127 Console.WriteLine(subTour[0]);
131 static String GetModelText()
134$Title Traveling Salesman Problem Instance with Python
138The sub_tour elimination constraints are generated by a Python
139script. The MIP is solved over and over, but GAMS have to
140generate the model only after n cuts have been added.
144$if not set tspdata $abort 'tspdata not set'
147 i(ii) subset of cities
148alias (ii,jj),(i,j,k);
150parameter c(ii,jj) distance matrix;
155$if not set nrCities $set nrCities 20
156i(ii)$(ord(ii) < %nrCities%) = yes;
158variables x(ii,jj) decision variables - leg of trip
159 z objective variable;
160binary variable x; x.fx(ii,ii) = 0;
162equations objective total cost
163 rowsum(ii) leave each city only once
164 colsum(jj) arrive at each city only once;
166* the assignment problem is a relaxation of the TSP
167objective.. z =e= sum((i,j), c(i,j)*x(i,j));
168rowsum(i).. sum(j, x(i,j)) =e= 1;
169colsum(j).. sum(i, x(i,j)) =e= 1;
171$if not set cmax $set cmax 2
174 acut(cut,ii,jj) cut constraint matrix
175 rhscut(cut) cut constraint rhs;
177equation sscut(cut) sub_tour elimination cut;
178sscut(cut).. sum((i,j), Acut(cut,i,j)*x(i,j)) =l= RHScut(cut);
180set cc(cut) previous cuts; cc(cut) = no;
181$if set cutdata execute_load '%cutdata%', cc, Acut, RHScut;
183Acut(cut,i,j)$(not cc(cut)) = eps;
184RHScut(cut)$(not cc(cut)) = card(ii);
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Export(string filePath=null)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void Solve(SymbolUpdateType updateType=SymbolUpdateType.BaseCase, TextWriter output=null, GAMSModelInstanceOpt miOpt=null)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
Dictionary< string, string > Defines
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
new GAMSVariableRecord FindRecord(params string[] keys)
new GAMSVariableRecord FirstRecord()
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)