Example #1
0
        static void Main(string[] args)
        {
            var @params = new MPSolverParameters();

            @params.SetDoubleParam(MPSolverParameters.DoubleParam.RELATIVE_MIP_GAP, 0);
            @params.SetDoubleParam(MPSolverParameters.DoubleParam.PRIMAL_TOLERANCE, 0);
            @params.SetDoubleParam(MPSolverParameters.DoubleParam.DUAL_TOLERANCE, 0);

            var solver = new Solver("BOP_Knapsack", (Solver.OptimizationProblemType) 12);

            BuildModel(solver);

            var result = solver.Solve(@params);

            Console.WriteLine("Using BOP:");
            Console.WriteLine("result: " + result);
            Console.WriteLine("objective: " + solver.Objective().Value());
            Console.WriteLine("bound: " + solver.Objective().BestBound());

            solver = new Solver("CBC_Knapsack", (Solver.OptimizationProblemType) 5);
            BuildModel(solver);

            result = solver.Solve(@params);
            Console.WriteLine("Using CBC:");
            Console.WriteLine("result: " + result);
            Console.WriteLine("objective: " + solver.Objective().Value());
            Console.WriteLine("bound: " + solver.Objective().BestBound());
        }
        private ResultState SolveInternal()
        {
            PrintDebugRessourcesBefore("SolveInternal");

            var param = new MPSolverParameters();

            param.SetDoubleParam(MPSolverParameters.RELATIVE_MIP_GAP, MIP_GAP);
            if (timelimit != 0)
            {
                solver.SetTimeLimit(timelimit);
            }
#if DEBUG
            solver.EnableOutput();
#else
            solver.SuppressOutput();
#endif
            resultState = FromGoogleResultState(solver.Solve(param));

            PrintDebugRessourcesAfter();
#if DEBUG
            PrintDebugOutput();
#endif

            return(resultState);
        }
        // TODO: TBD: taking LinearSolver assembly(ies) out of the build configuration for the time being until a usage/migration path can be established...
        /// <summary>
        /// Builds the <see cref="MPSolverParameters"/> instance given a set of
        /// <see cref="MpParamSpec"/> pairs.
        /// </summary>
        /// <param name="specs"></param>
        /// <returns></returns>
        protected virtual MPSolverParameters BuildParameters(params MpParamSpec[] specs)
        {
            var parameters = new MPSolverParameters();

            foreach (var(spec, value) in specs)
            {
                parameters.SetDoubleParam(spec.ForSolver(), value);
            }

            ClrCreatedObjects.Add(parameters);
            return(parameters);
        }
        private MPSolverParameters BuildParameters(IEnumerable <double> values)
        {
            var parameters = new MPSolverParameters();

            // ReSharper disable PossibleMultipleEnumeration
            for (var i = 0; i < values.Count(); i++)
            {
                parameters.SetDoubleParam(i, values.ElementAt(i));
                // ReSharper enable PossibleMultipleEnumeration
            }
            ClrCreatedObjects.Add(parameters);
            return(parameters);
        }