static void Main(string[] args) { Network net = new Network(); IntVariable[] A = new IntVariable[6]; for (int j=0; j< 6; j++) { IntDomain d = new IntDomain(1, 9); A[j] = new IntVariable(net); A[j].Domain = d; Trail trail = new Trail(); } ((A[4].Multiply(10).Add(A[5])).Multiply(A[0])).Add( ((A[1].Multiply(10).Add(A[2])).Multiply(A[3]))).Equals( (A[1].Multiply(10).Add(A[2])).Multiply(A[4].Multiply(10).Add(A[5]))); new NotEquals(net, A); Solver solver = new DefaultSolver(net); int i = 0; for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; Console.Out.WriteLine(); Console.Out.WriteLine(solution.GetIntValue(A[0]) + " " + solution.GetIntValue(A[3])); Console.Out.WriteLine("-- + -- = 1"); Console.Out.WriteLine(solution.GetIntValue(A[1])+""+solution.GetIntValue(A[2])+" "+ solution.GetIntValue(A[4]) + solution.GetIntValue(A[5])); Console.Out.WriteLine("========="); i++; } Console.Out.WriteLine("There are {0} solutions",i); solver.Stop(); Console.In.ReadLine(); //------------------------------------------------------------------------------------------ }
internal void queens(int n) { var c = 0; var net = new Network(); var q = new IntVariable[n]; var u = new IntVariable[n]; var d = new IntVariable[n]; for (var i = 0; i < n; ++i) { q[i] = new IntVariable(net, 1, n); u[i] = q[i].Add(i); d[i] = q[i].Subtract(i); } new NotEquals(net, q); new NotEquals(net, u); new NotEquals(net, d); Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; sol[c] = new int[8]; for (int i = 0; i < n; i++) { var s = solution.GetIntValue(q[i]); sol[c][i] = solution.GetIntValue(q[i]); } c++; } solver.Stop(); }
public static void Main(String[] args) { // Create a constraint network Network net = new Network(); // Declare variables IntVariable x = new IntVariable(net); IntVariable y = new IntVariable(net); // x >= 0 x.Ge(0); // y >= 0 y.Ge(0); // x + y == 7 x.Add(y).Equals(7); // 2x + 4y == 20 x.Multiply(2).Add(y.Multiply(4)).Equals(20); // Solve the problem Solver solver = new DefaultSolver(net); /*Solution solution = solver.findAll(Solution); int xv = solution.getIntValue(x); int yv = solution.getIntValue(y); Console.Out.WriteLine("x = " + xv + ", y = " + yv); */ for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; int xv = solution.GetIntValue(x); int yv = solution.GetIntValue(y); Console.Out.WriteLine("x8 = " + xv + ", y = " + yv); } solver.Stop(); //solver.findAll(new FirstStepHandler(x, y)); Console.In.ReadLine(); }
public static void Main(string[] args) { var net = new Network(); var x = new IntVariable(net, 0, 708); var y = new IntVariable(net, 0, 708); var z = new IntVariable(net, 0, 708); var t = new IntVariable(net, 0, 708); x.Add(y).Add(z).Add(t).Equals(711); x.Ge(y); y.Ge(z); z.Ge(t); x.Multiply(y).Multiply(z).Multiply(t).Equals(711000000); Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { var solution = solver.Solution; Console.Out.WriteLine(); Console.Out.WriteLine(" {0:F} + {1:F} + {2:F} + {3:F} = {4:F} ", solution.GetIntValue(x)/100.0, solution.GetIntValue(y)/100.0, solution.GetIntValue(z)/100.0, solution.GetIntValue(t)/100.0,7.11 ); } solver.Stop(); Console.ReadLine(); }
internal static void pp() { Network net = new Network(); // number of materials int m = 3; // limit of each material int[] limit = new int[] { 1650, 1400, 1800 }; // number of products int n = 2; // profit of each product int[] p = new int[] { 5, 4 }; // amount of materials required to make each product int[][] a = new int[][] { new int[] { 15, 10, 9 }, new int[] { 11, 14, 20 } }; // initialize variables for products IntVariable[] x = new IntVariable[n]; for (int j = 0; j < n; j++) { x[j] = new IntVariable(net); x[j].Ge(0); } // generate constraits of limiting materials for (int i = 0; i < m; i++) { IntVariable sum = new IntVariable(net, 0); for (int j = 0; j < n; j++) { sum = sum.Add(x[j].Multiply(a[j][i])); } sum.Le(limit[i]); } // total profit IntVariable profit = new IntVariable(net, 0); for (int j = 0; j < n; j++) { profit = profit.Add(x[j].Multiply(p[j])); } // maximize the total profit net.Objective = profit; // iteratively find a better solution until the optimal solution is found Solver solver = new DefaultSolver(net, Solver.Maximize | Solver.Better); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; Console.WriteLine(solver.GetCount()); Console.Out.WriteLine("Profit = " + solution.GetIntValue(profit)); for (int j = 0; j < n; j++) { Console.Out.WriteLine("x[" + j + "]=" + solution.GetIntValue(x[j])); } Console.Out.WriteLine(); } solver.Stop(); Console.ReadLine(); }
internal static void golomb(int m) { int n = (1 << (m - 1)) - 1; Network net = new Network(); IntVariable[] a = new IntVariable[m]; a[0] = new IntVariable(net, 0); for (int i = 1; i < m; i++) { a[i] = new IntVariable(net, 1, n); a[i - 1].Lt(a[i]); } IntVariable[] d = new IntVariable[m * (m - 1) / 2]; int k = 0; for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { d[k++] = a[j].Subtract(a[i]); } } //d[0].Lt((d[m - 1])); new NotEquals(net, d); net.Objective=a[m - 1]; Solver solver = new DefaultSolver(net, Solver.Minimize); Solution solution; for (solver.Start(); solver.WaitNext(); solver.Resume()) { solution = solver.Solution; //estSolution = solver.BestSolution; Console.Out.Write("0"); for (int i = 1; i < m; i++) { Console.Out.Write("," + solution.GetIntValue(a[i])); } Console.Out.WriteLine(); } solver.Stop(); solution = solver.FindBest(); Console.Out.WriteLine("==========================="); Console.Out.Write("0"); for (int i = 1; i < m; i++) { Console.Out.Write("," + solution.GetIntValue(a[i])); } Console.Out.WriteLine(); }
public static void Main(String[] args) { Network net = new Network(); int n = 3; int sum = n * (n * n + 1) / 2; IntVariable[][] v = new IntVariable[n][]; for (int i = 0; i < n; i++) { v[i] = new IntVariable[n]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) v[i][j] = new IntVariable(net, 1, n * n); IntVariable[] u = new IntVariable[] { v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2], v[2][0], v[2][1], v[2][2] }; new NotEquals(net, u); for (int i = 0; i < n; i++) v[i][0].Add(v[i][1]).Add(v[i][2]).Equals(sum); for (int j = 0; j < n; j++) v[0][j].Add(v[1][j]).Add(v[2][j]).Equals(sum); v[0][0].Add(v[1][1]).Add(v[2][2]).Equals(sum); v[0][2].Add(v[1][1]).Add(v[2][0]).Equals(sum); Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Out.Write(solution.GetIntValue(v[i][j]) + " "); Console.Out.WriteLine(); } Console.Out.WriteLine(); } solver.Stop(); Console.ReadLine(); }
internal static void solve() { setProblem(); Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; printSolution(solution); } solver.Stop(); }
public static void solve() { int FAMILIES = 2; int CHILDREN = 6; int maxAge = 9; Network net = new Network(); IntVariable[][] isBoy = new IntVariable[FAMILIES][]; for (int i = 0; i < FAMILIES; i++) { isBoy[i] = new IntVariable[CHILDREN]; } IntVariable[][] age = new IntVariable[FAMILIES][]; for (int i2 = 0; i2 < FAMILIES; i2++) { age[i2] = new IntVariable[CHILDREN]; } IntVariable[][] boyAge = new IntVariable[FAMILIES][]; for (int i3 = 0; i3 < FAMILIES; i3++) { boyAge[i3] = new IntVariable[CHILDREN]; } IntVariable[][] girlAge = new IntVariable[FAMILIES][]; for (int i4 = 0; i4 < FAMILIES; i4++) { girlAge[i4] = new IntVariable[CHILDREN]; } for (int family = 0; family < FAMILIES; family++) { for (int child = 0; child < CHILDREN; child++) { isBoy[family][child] = new IntVariable(net, 0, 1); age[family][child] = new IntVariable(net, 0, maxAge); if (child > 0) age[family][child].Gt(age[family][child - 1]); boyAge[family][child] = age[family][child].Multiply(isBoy[family][child]); girlAge[family][child] = age[family][child].Subtract(boyAge[family][child]); } } isBoy[0][0].Equals(0); isBoy[1][0].Equals(0); age[1][0].Equals(0); for (int family = 0; family < FAMILIES; family++) { sum(isBoy[family]).Equals(3); sum(boyAge[family]).Equals(sum(girlAge[family])); sum2(boyAge[family]).Equals(sum2(girlAge[family])); } sum(age).Equals(60); Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; for (int family = 0; family < FAMILIES; family++) { Console.Out.Write("Family " + family + ": "); for (int child = 0; child < CHILDREN; child++) { int _isBoy = solution.GetIntValue(isBoy[family][child]); int _age = solution.GetIntValue(age[family][child]); Console.Out.Write(_isBoy != 0 ? "Boy " : "Girl "); Console.Out.Write(_age + " "); } Console.Out.WriteLine(); } } Console.ReadLine(); }
internal static void runExample(Network net, int opt) { Console.Out.WriteLine("# Problem"); //UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'" Console.Out.WriteLine(net); Console.Out.WriteLine("# Solutions"); Solver solver = new DefaultSolver(net, opt); for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; Console.Out.WriteLine(solution); } solver.Stop(); long count = solver.GetCount(); long time = solver.GetElapsedTime(); Console.Out.WriteLine("Found " + count + " solutions in " + time + " milli seconds"); Console.Out.WriteLine(); }
public static void magic(int n) { Network net = new Network(); IntVariable[][] square = new IntVariable[n][]; for (int i = 0; i < n; i++) { square[i] = new IntVariable[n]; } // All squares have different numbers 1 .. n*n IntVariable[] v = new IntVariable[n * n]; int k = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { square[i][j] = new IntVariable(net, 1, n * n); v[k++] = square[i][j]; } } new NotEquals(net, v); // Sum of each row is n*(n*n+1)/2 IntVariable s; int sum = n * (n * n + 1) / 2; for (int i = 0; i < n; i++) { s = square[i][0]; for (int j = 1; j < n; j++) s = s.Add(square[i][j]); s.Equals(sum); } // Sum of each column is n*(n*n+1)/2 for (int j = 0; j < n; j++) { s = square[0][j]; for (int i = 1; i < n; i++) s = s.Add(square[i][j]); s.Equals(sum); } // Sum of down-diagonal is n*(n*n+1)/2 s = square[0][0]; for (int i = 1; i < n; i++) s = s.Add(square[i][i]); s.Equals(sum); // Sum of up-diagonal is n*(n*n+1)/2 s = square[0][n - 1]; for (int i = 1; i < n; i++) s = s.Add(square[i][n - i - 1]); s.Equals(sum); // Left-upper corner is minimum square[0][0].Lt(square[0][n - 1]); square[0][0].Lt(square[n - 1][0]); square[0][0].Lt(square[n - 1][n - 1]); // Upper-right is less than lower-left square[0][n - 1].Lt(square[n - 1][0]); Console.Out.WriteLine("Start"); long time0 = (DateTime.Now.Ticks - 621355968000000000) / 10000; int count = 0; bool output = true; Solver solver = new DefaultSolver(net); for (solver.Start(); solver.WaitNext(); solver.Resume()) { if (output) { Solution solution = solver.Solution; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Out.Write(solution.GetIntValue(square[i][j]) + " "); } Console.Out.WriteLine(); } Console.Out.WriteLine(); } count++; } solver.Stop(); int time = (int)(((DateTime.Now.Ticks - 621355968000000000) / 10000 - time0) / 1000); Console.Out.WriteLine(count + " solutions found in " + time + " seconds"); }