Skip to content

Commit

Permalink
Merge pull request #42 from eMoflon/feature/mutable-config
Browse files Browse the repository at this point in the history
Makes the `SolverConfig` mutable + fixes a minor bug with an empty objective + adds names for constraints
  • Loading branch information
maxkratz authored Oct 24, 2023
2 parents 92a94e7 + e4de4df commit 4dabd97
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 148 deletions.
3 changes: 1 addition & 2 deletions org.emoflon.ilp/src/org/emoflon/ilp/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

/**
* Record class representing constant values.
*
*/
public record Constant(double weight) {

}
}
29 changes: 27 additions & 2 deletions org.emoflon.ilp/src/org/emoflon/ilp/Constraint.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package org.emoflon.ilp;

public interface Constraint {
// empty
/**
* Abstract class for all constraints.
*/
public abstract class Constraint {
/**
* The name of the constraint.
*/
private String name;

/**
* Returns the name of the constraint.
*
* @return Name of the constraint.
*/
public String getName() {
return name;
}

/**
* Sets the name of the constraint to a given value.
*
* @param name New name of the constraint to set.
*/
public void setName(final String name) {
this.name = name;
}

}
35 changes: 18 additions & 17 deletions org.emoflon.ilp/src/org/emoflon/ilp/CplexSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ private void init() {

// set configuration parameters
// Presolve?
cplex.setParam(IloCplex.Param.Preprocessing.Presolve, config.presolveEnabled());
cplex.setParam(IloCplex.Param.Preprocessing.Presolve, config.isPresolveEnabled());
// Random Seed?
if (config.randomSeedEnabled()) {
cplex.setParam(IloCplex.Param.RandomSeed, config.randomSeed());
if (config.isRandomSeedEnabled()) {
cplex.setParam(IloCplex.Param.RandomSeed, config.getRandomSeed());
}
// Output?
if (!config.debugOutputEnabled()) {
if (!config.isDebugOutputEnabled()) {
cplex.setOut(null);
}
// Tolerance?
if (config.toleranceEnabled()) {
cplex.setParam(IloCplex.Param.MIP.Tolerances.Integrality, config.tolerance());
cplex.setParam(IloCplex.Param.MIP.Tolerances.AbsMIPGap, config.tolerance());
if (config.isToleranceEnabled()) {
cplex.setParam(IloCplex.Param.MIP.Tolerances.Integrality, config.getTolerance());
cplex.setParam(IloCplex.Param.MIP.Tolerances.AbsMIPGap, config.getTolerance());
}
// Timeout?
if (config.timeoutEnabled()) {
cplex.setParam(IloCplex.Param.TimeLimit, config.timeout());
if (config.isTimeoutEnabled()) {
cplex.setParam(IloCplex.Param.TimeLimit, config.getTimeout());
}

// set output path, if configured
if (config.outputEnabled()) {
this.outputPath = config.outputPath();
if (config.isOutputEnabled()) {
this.outputPath = config.getOutputPath();
}
} catch (final IloException e) {
throw new RuntimeException(e);
Expand All @@ -84,6 +84,7 @@ private void init() {
@Override
public void buildILPProblem(Problem problem) {
this.problem = problem;
problem.validateConstraints();

/*
* // Quadratic Constraints or Functions are not yet implemented if
Expand Down Expand Up @@ -183,13 +184,13 @@ private IloIntVar translateIntegerVariable(IntegerVariable integerVariable) {
int lb = integerVariable.getLowerBound();
int ub = integerVariable.getUpperBound();

if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (integerVariable.isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
integerVariable.setLowerBound((int) lb);
}
if (integerVariable.isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
integerVariable.setUpperBound((int) ub);
}
}
Expand All @@ -216,13 +217,13 @@ private IloNumVar translateRealVariable(RealVariable realVariable) {
double lb = realVariable.getLowerBound();
double ub = realVariable.getUpperBound();

if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (realVariable.isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
realVariable.setLowerBound(lb);
}
if (realVariable.isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
realVariable.setUpperBound(ub);
}
}
Expand Down
21 changes: 12 additions & 9 deletions org.emoflon.ilp/src/org/emoflon/ilp/GeneralConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,51 @@

import java.util.List;

// Class that is currently only used for Gurobi OrConstraints
// Could be extended for other general constraints (min, max, ...)
public interface GeneralConstraint extends Constraint {
/**
* Class that is currently only used for Gurobi OrConstraints. Could be extended
* for other general constraints (min, max, ...).
*/
public abstract class GeneralConstraint extends Constraint {
/**
* Returns all variables that are part of the constraint.
*
* @return List of variables.
*/
public List<? extends Variable<?>> getVariables();
public abstract List<? extends Variable<?>> getVariables();

/**
* Sets the variables for this constraint.
*
* @param variables List of variables.
*/
public void setVariables(List<Variable<?>> variables);
public abstract void setVariables(List<Variable<?>> variables);

/**
* Adds one variable to the list of variables.
*
* @param var New variable to be added.
*/
public void addVariable(Variable<?> var);
public abstract void addVariable(Variable<?> var);

/**
* Returns the result variable of the constraint.
*
* @return Result variable.
*/
public Variable<?> getResult();
public abstract Variable<?> getResult();

/**
* Sets the result variable of the constraint.
*
* @param res New result variable.
*/
public void setResult(Variable<?> res);
public abstract void setResult(Variable<?> res);

/**
* Returns the type of the constraint.
*
* @return Type of the constraint.
*/
public ConstraintType getType();
public abstract ConstraintType getType();

}
33 changes: 17 additions & 16 deletions org.emoflon.ilp/src/org/emoflon/ilp/GlpkSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,36 @@ private void init() {
GLPK.glp_init_iocp(iocp);
// Set configuration parameters
// Presolve?
iocp.setPresolve(config.presolveEnabled() ? GLPK.GLP_ON : GLPK.GLP_OFF);
iocp.setPresolve(config.isPresolveEnabled() ? GLPK.GLP_ON : GLPK.GLP_OFF);
// Random Seed?
// not supported in glpk for Java
// --seed value is option fo glpsol
// Debug Output?
if (!config.debugOutputEnabled()) {
if (!config.isDebugOutputEnabled()) {
GLPK.glp_term_out(GLPK.GLP_OFF);
}
// Output?
if (config.outputEnabled()) {
this.outputPath = config.outputPath();
if (config.isOutputEnabled()) {
this.outputPath = config.getOutputPath();
}
// Tolerance?
if (config.toleranceEnabled()) {
iocp.setTol_int(config.tolerance());
iocp.setTol_obj(config.tolerance());
iocp.setMip_gap(config.tolerance());
if (config.isToleranceEnabled()) {
iocp.setTol_int(config.getTolerance());
iocp.setTol_obj(config.getTolerance());
iocp.setMip_gap(config.getTolerance());
}
// Timeout?
if (config.timeoutEnabled()) {
if (config.isTimeoutEnabled()) {
// GLPK expects milliseconds
iocp.setTm_lim((int) config.timeout() * 1000);
iocp.setTm_lim((int) config.getTimeout() * 1000);
}

}

@Override
public void buildILPProblem(Problem problem) {
this.problem = problem;
problem.validateConstraints();

// Quadratic Constraints or Functions are not supported by GLPK
if (problem.getConstraints().stream().anyMatch(QuadraticConstraint.class::isInstance)
Expand Down Expand Up @@ -139,27 +140,27 @@ private void translateVariables(Map<String, Variable<?>> vars) {
break;
case INTEGER:
// Check if other bounds are defined in the solver config
if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (((IntegerVariable) var).isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
((IntegerVariable) var).setLowerBound((int) lb);
}
if (((IntegerVariable) var).isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
((IntegerVariable) var).setUpperBound((int) ub);
}
}
translateVariable(j, GLPK.GLP_IV, lb, ub);
break;
case REAL:
// Check if other bounds are defined in the solver config
if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (((RealVariable) var).isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
((RealVariable) var).setLowerBound(lb);
}
if (((RealVariable) var).isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
((RealVariable) var).setUpperBound(ub);
}
}
Expand Down
41 changes: 21 additions & 20 deletions org.emoflon.ilp/src/org/emoflon/ilp/GurobiSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ private void init() throws GRBException {

// set configuration parameters
// Presolve?
env.set(IntParam.Presolve, config.presolveEnabled() ? 1 : 0);
env.set(IntParam.Presolve, config.isPresolveEnabled() ? 1 : 0);
// Random Seed?
if (config.randomSeedEnabled()) {
env.set(IntParam.Seed, config.randomSeed());
if (config.isRandomSeedEnabled()) {
env.set(IntParam.Seed, config.getRandomSeed());
}
// Output?
if (!config.debugOutputEnabled()) {
if (!config.isDebugOutputEnabled()) {
env.set(IntParam.OutputFlag, 0);
}
// Tolerance?
if (config.toleranceEnabled()) {
env.set(DoubleParam.OptimalityTol, config.tolerance());
env.set(DoubleParam.IntFeasTol, config.tolerance());
if (config.isToleranceEnabled()) {
env.set(DoubleParam.OptimalityTol, config.getTolerance());
env.set(DoubleParam.IntFeasTol, config.getTolerance());
}
// Timeout?
if (config.timeoutEnabled()) {
env.set(DoubleParam.TimeLimit, config.timeout());
if (config.isTimeoutEnabled()) {
env.set(DoubleParam.TimeLimit, config.getTimeout());
}

// create new Gurobi Model/Problem
model = new GRBModel(env);

// set output path, if configured
if (config.outputEnabled()) {
this.outputPath = config.outputPath();
if (config.isOutputEnabled()) {
this.outputPath = config.getOutputPath();
}

grbVars.clear();
Expand All @@ -91,6 +91,7 @@ private void init() throws GRBException {
@Override
public void buildILPProblem(Problem problem) {
this.problem = problem;
problem.validateConstraints();

// Substitute Or Constraints
problem.substituteOr();
Expand Down Expand Up @@ -217,7 +218,7 @@ private void translateNormalConstraint(NormalConstraint constraint) {
tempLin.addTerm(term.getWeight(), grbVars.get(term.getVar1().getName()));
}
try {
model.addConstr(tempLin, op, rhs, constraint.toString());
model.addConstr(tempLin, op, rhs, constraint.getName());
} catch (GRBException e) {
throw new RuntimeException(e);
}
Expand All @@ -233,7 +234,7 @@ private void translateNormalConstraint(NormalConstraint constraint) {
}
}
try {
model.addQConstr(tempQuad, op, rhs, constraint.toString());
model.addQConstr(tempQuad, op, rhs, constraint.getName());
} catch (GRBException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -280,7 +281,7 @@ private void translateGeneralConstraint(GeneralConstraint constraint) {
}
}
model.addGenConstrOr(model.addVar(0.0, 1.0, 0.0, GRB.BINARY, res.getName()), grbVars,
constraint.toString());
constraint.getName());
} catch (GRBException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -353,13 +354,13 @@ private GRBVar translateIntegerVariable(IntegerVariable variable) {
int lb = variable.getLowerBound();
int ub = variable.getUpperBound();

if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (variable.isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
variable.setLowerBound((int) lb);
}
if (variable.isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
variable.setUpperBound((int) ub);
}
}
Expand All @@ -386,13 +387,13 @@ private GRBVar translateRealVariable(RealVariable variable) {
double lb = variable.getLowerBound();
double ub = variable.getUpperBound();

if (config.boundsEnabled()) {
if (config.isBoundsEnabled()) {
if (variable.isDefaultLowerBound()) {
lb = config.lowerBound();
lb = config.getLowerBound();
variable.setLowerBound(lb);
}
if (variable.isDefaultUpperBound()) {
ub = config.upperBound();
ub = config.getUpperBound();
variable.setUpperBound(ub);
}
}
Expand Down
2 changes: 1 addition & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/LinearConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* w_1 * x_1 + w_2 * x_2 + ... (>= | > | = | != | < | <=) rhs
*/
public class LinearConstraint implements NormalConstraint {
public class LinearConstraint extends NormalConstraint {

private List<Term> lhsTerms = new ArrayList<Term>();
private Operator op;
Expand Down
Loading

0 comments on commit 4dabd97

Please sign in to comment.