Example #1
0
        /// <summary>
        /// Creates a new variable.
        /// </summary>
        /// <param name="solver">The model / solver instance this variable belongs to.</param>
        /// <param name="variableType">The type of the variable.</param>
        /// <param name="lb">The lower bound of the variable.</param>
        /// <param name="ub">The upper bound of the variable.</param>
        /// <param name="name">The name of the variable.</param>
        public Variable(LinearModel solver, VariableType variableType, double lb, double ub, string name)
        {
            // Set active solver reference
            Solver = solver;
            Solver.RegisterVariable(this);
            // Set parameters
            _variableType = variableType;
            // Instantiate the variable
            switch (solver.Type)
            {
            case SolverType.CPLEX:
                Expression =
                    (_variableType == VariableType.Continuous) ? solver.CplexModel.NumVar(lb, ub, NumVarType.Float) :
                    (_variableType == VariableType.Binary) ? solver.CplexModel.BoolVar(name) :
                    solver.CplexModel.IntVar((int)lb, (int)ub, name);
                break;

            case SolverType.Gurobi:
                Expression =
                    solver.GurobiModel.AddVar(lb, ub, 0, (variableType == VariableType.Continuous) ? GRB.CONTINUOUS : (variableType == VariableType.Binary) ? GRB.BINARY : GRB.INTEGER, name);
                break;

            default: throw new ArgumentException("Unknown solver type: " + solver.Type);
            }
        }
Example #2
0
        public static LinearExpression Sum(IEnumerable <LinearExpression> expressions)
        {
            LinearModel solver = expressions.First().Solver;

            switch (solver.Type)
            {
            case SolverType.CPLEX: { return(new LinearExpression()
                    {
                        Solver = solver, Expression = solver.CplexModel.Sum(expressions.Select(e => e.Expression as INumExpr).ToArray())
                    }); }

            case SolverType.Gurobi:
            {
                GRBLinExpr expr = new GRBLinExpr();
                foreach (var exp in expressions)
                {
                    expr += exp.Expression;
                }
                return(new LinearExpression()
                    {
                        Solver = solver, Expression = expr
                    });
            }

            default: throw new ArgumentException("Unknown solver type: " + solver.Type);
            }
        }
Example #3
0
        //public class CplexStatusCallback : CpxIncumbentCallbackFunction, IStatusCallback
        //{
        //    /// <summary>
        //    /// Creates a new callback.
        //    /// </summary>
        //    /// <param name="solver">The active solver.</param>
        //    public CplexStatusCallback(SolverWrapper solver) { _solver = solver; }
        //    /// <summary>
        //    /// The active solver.
        //    /// </summary>
        //    private SolverWrapper _solver;
        //    /// <summary>
        //    /// Logs messages of the solver.
        //    /// </summary>
        //    public Action<string> Logger { get; set; }
        //    /// <summary>
        //    /// Notifies about new incumbents.
        //    /// </summary>
        //    public Action NewIncumbent { get; set; }
        //    /// <summary>
        //    /// Logs new incumbent values.
        //    /// </summary>
        //    public Action<double> LogIncumbent { get; set; }

        //    public int CallIt(IntPtr xenv, IntPtr cbdata, int wherefrom, object cbhandle, double objval, double[] x, ref int isfeas_p, ref int useraction)
        //    {

        //        return 0;
        //    }
        //}

        #endregion

        public static void Test(SolverType type)
        {
            Console.WriteLine("Is64BitProcess: " + Environment.Is64BitProcess);
            LinearModel wrapper = new LinearModel(type, (string s) => { Console.Write(s); });

            Console.WriteLine("Setting up model and optimizing it with " + wrapper.Type);
            string        x = "x"; string y = "y"; string z = "z";
            List <string> variableNames = new List <string>()
            {
                x, y, z
            };
            VariableCollection <string> variables = new VariableCollection <string>(wrapper, VariableType.Binary, 0, 1, (string s) => { return(s); });

            wrapper.SetObjective(0.5 * variables[x] + variables[y] + 4 * variables[z], OptimizationSense.Maximize);
            wrapper.AddConstr(LinearExpression.Sum(variableNames.Select(v => 2 * variables[v])) <= 4, "Summation");
            wrapper.AddConstr(variables[x] + variables[y] + variables[z] <= 2.0, "limit");
            wrapper.AddConstr(variables[x] == variables[z], "equal");
            wrapper.AddConstr(variables[x] * 2 == 2, "times");
            wrapper.Update();
            wrapper.Optimize();

            Console.WriteLine("Solution:");
            if (wrapper.HasSolution())
            {
                Console.WriteLine("Obj: " + wrapper.GetObjectiveValue());
                foreach (var variableName in variableNames)
                {
                    Console.WriteLine(variableName + ": " + variables[variableName].GetValue());
                }
            }
            else
            {
                Console.WriteLine("No solution!");
            }
        }
Example #4
0
 /// <summary>
 /// Creates a new collection of variables.
 /// </summary>
 /// <param name="solver">The solver the variable collection belongs to.</param>
 /// <param name="variableType">The type of the variables in the collection.</param>
 /// <param name="lowerBound">The default lower bound when adding a variable on-the-fly.</param>
 /// <param name="upperBound">The default upper bound when adding a variable on-the-fly.</param>
 public VarCollection(LinearModel solver, VariableType variableType, double lowerBound, double upperBound)
 {
     // Set parameters
     _variableType = variableType;
     LB            = lowerBound;
     UB            = upperBound;
     // Reference active solver
     _solver = solver;
     // Instantiate the collection
     _variables = new Dictionary <object[], Variable>(new ObjectArrayEqualityComparer <object>());
 }
Example #5
0
        public static LinearExpression Sum(IEnumerable <Variable> variables)
        {
            LinearModel solver = variables.First().Solver;

            switch (solver.Type)
            {
            case SolverType.CPLEX: { return(new LinearExpression()
                    {
                        Solver = solver, Expression = solver.CplexModel.Sum(variables.Select(e => e.Expression as INumExpr).ToArray())
                    }); }

            case SolverType.Gurobi:
            {
                GRBLinExpr expr = new GRBLinExpr();
                expr.AddTerms(Enumerable.Repeat(1.0, variables.Count()).ToArray(), variables.Select(e => e.Expression as GRBVar).ToArray());
                return(new LinearExpression()
                    {
                        Solver = solver, Expression = expr
                    });
            }

            default: throw new ArgumentException("Unknown solver type: " + solver.Type);
            }
        }
Example #6
0
 /// <summary>
 /// Creates a new collection of variables.
 /// </summary>
 /// <param name="solver">The solver the variable collection belongs to.</param>
 /// <param name="variableType">The type of the variables in the collection.</param>
 /// <param name="lowerBound">The default lower bound when adding a variable on-the-fly.</param>
 /// <param name="upperBound">The default upper bound when adding a variable on-the-fly.</param>
 public VariableCollection(LinearModel solver, VariableType variableType, double lowerBound, double upperBound) : base(solver, variableType, lowerBound, upperBound)
 {
 }
Example #7
0
 /// <summary>
 /// Creates a new callback.
 /// </summary>
 /// <param name="solver">The active solver.</param>
 public CplexStatusCallback(LinearModel solver) : base()
 {
     _solver = solver;
 }
Example #8
0
 /// <summary>
 /// Creates a new callback.
 /// </summary>
 /// <param name="solver">The active solver.</param>
 public GurobiStatusCallback(LinearModel solver)
 {
     _solver = solver;
 }