Skip to content

Commit

Permalink
Adds javadoc refs and changes internal weights representation in SOS1
Browse files Browse the repository at this point in the history
  • Loading branch information
luuma27 committed Sep 20, 2023
1 parent 0e993cb commit 6bb1dfb
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void testSOS1Substitution() {

// SOS1(x, y, z)
SOS1Constraint sos = new SOS1Constraint();
sos.setVariables(Arrays.asList(x, y, z));
sos.addVariables(Arrays.asList(x, y, z));

List<LinearConstraint> substitution = sos.convert();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,18 +763,16 @@ public void testCplexSOS() {
List<Variable<?>> sosVars1 = new ArrayList<Variable<?>>();
sosVars1.add(r1);
sosVars1.add(r2);
SOS1Constraint sos1 = new SOS1Constraint(sosVars1);
double[] weights = { 1, 2 };
sos1.setWeights(weights);
SOS1Constraint sos1 = new SOS1Constraint(sosVars1, weights);

// SOS1: x0=0 or x2=0
// here: SOS1(r1, r3)

List<Variable<?>> sosVars2 = new ArrayList<Variable<?>>();
sosVars2.add(r1);
sosVars2.add(r3);
SOS1Constraint sos2 = new SOS1Constraint(sosVars2);
sos2.setWeights(weights);
SOS1Constraint sos2 = new SOS1Constraint(sosVars2, weights);

// Model
problem.setObjective(lin);
Expand Down
10 changes: 4 additions & 6 deletions org.emoflon.ilp.tests/src/org/emoflon/ilp/tests/GurobiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void testGurobiExample() {
* System.out.println("===================");
* System.out.println(out.toString());
* System.out.println("Computation Result:"); for (String varName :
* problem.getVariables().keySet()) { System.out.println("Value for " + varName +
* ": " + problem.getVariables().get(varName).getValue()); }
* problem.getVariables().keySet()) { System.out.println("Value for " + varName
* + ": " + problem.getVariables().get(varName).getValue()); }
* System.out.println("===================");
*/

Expand Down Expand Up @@ -721,18 +721,16 @@ public void testGurobiSOS() {
List<Variable<?>> sosVars1 = new ArrayList<Variable<?>>();
sosVars1.add(r1);
sosVars1.add(r2);
SOS1Constraint sos1 = new SOS1Constraint(sosVars1);
double[] weights = { 1, 2 };
sos1.setWeights(weights);
SOS1Constraint sos1 = new SOS1Constraint(sosVars1, weights);

// SOS1: x0=0 or x2=0
// here: SOS1(r1, r3)

List<Variable<?>> sosVars2 = new ArrayList<Variable<?>>();
sosVars2.add(r1);
sosVars2.add(r3);
SOS1Constraint sos2 = new SOS1Constraint(sosVars2);
sos2.setWeights(weights);
SOS1Constraint sos2 = new SOS1Constraint(sosVars2, weights);

// Model
problem.setObjective(lin);
Expand Down
1 change: 1 addition & 0 deletions org.emoflon.ilp/src/org/emoflon/ilp/CplexSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CplexSolver implements Solver {
* The constructor for CplexSolver.
*
* @param config The configuration parameters used for this solver.
* @see SolverConfig
*/
public CplexSolver(final SolverConfig config) {
this.config = config;
Expand Down
7 changes: 6 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

/**
* This abstract class represents functions that can be used in the objective of
* the optimization problem.
* the optimization problem. <br>
* <br>
*
* Sum(terms) + Sum(constants) + Sum(nestedFunctions)
*
*/
public abstract class Function {
Expand Down Expand Up @@ -81,6 +84,7 @@ public List<Constant> getConstants() {
* Adds a weighted function as a nested function to this function.
*
* @param func Weighted function to be added to this function.
* @see WeightedFunction
*/
public void addNestedFunction(WeightedFunction func) {
this.nestedFunctions.add(func);
Expand All @@ -91,6 +95,7 @@ public void addNestedFunction(WeightedFunction func) {
*
* @param func Function to be added.
* @param weight Weight of the new weighted function.
* @see Function
*/
public void addNestedFunction(Function func, double weight) {
this.nestedFunctions.add(new WeightedFunction(func, weight));
Expand Down
1 change: 1 addition & 0 deletions org.emoflon.ilp/src/org/emoflon/ilp/GlpkSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GlpkSolver implements Solver {
* The constructor for GlpkSolver.
*
* @param config The configuration parameters used for this solver.
* @see SolverConfig
*/
public GlpkSolver(final SolverConfig config) {
this.config = config;
Expand Down
1 change: 1 addition & 0 deletions org.emoflon.ilp/src/org/emoflon/ilp/GurobiSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class GurobiSolver implements Solver {
* The constructor for GurobiSolver.
*
* @param config The configuration parameters used for this solver.
* @see SolverConfig
*/
public GurobiSolver(final SolverConfig config) {
try {
Expand Down
11 changes: 9 additions & 2 deletions org.emoflon.ilp/src/org/emoflon/ilp/LinearConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import java.util.List;

/**
* This class represents linear constraints.
* This class represents linear constraints. <br>
*
* The terms on the left-hand side are summed up.
* The terms on the left-hand side are summed up. <br>
* <br>
*
* w_1 * x_1 + w_2 * x_2 + ... (>= | > | = | != | < | <=) rhs
*/
Expand All @@ -24,6 +25,8 @@ public class LinearConstraint implements NormalConstraint {
* constraint.
* @param op The operator used for this constraint.
* @param rhs The value on the right-hand side of the constraint.
* @see LinearTerm
* @see Operator
*/
public LinearConstraint(List<Term> lhsTerms, Operator op, double rhs) {
this.setLhsTerms(lhsTerms);
Expand All @@ -36,6 +39,7 @@ public LinearConstraint(List<Term> lhsTerms, Operator op, double rhs) {
*
* @param op The operator used for this constraint.
* @param rhs The value on the right-hand side of the constraint.
* @see Operator
*/
public LinearConstraint(Operator op, double rhs) {
this.setLhsTerms(new ArrayList<Term>());
Expand All @@ -51,6 +55,8 @@ public LinearConstraint(Operator op, double rhs) {
* @param op The operator used for this constraint.
* @param rhs The value on the right-hand side of the constraint.
* @param epsilon A small value used for conversion if necessary.
* @see LinearTerm
* @see Operator
*/
public LinearConstraint(List<Term> lhsTerms, Operator op, double rhs, double epsilon) {
this.setLhsTerms(lhsTerms);
Expand All @@ -65,6 +71,7 @@ public LinearConstraint(List<Term> lhsTerms, Operator op, double rhs, double eps
* @param op The operator used for this constraint.
* @param rhs The value on the right-hand side of the constraint.
* @param epsilon A small value used for conversion if necessary.
* @see Operator
*/
public LinearConstraint(Operator op, double rhs, double epsilon) {
this.setLhsTerms(new ArrayList<Term>());
Expand Down
8 changes: 7 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/LinearFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
/**
* This class represents linear functions. A linear function can consist of
* multiple terms (weight * variable), constants and nested weighted linear
* functions (weight * function).
* functions (weight * function). <br>
* <br>
*
* function = term1 + term2 + ... + constant1 + constant2 + ... + w1 * func1 +
* w2 * func2 + ...
Expand All @@ -23,6 +24,9 @@ public class LinearFunction extends Function {
* @param terms A list of linear terms.
* @param constantTerms A list of constants.
* @param nestedFunctions A list of nested weighted linear functions.
* @see LinearTerm
* @see Constant
* @see WeightedFunction
*/
public LinearFunction(List<Term> terms, List<Constant> constantTerms, List<WeightedFunction> nestedFunctions) {
if (terms.stream().anyMatch(QuadraticTerm.class::isInstance)) {
Expand Down Expand Up @@ -51,6 +55,8 @@ public LinearFunction() {
*
* @param terms A list of linear terms.
* @param constantTerms A list of constants.
* @see LinearTerm
* @see Constant
*/
public LinearFunction(List<Term> terms, List<Constant> constantTerms) {
this.terms = terms;
Expand Down
3 changes: 2 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/LinearTerm.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.emoflon.ilp;

/**
* This class represents linear terms.
* This class represents linear terms. <br>
* <br>
*
* linear term = weight * variable
*
Expand Down
6 changes: 6 additions & 0 deletions org.emoflon.ilp/src/org/emoflon/ilp/NormalConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,39 @@ public interface NormalConstraint extends Constraint {
* Returns a list of the terms on the left-hand side of the constraint.
*
* @return Current list of terms on the left-hand side of the constraint.
* @see Term
*/
public List<Term> getLhsTerms();

/**
* Sets the terms of the constraint.
*
* @param lhsTerms New list of terms to be set for the constraint.
* @see Term
*/
public void setLhsTerms(List<Term> lhsTerms);

/**
* Adds a term to the existing terms on the left-hand side of the constraint.
*
* @param term New term to be added to the constraint.
* @see Term
*/
public void addTerm(Term term);

/**
* Returns the operator of the constraint.
*
* @return Current operator of the constraint.
* @see Operator
*/
public Operator getOp();

/**
* Sets the operator of the constraint.
*
* @param op Operator to be set for the constraint.
* @see Operator
*/
public void setOp(Operator op);

Expand All @@ -57,6 +62,7 @@ public interface NormalConstraint extends Constraint {
* Returns the type of the constraint.
*
* @return Type of the constraint.
* @see ConstraintType
*/
public ConstraintType getType();

Expand Down
5 changes: 4 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/ObjectiveType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.emoflon.ilp;

/**
* The type of the objective function of the problem.
* The type of the objective function of the problem. <br>
* <br>
*
* Minimize (MIN) or maximize (MAX).
*
*/
public enum ObjectiveType {
Expand Down
5 changes: 4 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

/**
* The operators used in the normal constraints (linear/quadratic constraints).
*
* <br>
* <br>
*
* LESS, LESS_OR_EQUAL, EQUAL, GREATER_OR_EQUAL, GREATER, NOT_EQUAL
*/
public enum Operator {
LESS, LESS_OR_EQUAL, EQUAL, GREATER_OR_EQUAL, GREATER, NOT_EQUAL;
Expand Down
11 changes: 10 additions & 1 deletion org.emoflon.ilp/src/org/emoflon/ilp/OrConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Or(lin_constr_1, lin_constr_2, ..., lin_constr_n)
*
* @see LinearConstraint
*/
public class OrConstraint implements Constraint {

Expand All @@ -19,6 +20,7 @@ public class OrConstraint implements Constraint {
* A constructor for an Or constraint.
*
* @param constraints List of linear constraints affected by this constraint.
* @see LinearConstraint
*/
public OrConstraint(List<LinearConstraint> constraints) {
this.setConstraints(constraints);
Expand All @@ -38,6 +40,7 @@ public OrConstraint() {
*
* @param constraints List of linear constraints affected by this constraint.
* @param epsilon Value of epsilon, used for converting this constraint.
* @see LinearConstraint
*/
public OrConstraint(List<LinearConstraint> constraints, double epsilon) {
this.setConstraints(constraints);
Expand All @@ -60,6 +63,7 @@ public OrConstraint(double epsilon) {
* Returns the linear constraints affected by this constraint.
*
* @return List of linear constraints affected by this constraint.
* @see LinearConstraint
*/
public List<LinearConstraint> getConstraints() {
return this.constraints;
Expand All @@ -70,6 +74,7 @@ public List<LinearConstraint> getConstraints() {
* constraint.
*
* @param constraints List of linear constraints to be added.
* @see LinearConstraint
*/
public void setConstraints(List<LinearConstraint> constraints) {
this.constraints.addAll(constraints);
Expand All @@ -79,6 +84,7 @@ public void setConstraints(List<LinearConstraint> constraints) {
* Returns the constraint type of this constraint.
*
* @return Constraint type (OR).
* @see ConstraintType
*/
public ConstraintType getType() {
return ConstraintType.OR;
Expand All @@ -89,6 +95,7 @@ public ConstraintType getType() {
* constraint.
*
* @param constr Linear constraint to be added.
* @see LinearConstraint
*/
public void addConstraint(LinearConstraint constr) {
constraints.add(constr);
Expand All @@ -113,11 +120,13 @@ public void setEpsilon(double epsilon) {
this.epsilon = epsilon;
}

// TODO: negation of constraints?
// TODO: (future work) negation of constraints
/**
* Converts this Or constraint into a set of linear and SOS1 constraints.
*
* @return List of constraints to substitute this Or constraint.
* @see LinearConstraint
* @see SOS1Constraint
*/
public List<Constraint> convert() {
List<Constraint> substitute = new ArrayList<Constraint>();
Expand Down
Loading

0 comments on commit 6bb1dfb

Please sign in to comment.