Beispiel #1
0
        public void SonnetCbcTest2()
        {
            Console.WriteLine("SonnetCbcTest2 - Cbc set CbcStrategyDefault");

            Model  model  = Model.New("MIP-124725.mps"); // added file to project, "Copy Always"
            Solver solver = new Solver(model, typeof(OsiCbcSolverInterface));

            model.ObjectiveSense = ObjectiveSense.Minimise;

            OsiCbcSolverInterface osisolver = solver.OsiSolver as OsiCbcSolverInterface;

            Assert.IsTrue(osisolver != null);

            CbcModel cbcModel = osisolver.getModelPtr();

            cbcModel.setStrategy(new CbcStrategyDefault(1, 5, 5));
            //cbcModel.strategy().setupCutGenerators(cbcModel);

            solver.AutoResetMIPSolve = true;
            solver.Minimise();

            string message = "Used cut generators: " + string.Join(", ", cbcModel.cutGenerators().Select(generator => generator.generator().GetType().Name));

            Console.WriteLine(message);

            Assert.IsTrue(Utils.EqualsDouble(model.Objective.Value, 124725));

            solver.Solve(true);
            Assert.IsTrue(Utils.EqualsDouble(model.Objective.Value, 104713.12807881772));
        }
Beispiel #2
0
        public static CbcAction SonnetCbcTest5EventHandler(CbcModel model, CbcEvent cbcevent)
        {
            // This is an example EventHandler for CbcModel, invoked at events such as a solution was found, etc.
            // This depends on the heuristics applied during the solving, so the solutions found can be different.
            if (cbcevent == CbcEvent.solution)
            {
                SonnetCbcTest5numSolutions++;

                Assert.IsTrue(model.getBestPossibleObjValue() >= 10482.79 && model.getBestPossibleObjValue() <= 11801.18, $"Dual bound is ${model.getBestPossibleObjValue()} but should be between 10482.79 and 11801.18 (opt)");
                Assert.IsTrue(model.getObjValue() >= 11801.18 && model.getObjValue() <= 17602.0, $"Best minimization solution of mas74 until now is ${model.getObjValue()} but should be between 11801.18 (opt) and 14372.88");
                // If either of these asserts fail, it might be that the underlying CbcSolver improved with better cuts, etc.
                // If this assert fails, then the best solution found so far (by heuristics or on the tree) is worse than expected.

                if (SonnetCbcTest5numSolutions == 1)
                {
                    return(CbcAction.noAction);
                }
                else if (SonnetCbcTest5numSolutions == 5)
                {
                    // Note: the action to Stop is not always respected by Cbc, for example, during heuristics.
                    return(CbcAction.stop);
                }
            }

            return(CbcAction.noAction);
        }
Beispiel #3
0
        public void SonnetCbcTest3()
        {
            Console.WriteLine("SonnetCbcTest3 - Test log message handler vs CbcMain");

            Model  model  = Model.New("MIP-124725.mps"); // added file to project, "Copy Always";
            Solver solver = new Solver(model, typeof(OsiCbcSolverInterface));

            SonnetLog.Default.LogLevel = 2;

            OsiCbcSolverInterface osisolver = solver.OsiSolver as OsiCbcSolverInterface;

            //SonnetLog.Default.PassToSolver(osisolver);
            Assert.IsTrue(osisolver != null);

            CbcModel cbcModel = osisolver.getModelPtr();

            solver.Minimise();
            Ensure.IsTrue(SonnetLog.Default.LogLevel == 2, "Somehow the SonnetLog LogLevel changed!");

            Assert.IsTrue(Utils.EqualsDouble(model.Objective.Value, 124725));
        }
Beispiel #4
0
        public void SonnetCbcTest1()
        {
            Console.WriteLine("SonnetCbcTest1 - Cbc test set CbcStrategyNull and addCutGenerator");

            Model  model  = Model.New("MIP-124725.mps"); // added file to project, "Copy Always";
            Solver solver = new Solver(model, typeof(OsiCbcSolverInterface));

            OsiCbcSolverInterface osisolver = solver.OsiSolver as OsiCbcSolverInterface;

            Assert.IsTrue(osisolver != null);

            CbcModel cbcModel = osisolver.getModelPtr();

            cbcModel.setStrategy(new CbcStrategyNull());
            cbcModel.addCutGenerator(new CglProbing());
            Assert.IsTrue(cbcModel.numberCutGenerators() == 1);
            //cbcModel.cutGenerators();

            solver.Minimise();

            Assert.IsTrue(Utils.EqualsDouble(model.Objective.Value, 124725));
            //Assert.IsTrue(Utils.EqualsDouble(model.Objective.Value, 104713.12807881772));
        }