/** * Sets the direction of the objective function. * * @param d <code>true</code> if the objective function is to be maximized. **/ public void setMaximize(boolean d) { try { model.set(GRB.IntAttr.ModelSense, d ? -1 : 1); } catch (GRBException e) { handleException(e); } }
/** * Sets the direction of the objective function. * * @param d <code>true</code> if the objective function is to be maximized. **/ public void setMaximize(boolean d) { try { model.set(GRB.IntAttr.ModelSense, d ? -1 : 1); } catch (GRBException e) { handleException(e); } }
@Override public int getIntegerValue(int index) { if (!isSolved) return 0; try { double x = ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); return (int) Math.round(x); } catch (GRBException e) { handleException(e); } return 0; }
@Override public int getIntegerValue(int index) { if (!isSolved) return 0; try { double x = ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); return (int) Math.round(x); } catch (GRBException e) { handleException(e); } return 0; }
@Override public double getRealValue(int index) { if (!isSolved) return 0.0; try { return ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); } catch (GRBException e) { handleException(e); } return 0.0; }
@Override public double getRealValue(int index) { if (!isSolved) return 0.0; try { return ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); } catch (GRBException e) { handleException(e); } return 0.0; }
/** * When the problem has been solved, use this method to retrieve the value of any Boolean * inference variable. The result of this method is undefined when the problem has not yet been * solved. * * @param index The index of the variable whose value is requested. * @return The value of the variable. **/ public boolean getBooleanValue(int index) { if (!isSolved) return false; try { double x = ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); return x > 0.5; } catch (GRBException e) { handleException(e); } return false; }
/** * When the problem has been solved, use this method to retrieve the value of any Boolean * inference variable. The result of this method is undefined when the problem has not yet been * solved. * * @param index The index of the variable whose value is requested. * @return The value of the variable. **/ public boolean getBooleanValue(int index) { if (!isSolved) return false; try { double x = ((GRBVar) variables.get(index)).get(GRB.DoubleAttr.X); return x > 0.5; } catch (GRBException e) { handleException(e); } return false; }
public int addIntegerVariable(double c) { int id = variables.size(); try { variables.add(model.addVar(-GRB.INFINITY, GRB.INFINITY, c, GRB.INTEGER, "x_" + id)); } catch (GRBException e) { handleException(e); } // TODO: delete the line below once we get Gurobi 4.0 objectiveCoefficients.add(c); needsUpdate = true; return id; }
public int addRealVariable(double c) { int id = variables.size(); try { variables.add(model.addVar(-GRB.INFINITY, GRB.INFINITY, c, GRB.CONTINUOUS, "x_" + id)); } catch (GRBException e) { handleException(e); } // TODO: delete the line below once we get Gurobi 4.0 objectiveCoefficients.add(c); needsUpdate = true; return id; }
public int addIntegerVariable(double c) { int id = variables.size(); try { variables.add(model.addVar(-GRB.INFINITY, GRB.INFINITY, c, GRB.INTEGER, "x_" + id)); } catch (GRBException e) { handleException(e); } // TODO: delete the line below once we get Gurobi 4.0 objectiveCoefficients.add(c); needsUpdate = true; return id; }
/** * Creates a Gurobi linear expression object representing the dot product of the variables with * the specified indexes and the specified coefficients. * * @param ids The indexes of the variables. * @param c The corresponding coefficients. * @return A Gurobi linear expression representing the dot product. **/ protected GRBLinExpr makeLinearExpression(int[] ids, double[] c) { try { GRBLinExpr expr = new GRBLinExpr(); expr.addTerms(c, idsToVariables(ids)); return expr; } catch (GRBException e) { handleException(e); } return null; }
/** * When the problem has been solved, use this method to retrieve the value of the objective * function at the solution. The result of this method is undefined when the problem has not yet * been solved. If the problem had no feasible solutions, negative (positive, respectively) * infinity will be returned if maximizing (minimizing). * * @return The value of the objective function at the solution. **/ public double objectiveValue() { try { if (isSolved) return model.get(GRB.DoubleAttr.ObjVal); int status = model.get(GRB.IntAttr.Status); if (status == GRB.INFEASIBLE || status == GRB.INF_OR_UNBD || status == GRB.UNBOUNDED) return model.get(GRB.IntAttr.ModelSense) == -1 ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; } catch (GRBException e) { handleException(e); } return 0; }
/** * When the problem has been solved, use this method to retrieve the value of the objective * function at the solution. The result of this method is undefined when the problem has not yet * been solved. If the problem had no feasible solutions, negative (positive, respectively) * infinity will be returned if maximizing (minimizing). * * @return The value of the objective function at the solution. **/ public double objectiveValue() { try { if (isSolved) return model.get(GRB.DoubleAttr.ObjVal); int status = model.get(GRB.IntAttr.Status); if (status == GRB.INFEASIBLE || status == GRB.INF_OR_UNBD || status == GRB.UNBOUNDED) return model.get(GRB.IntAttr.ModelSense) == -1 ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; } catch (GRBException e) { handleException(e); } return 0; }
/** * Creates a Gurobi linear expression object representing the dot product of the variables with * the specified indexes and the specified coefficients. * * @param ids The indexes of the variables. * @param c The corresponding coefficients. * @return A Gurobi linear expression representing the dot product. **/ protected GRBLinExpr makeLinearExpression(int[] ids, double[] c) { try { GRBLinExpr expr = new GRBLinExpr(); expr.addTerms(c, idsToVariables(ids)); return expr; } catch (GRBException e) { handleException(e); } return null; }
public int addRealVariable(double c) { int id = variables.size(); try { variables.add(model.addVar(-GRB.INFINITY, GRB.INFINITY, c, GRB.CONTINUOUS, "x_" + id)); } catch (GRBException e) { handleException(e); } // TODO: delete the line below once we get Gurobi 4.0 objectiveCoefficients.add(c); needsUpdate = true; return id; }
/** * This method clears the all constraints and variables out of the ILP solver's problem * representation, bringing the <code>ILPSolver</code> instance back to the state it was in when * first constructed. **/ public void reset() { try { model = new GRBModel(environment); } catch (GRBException e) { handleException(e); } variables = new OVector(); SOSes = new OVector(); objectiveCoefficients = new DVector(); needsUpdate = isSolved = false; }
/** * This method clears the all constraints and variables out of the ILP solver's problem * representation, bringing the <code>ILPSolver</code> instance back to the state it was in when * first constructed. **/ public void reset() { try { model = new GRBModel(environment); } catch (GRBException e) { handleException(e); } variables = new OVector(); SOSes = new OVector(); objectiveCoefficients = new DVector(); needsUpdate = isSolved = false; }
/** * Adds a general, multi-valued discrete variable, which is implemented as a set of Boolean * variables, one per value of the discrete variable, with exactly one of those variables set * <code>true</code> at any given time. * * @param c The objective function coefficients for the new Boolean variables. * @return The indexes of the newly created variables. **/ public int[] addDiscreteVariable(double[] c) { int[] result = new int[c.length]; for (int i = 0; i < c.length; ++i) result[i] = addBooleanVariable(c[i]); double[] w = new double[c.length]; Arrays.fill(w, 1); addGreaterThanConstraint(result, w, 1); try { SOSes.add(model.addSOS(idsToVariables(result), w, GRB.SOS_TYPE1)); } catch (GRBException e) { handleException(e); } return result; }
/** * Adds a new Boolean variable (an integer variable constrained to take either the value 0 or * the value 1) with the specified coefficient in the objective function to the problem. * * @param c The objective function coefficient for the new Boolean variable. * @return The index of the created variable. **/ public int addBooleanVariable(double c) { int id = variables.size(); try { variables.add(model.addVar(0, 1, c, GRB.BINARY, "x_" + id)); } catch (GRBException e) { handleException(e); } // TODO: delete the line below once we get Gurobi 4.0 objectiveCoefficients.add(c); needsUpdate = true; return id; }