1package com.gams.examples.tsp;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
34 public static void main(String[] args) {
42 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"Tsp");
43 workingDirectory.mkdir();
51 int cutsPerRound = 10;
64 List<String> n =
new ArrayList<String>();
70 List<String> subTour =
null;
76 System.out.print(
",");
77 cMax = curCut + cutsPerRound;
85 opt.
defines(
"cmax", Integer.toString(cMax - 1));
96 opt.
defines(
"tspdata",
"\"" + datafile.getAbsolutePath() +
"\"");
112 System.out.print(
".");
120 Map<String, String> graph =
new HashMap<String, String>();
122 List <String> notVisited =
new ArrayList<String>(n);
125 String[] keys = { i, j };
133 while (notVisited.size() != 0) {
134 String ii = notVisited.get(0);
135 subTour =
new ArrayList<String>();
137 while (graph.get(ii) != notVisited.get(0))
143 final List<String> source = subTour;
144 IPredicate<String> doesNotContain =
new IPredicate<String>() {
145 public boolean apply(String element) {
146 return !source.contains(element);
149 notVisited = (List<String>) filter(subTour, doesNotContain);
152 for(String i : subTour) {
153 for(String j : subTour) {
154 String[] keys = {
"c"+curCut, i, j };
160 String key =
"c"+curCut;
168 while(subTour.size() < n.size());
170 System.out.println();
172 System.out.println(
"sub_tour: ");
173 for(String i : subTour)
174 System.out.print(i +
" -> ");
175 System.out.println( subTour.get(0) );
179 interface IPredicate<T> {
boolean apply(T type); }
180 static <T> Collection<T> filter(Collection<T> target, IPredicate<T> predicate) {
181 Collection<T> result =
new ArrayList<T>();
182 for (T element : target) {
183 if (predicate.apply(element)) {
190 static String model =
191 "$Title Traveling Salesman Problem \n" +
194 "The sub_tour elimination constraints are generated by a Python \n" +
195 "script. The MIP is solved over and over, but GAMS have to \n" +
196 "generate the model only after n cuts have been added. \n" +
200 "$if not set tspdata $abort 'tspdata not set' \n" +
203 " i(ii) subset of cities \n" +
204 "alias (ii,jj),(i,j,k); \n" +
206 "parameter c(ii,jj) distance matrix; \n" +
208 "$gdxin %tspdata% \n" +
211 "$if not set nrCities $set nrCities 20 \n" +
212 "i(ii)$(ord(ii) < %nrCities%) = yes; \n" +
214 "variables x(ii,jj) decision variables - leg of trip \n" +
215 " z objective variable; \n" +
216 "binary variable x; x.fx(ii,ii) = 0; \n" +
218 "equations objective total cost \n" +
219 " rowsum(ii) leave each city only once \n" +
220 " colsum(jj) arrive at each city only once; \n" +
222 "* the assignment problem is a relaxation of the TSP \n" +
223 "objective.. z =e= sum((i,j), c(i,j)*x(i,j)); \n" +
224 "rowsum(i).. sum(j, x(i,j)) =e= 1; \n" +
225 "colsum(j).. sum(i, x(i,j)) =e= 1; \n" +
227 "$if not set cmax $set cmax 2 \n" +
228 "set cut /c0*c%cmax%/; \n" +
230 " acut(cut,ii,jj) cut constraint matrix \n" +
231 " rhscut(cut) cut constraint rhs; \n" +
233 "equation sscut(cut) sub_tour elimination cut; \n" +
234 "sscut(cut).. sum((i,j), Acut(cut,i,j)*x(i,j)) =l= RHScut(cut); \n" +
236 "set cc(cut) previous cuts; cc(cut) = no; \n" +
237 "$if set cutdata execute_load '%cutdata%', cc, Acut, RHScut; \n" +
239 "Acut(cut,i,j)$(not cc(cut)) = eps; \n" +
240 "RHScut(cut)$(not cc(cut)) = card(ii); \n" +
242 "model assign /all/; \n" +
244 "option optcr=0; \n";
GAMSModelInstance addModelInstance()
GAMSParameter getParameter(String identifier)
GAMSSet addSet(String identifier, int dimension)
GAMSParameter addParameter(String identifier, int dimension)
GAMSVariable getVariable(String identifier)
GAMSSet getSet(String identifier)
static final String FILE_SEPARATOR
void instantiate(String modelDefinition, GAMSModifier ... modifiers)
void defines(String defStr, String asStr)
void setValue(double value)
T findRecord(String ... keys)
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
GAMSCheckpoint addCheckpoint()
GAMSDatabase addDatabase()
This example demonstrates how to use a GAMSModelInstance to implement the subtour elimination algorit...
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).