//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!"); } }
/// <summary> /// Adds the constraint to the model. /// </summary> /// <param name="expression">The constraint to add to the model.</param> /// <param name="name">A unique name of the constraint.</param> public void AddConstr(LinearExpression expression, string name) { switch (Type) { case SolverType.CPLEX: CplexModel.Add(expression.Expression); break; case SolverType.Gurobi: GurobiModel.AddConstr(expression.Expression, name); break; default: throw new ArgumentException("Unknown solver type: " + Type); } }
/// <summary> /// Sets the objective function. /// </summary> /// <param name="expression">The objective function.</param> /// <param name="sense">The sense (optimization direction) of the objective function.</param> public void SetObjective(LinearExpression expression, OptimizationSense sense) { switch (Type) { case SolverType.CPLEX: CplexModel.AddObjective((sense == OptimizationSense.Minimize) ? ObjectiveSense.Minimize : ObjectiveSense.Maximize, expression.Expression); break; case SolverType.Gurobi: GurobiModel.SetObjective(expression.Expression, (sense == OptimizationSense.Minimize) ? GRB.MINIMIZE : GRB.MAXIMIZE); break; default: throw new ArgumentException("Unknown solver type: " + Type); } }