private static IntVariable sum2(IntVariable[] v) { IntVariable sum = null; for (int i = 0; i < v.Length; i++) { IntVariable x = v[i].Multiply(v[i]); sum = (sum == null) ? x : sum.Add(x); } return(sum); }
internal static void setProblem() { net = new Network(); // Set constraint variables v = new IntVariable[m][]; for (int i = 0; i < m; i++) { v[i] = new IntVariable[n]; } _vsum = new IntVariable[m][]; for (int i2 = 0; i2 < m; i2++) { _vsum[i2] = new IntVariable[n]; } _hsum = new IntVariable[m][]; for (int i3 = 0; i3 < m; i3++) { _hsum[i3] = new IntVariable[n]; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { v[i][j] = null; if (puzzle[i][j].Equals("-")) { v[i][j] = new IntVariable(net, 0, 1); } _vsum[i][j] = null; _hsum[i][j] = null; } } // Set constraints for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (puzzle[i][j].Equals("-")) { IntVariable vsum_ = vsum(i, j); IntVariable hsum_ = hsum(i, j); vsum_.Add(hsum_).Subtract(v[i][j]).Ge(1); } else if (puzzle[i][j].Equals("^\\d$")) { int sum = Int32.Parse(puzzle[i][j]); IntVariable asum = adjacentSum(i, j); asum.Equals(sum); } } } }
private static IntVariable sum(IntVariable[][] v) { IntVariable sum = null; for (int i = 0; i < v.Length; i++) { for (int j = 0; j < v[i].Length; j++) { sum = (sum == null) ? v[i][j] : sum.Add(v[i][j]); } } return(sum); }
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); }
internal static void minimizeExample() { Network net = new Network(); IntVariable x = new IntVariable(net, 1, 10, "x"); IntVariable y = new IntVariable(net, 1, 10, "y"); // x + y >= 10 x.Add(y).Ge(10); // z = max(x, y) IntVariable z = x.Max(y); z.Name = "z"; // minimize z net.Objective = z; runExample(net, Solver.Minimize | Solver.Better); }
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(); }