public override bool SetObjFn(object solver, double[] row) { GRBModel grbSolver = solver as GRBModel; if (grbSolver == null) { return(false); } GRBLinExpr obj = new GRBLinExpr(); obj.AddTerms(row, grbSolver.GetVars()); grbSolver.SetObjective(obj, GRB.MINIMIZE); return(true); }
public override bool AddConstraint(object solver, double[] row, SolverAdapterConstraintTypes constr_type, double rh) { GRBModel grbSolver = solver as GRBModel; if (grbSolver == null) { return(false); } GRBLinExpr expr = 0.0; ind++; expr.AddTerms(row, grbSolver.GetVars()); grbSolver.AddConstr(expr, (char)GetAdaptedConstraintType(constr_type), rh, "R" + ind); return(true); }
private void AddOnlyOnePositionPerGroupConstraint(GRBModel model, GRBVar[,,] seated) { var coeff = Utils.GenerateArrayOfOnes(Cinema.Width * Cinema.Height); for (int g = 0; g < Cinema.TotalNumberOfGroups; g++) { var result = new GRBVar[Cinema.Width * Cinema.Height]; var resultCount = 0; for (int x = 0; x < Cinema.Width; x++) { for (int y = 0; y < Cinema.Height; y++) { result[resultCount] = seated[x, y, g]; resultCount++; } } var expr = new GRBLinExpr(); expr.AddTerms(coeff, result); model.AddConstr(expr, GRB.LESS_EQUAL, 1, "Only One Position Per Group Constraint"); } }
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); } }