Esempio n. 1
0
        public void VerifySolverModelSerialization()
        {
            const string solverName1 = "Test1";
            const string solverName2 = "Test2";

            const string path =
                @"G:\Source\Kingdom Software\Kingdom.ConstraintSolvers\Working\src\Kingdom.Constraints.Sample.Fixturing.Tests\ExportedSolverModel.dat";

            using (var sc = GetSampleContext(solverName1))
            {
                sc.NewSearch();

                var vars = sc.Expressions.OfType <IntVar>().ToArray();

                var x = vars.SingleOrDefault(v => v.Name() == "x");
                var y = vars.SingleOrDefault(v => v.Name() == "y");

                Assert.That(x, Is.Not.Null);
                Assert.That(y, Is.Not.Null);

                var collector = sc.Monitors.OfType <SolutionCollector>().SingleOrDefault();

                Assert.That(collector, Is.Not.Null);

                var count = 0;

                while (sc.NextSolution())
                {
                    // ReSharper disable once PossibleNullReferenceException
                    Assert.That(collector.SolutionCount(), Is.GreaterThan(0));

                    Assert.That(collector.SolutionCount(), Is.EqualTo(++count));

                    // ReSharper disable once PossibleNullReferenceException
                    Console.WriteLine("Solution: {0} + {1} = 5", x.Value(),
                                      // ReSharper disable once PossibleNullReferenceException
                                      y.Value()
                                      );

                    break;
                }

                sc.Save(path);
            }

            //const string constraintName = "equation";

            using (var sc = new SolverContext(solverName2))
            {
                sc
                .Monitor(s => s.MakeAllSolutionCollector())
                ;

                var collector = sc.Monitors.OfType <SolutionCollector>().SingleOrDefault();

                sc.Load(path);

                //Assert.That(sc.Solver.ConstraintCount(), Is.EqualTo(1));

                var x = GetIntVarFromIntExpr(sc.Solver.ModelLoader(), "x");
                var y = GetIntVarFromIntExpr(sc.Solver.ModelLoader(), "y");

                var strats = new { Var = Solver.INT_VAR_SIMPLE, Val = Solver.INT_VALUE_SIMPLE };

                sc
                .Expression(delegate { return(x); })
                .Expression(delegate { return(y); })
                .Constrain(s =>
                {
                    var c = x + y == 5;

                    //c.Cst.SetName(constraintName);
                    //Assert.That(c.Cst.Name(), Is.EqualTo(constraintName));
                    Assert.That(c.Cst.Name(), Is.EqualTo(string.Empty));

                    Assert.That(c, Is.TypeOf <WrappedConstraint>());
                    Assert.That(c.Cst.ToString(), Is.Not.EqualTo("TrueConstraint()"));
                    Assert.That(c.Cst.ToString(), Is.EqualTo("((x(0..10) + y(0..10)) == 5)"));

                    return(c);
                })
                .CreateDecisionBuilder(
                    e => new IntVarVector(e.OfType <IntVar>().ToList())
                    , (s, vect) => s.MakePhase(vect, strats.Var, strats.Val)
                    )
                ;

                // TODO: TBD: new search? or ability to pick up where we left off?
                sc.NewSearch();

                // TODO: TBD: currently, "re-loading" does not include state, i.e. previously covered ground, so to speak.
                var count = 0;

                while (sc.NextSolution())
                {
                    // ReSharper disable once PossibleNullReferenceException
                    Assert.That(collector.SolutionCount(), Is.GreaterThan(0));

                    Assert.That(collector.SolutionCount(), Is.EqualTo(++count));

                    // ReSharper disable once PossibleNullReferenceException
                    Console.WriteLine("Solution: {0} + {1} = 5", x.Value(),
                                      // ReSharper disable once PossibleNullReferenceException
                                      y.Value()
                                      );

                    break;
                }


                // TODO: TBD: we seem to have the Constraints loaded. What about the variables?
            }
        }