public void NegativeSquareVar() { CpModel model = new CpModel(); IntVar boolvar = model.NewBoolVar("boolvar"); IntVar x = model.NewIntVar(0, 10, "x"); IntVar delta = model.NewIntVar(-5, 5, "delta"); IntVar squaredDelta = model.NewIntVar(0, 25, "squaredDelta"); model.Add(x == 4).OnlyEnforceIf(boolvar); model.Add(x == 0).OnlyEnforceIf(boolvar.Not()); model.Add(delta == x - 5); long[,] tuples = { { -5, 25 }, { -4, 16 }, { -3, 9 }, { -2, 4 }, { -1, 1 }, { 0, 0 }, { 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } }; model.AddAllowedAssignments(new IntVar[] { delta, squaredDelta }, tuples); model.Minimize(squaredDelta); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); CpSolverResponse response = solver.Response; Assert.Equal(1, solver.Value(boolvar)); Assert.Equal(4, solver.Value(x)); Assert.Equal(-1, solver.Value(delta)); Assert.Equal(1, solver.Value(squaredDelta)); Assert.Equal(new long[] { 1, 4, -1, 1 }, response.Solution); Assert.Equal(1.0, response.ObjectiveValue, 6); }
public void SolveFromString() { string model_str = @" { ""variables"": [ { ""name"": ""C"", ""domain"": [ ""1"", ""9"" ] }, { ""name"": ""P"", ""domain"": [ ""0"", ""9"" ] }, { ""name"": ""I"", ""domain"": [ ""1"", ""9"" ] }, { ""name"": ""S"", ""domain"": [ ""0"", ""9"" ] }, { ""name"": ""F"", ""domain"": [ ""1"", ""9"" ] }, { ""name"": ""U"", ""domain"": [ ""0"", ""9"" ] }, { ""name"": ""N"", ""domain"": [ ""0"", ""9"" ] }, { ""name"": ""T"", ""domain"": [ ""1"", ""9"" ] }, { ""name"": ""R"", ""domain"": [ ""0"", ""9"" ] }, { ""name"": ""E"", ""domain"": [ ""0"", ""9"" ] } ], ""constraints"": [ { ""allDiff"": { ""vars"": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] } }, { ""linear"": { ""vars"": [ 6, 5, 9, 4, 3, 7, 8, 2, 0, 1 ], ""coeffs"": [ ""1"", ""0"", ""-1"", ""100"", ""1"", ""-1000"", ""-100"", ""10"", ""10"", ""1"" ], ""domain"": [ ""0"", ""0"" ] } } ] }"; CpModelProto model = Google.Protobuf.JsonParser.Default.Parse <CpModelProto>(model_str); SolveWrapper solve_wrapper = new SolveWrapper(); CpSolverResponse response = solve_wrapper.Solve(model); Console.WriteLine(response); }
public void SimpleLinearModel() { CpModel model = new CpModel(); IntVar v1 = model.NewIntVar(-10, 10, "v1"); IntVar v2 = model.NewIntVar(-10, 10, "v2"); IntVar v3 = model.NewIntVar(-100000, 100000, "v3"); model.AddLinearConstraint(v1 + v2, -1000000, 100000); model.AddLinearConstraint(v1 + 2 * v2 - v3, 0, 100000); model.Maximize(v3); Assert.Equal(v1.Domain.FlattenedIntervals(), new long[] { -10, 10 }); //Console.WriteLine("model = " + model.Model.ToString()); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); Assert.Equal(CpSolverStatus.Optimal, status); CpSolverResponse response = solver.Response; Assert.Equal(30, response.ObjectiveValue); Assert.Equal(new long[] { 10, 10, 30 }, response.Solution); //Console.WriteLine("response = " + reponse.ToString()); }
public void NegativeIntVar() { CpModel model = new CpModel(); IntVar boolvar = model.NewBoolVar("boolvar"); IntVar x = model.NewIntVar(0, 10, "x"); IntVar delta = model.NewIntVar(-5, 5, "delta"); IntVar squaredDelta = model.NewIntVar(0, 25, "squaredDelta"); model.Add(x == boolvar * 4); model.Add(delta == x - 5); model.AddProdEquality(squaredDelta, new IntVar[] { delta, delta }); model.Minimize(squaredDelta); // Console.WriteLine("model = " + model.Model.ToString()); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); CpSolverResponse response = solver.Response; Console.WriteLine("response = " + response.ToString()); Assert.Equal(CpSolverStatus.Optimal, status); Assert.Equal(1, solver.Value(boolvar)); Assert.Equal(4, solver.Value(x)); Assert.Equal(-1, solver.Value(delta)); Assert.Equal(1, solver.Value(squaredDelta)); Assert.Equal(new long[] { 1, 4, -1, 1 }, response.Solution); Assert.Equal(1.0, response.ObjectiveValue, 5); }
static void TestSimpleLinearModel2() { CpModelProto model = new CpModelProto(); model.Variables.Add(NewIntegerVariable(-10, 10)); model.Variables.Add(NewIntegerVariable(-10, 10)); model.Constraints.Add(NewLinear2(0, 1, 1, 1, -1000000, 100000)); model.Objective = NewMaximize2(0, 1, 1, -2); CpSolverResponse response = SatHelper.Solve(model); Console.WriteLine("model = " + model.ToString()); Console.WriteLine("response = " + response.ToString()); }
public void SimpleLinearModelProto2() { CpModelProto model = new CpModelProto(); model.Variables.Add(NewIntegerVariable(-10, 10)); model.Variables.Add(NewIntegerVariable(-10, 10)); model.Constraints.Add(NewLinear2(0, 1 , 1, 1, -1000000, 100000)); model.Objective = NewMaximize2(0, 1, 1, -2); //Console.WriteLine("model = " + model.ToString()); CpSolverResponse response = SatHelper.Solve(model); Assert.Equal(CpSolverStatus.Optimal, response.Status); Assert.Equal(30, response.ObjectiveValue); Assert.Equal(new long[] {10, -10}, response.Solution); //Console.WriteLine("response = " + response.ToString()); }
public void SimpleLinearModel2() { CpModel model = new CpModel(); IntVar v1 = model.NewIntVar(-10, 10, "v1"); IntVar v2 = model.NewIntVar(-10, 10, "v2"); model.AddLinearConstraint(new[] {v1, v2}, new[] {1, 1}, -1000000, 100000); model.Maximize(v1 - 2 * v2); //Console.WriteLine("model = " + model.Model.ToString()); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); Assert.Equal(CpSolverStatus.Optimal, status); CpSolverResponse response = solver.Response; Assert.Equal(30, response.ObjectiveValue); Assert.Equal(new long[] {10, -10}, response.Solution); //Console.WriteLine("response = " + reponse.ToString()); }
public void Modulo() { CpModel model = new CpModel(); IntVar v1 = model.NewIntVar(1, 10, "v1"); IntVar v2 = model.NewIntVar(1, 10, "v2"); model.AddModuloEquality(3, v1, v2); //Console.WriteLine(model.Model); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); Assert.Equal(CpSolverStatus.Feasible, status); CpSolverResponse response = solver.Response; Assert.Equal(3, solver.Value(v1)); Assert.Equal(4, solver.Value(v2)); Assert.Equal(new long[] {3, 4, 3}, response.Solution); Assert.Equal(0, response.ObjectiveValue); //Console.WriteLine("response = " + reponse.ToString()); }
public void SimpleLinearModel3() { CpModel model = new CpModel(); IntVar v1 = model.NewIntVar(-10, 10, "v1"); IntVar v2 = model.NewIntVar(-10, 10, "v2"); model.Add(-100000 <= v1 + 2 * v2 <= 100000); model.Minimize(v1 - 2 * v2); //Console.WriteLine("model = " + model.Model.ToString()); CpSolver solver = new CpSolver(); CpSolverStatus status = solver.Solve(model); Assert.Equal(CpSolverStatus.Optimal, status); CpSolverResponse response = solver.Response; Assert.Equal(-10, solver.Value(v1)); Assert.Equal(10, solver.Value(v2)); Assert.Equal(new long[] {-10, 10}, response.Solution); Assert.Equal(-30, solver.Value(v1 - 2 * v2)); Assert.Equal(-30, response.ObjectiveValue); //Console.WriteLine("response = " + reponse.ToString()); }