internal static IntVariable adjacentSum(int i, int j) { IntVariable s = new IntVariable(net, 0); if (i - 1 >= 0 && v[i - 1][j] != null) s = s.Add(v[i - 1][j]); if (i + 1 < m && v[i + 1][j] != null) s = s.Add(v[i + 1][j]); if (j - 1 >= 0 && v[i][j - 1] != null) s = s.Add(v[i][j - 1]); if (j + 1 < n && v[i][j + 1] != null) s = s.Add(v[i][j + 1]); return s; }
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(); }
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(); }
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 minimizeExample() { var net = new Network(); var x = new IntVariable(net, 1, 10, "x"); var y = new IntVariable(net, 1, 10, "y"); // x + y >= 10 x.Add(y).Ge(10); // z = max(x, y) var z = x.Max(y); z.Name ="z"; // minimize z net.Objective = z; runExample(net, Solver.Minimize | Solver.Better); }