public void VerifyModelOnlySerializesToFile()
        {
            const string path      = "AnotherSimpleTest.dat";
            const string modelName = "TestModelLoader";

            {
                using (var s = new Solver(modelName))
                {
                    var x = s.MakeIntVar(0, 10, "x");
                    var y = s.MakeIntVar(0, 10, "y");

                    var c = x + y == 5;

                    //c.Cst.SetName("equation");

                    s.Add(c);

                    Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                    var model = s.ExportModel();

                    using (var stream = File.Open(path, Create))
                    {
                        model.WriteTo(stream);
                    }
                }
            }

            {
                var model = new CpModel();

                using (var s = new Solver(modelName))
                {
                    using (var stream = File.Open(path, Open))
                    {
                        model.MergeFrom(stream);
                    }

                    s.LoadModel(model);

                    var loader = s.ModelLoader();
                    Assert.That(loader, Is.Not.Null);

                    var x = loader.IntegerExpressionByName("x").Var();
                    var y = loader.IntegerExpressionByName("y").Var();

                    // Just check that all things are equivalent.
                    var c = x + y == 5;

                    Assert.That(c.Cst.ToString(), Is.Not.EqualTo("TrueConstraint()"));

                    // Constraints should reflect what was actually there.
                    Assert.That(s.ConstraintCount(), Is.EqualTo(1));
                }
            }
        }
        public void VerifyThatInMemoryExportToProtoAfterSolutionFoundWorks()
        {
            CpModel model;

            // Model name must be the same because loading does not re-set it.
            const string modelName = "TestModelLoader";

            string modelText;

            using (var s = new Solver(modelName))
            {
                var x = s.MakeIntVar(0, 10, "x");
                var y = s.MakeIntVar(0, 10, "y");

                s.Add(x + y == 5);
                // Verify that adding one Constraint appears in the Count.
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                var db = s.MakePhase(x, y, ChooseFirstUnbound, AssignMinValue);

                {
                    // TODO: TBD: consider adding a disposable search wrapper to hide that detail a bit...
                    // Ending the new search after next solution block is CRITICAL.
                    s.NewSearch(db);
                    while (s.NextSolution())
                    {
                        Console.WriteLine($"Found next solution: {x.ToString()} + {y.ToString()} == 5");
                        break;
                    }
                    s.EndSearch();
                }

                // Capture the ExportedModel textual (JSON) representation.
                model = s.ExportModel();
                Assert.That(model, Is.Not.Null);
                modelText = VerifyJsonText(model.ToString());
            }

            using (var s = new Solver(modelName))
            {
                Assert.That(s.LoadModel(model), Is.True);

                // Straight after load the Constraints should report the same number.
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                // The textual representation must be the same.
                var actual     = s.ExportModel();
                var actualText = VerifyJsonText(actual.ToString());
                Assert.That(actualText, Is.EqualTo(modelText));
            }
        }
        public void VerifyThatInMemoryExportToProtoWorks()
        {
            CpModel model;

            // Model name must be the same because loading does not re-set it.
            const string modelName = "TestModelLoader";

            string modelText;

            using (var s = new Solver(modelName))
            {
                var x = s.MakeIntVar(0, 10, "x");
                var y = s.MakeIntVar(0, 10, "y");

                s.Add(x + y == 5);
                // Verify that adding one Constraint appears in the Count.
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                // Capture the ExportedModel textual (JSON) representation.
                model = s.ExportModel();
                Assert.That(model, Is.Not.Null);
                modelText = VerifyJsonText(model.ToString());
            }

            using (var s = new Solver(modelName))
            {
                Assert.That(s.LoadModel(model), Is.True);

                // Straight after load the Constraints should report the same number.
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                // The textual representation must be the same.
                var actual     = s.ExportModel();
                var actualText = VerifyJsonText(actual.ToString());
                Assert.That(actualText, Is.EqualTo(modelText));
            }
        }
        public void SimpleTestWithSearchMonitorsAndDecisionBuilder()
        {
            CpModel model;

            string modelText;

            const string modelName    = "TestModelLoader";
            const string equationText = "((x(0..10) + y(0..10)) == 5)";

            using (var s = new Solver(modelName))
            {
                var x = s.MakeIntVar(0, 10, "x");
                var y = s.MakeIntVar(0, 10, "y");
                var c = x + y == 5;
                Assert.That(c.Cst.ToString(), Is.EqualTo(equationText));
                s.Add(c);
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));
                var collector = s.MakeAllSolutionCollector();
                var db        = s.MakePhase(x, y, ChooseFirstUnbound, AssignMinValue);
                Console.WriteLine("First search...");
                s.NewSearch(db, collector);
                while (s.NextSolution())
                {
                    Console.WriteLine($"{x.ToString()} + {y.ToString()} == 5");
                    break;
                }
                s.EndSearch();
                using (var vect = new SearchMonitorVector())
                {
                    vect.Add(collector);
                    model     = s.ExportModelWithSearchMonitorsAndDecisionBuilder(vect, db);
                    modelText = model.ToString();
                }
            }

            using (var s = new Solver(modelName))
            {
                // TODO: TBD: load but without any monitors and/or DB ...
                s.LoadModel(model);

                var loader = s.ModelLoader();
                // Do a quick sanity check that we at least have the proper constraint loaded.
                Assert.That(s.ConstraintCount(), Is.EqualTo(1));

                var x = loader.IntegerExpressionByName("x").Var();
                var y = loader.IntegerExpressionByName("y").Var();

                {
                    var c = x + y == 5;
                    // These should PASS as well...
                    Assert.That(c.Cst.ToString(), Is.Not.EqualTo("TrueConstraint()"));
                    Assert.That(c.Cst.ToString(), Is.EqualTo(equationText));
                }

                {
                    /* I dare say that THIS should PASS as well, but due to the fact that IntVar and
                     * derivatives are treated as IntExpr, it is FAILING. */
                    var actual = s.ExportModel();
                    Assert.That(actual.ToString(), Is.EqualTo(modelText));
                }

                var db = s.MakePhase(x, y, ChooseFirstUnbound, AssignMinValue);
                Console.WriteLine("Second search...");
                s.NewSearch(db);
                while (s.NextSolution())
                {
                    Console.WriteLine($"{x.ToString()} + {y.ToString()} == 5");
                }
                s.EndSearch();
            }
        }