Skip to content

Commit

Permalink
Refactoring for v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
irzinfante committed Sep 18, 2022
1 parent f236412 commit 16ac156
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 246 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
###### Note: Use always the latest version of the library.

# Usage of the library
Let's put it simple. Having the following linear programming (LP) problem,

<img src="https://latex.codecogs.com/gif.latex?\newline%20min%20\;%20Z%20=%20-X_1%20-%20X_2\newline%20\;%20-2X_1%20+%202X_2%20\ge%201\newline%20-8X_1%20+%2010X_2\le%2013\newline%20X_1,%20X_2%20\ge%200,\;%20X_1,%20X_2%20\in%20\mathbb{Z}">

our goal is to generate, in the most simple and intuitive way, a fixed-format MPS file representing the problem.

Using the ``fixed-mps`` library the instruction for building the LP problem is the following:
We import the ``fixed-mps`` library from Maven with

```xml
<!-- https://mvnrepository.com/artifact/eu.irzinfante/fixed-mps -->
<dependency>
<groupId>eu.irzinfante</groupId>
<artifactId>fixed-mps</artifactId>
<version>1.0.0</version>
</dependency>
```

Now, the instruction for building the LP problem is the following:

```java
import eu.irzinfante.fixedmps.core.Problem;
Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>eu.irzinfante</groupId>
<artifactId>fixed-mps</artifactId>
<version>0.9.5</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -153,5 +153,4 @@
<url>scm:git:[email protected]:irzinfante/fixed-mps.git</url>
<developerConnection>scm:git:[email protected]:irzinfante/fixed-mps.git</developerConnection>
</scm>

</project>
87 changes: 43 additions & 44 deletions src/main/java/eu/irzinfante/fixedmps/core/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* @author irzinfante [email protected]
* @version 1.0
* @since 1.0
* @version 1.0.0
* @since 1.0.0
*/
public class Constraint {

Expand All @@ -13,37 +13,36 @@ public class Constraint {

/**
* @author irzinfante [email protected]
* @version 1.0
* @since 1.0
* @version 1.0.0
* @since 1.0.0
*/
public static class ConstraintBuilder {

private double coeffs[];

/**
* Initializes a Constraint object builder
*
* @param coeffs Coefficients of the variables in the
* same order they are added to the Problem object builder.
* Extra coefficients are ignored and, if there are less
* coefficients than variables, missing coefficients
* are set to zero
*
* @since 1.0
*/
* Initializes a Constraint object builder
*
* @param coeffs Coefficients of the variables in the
* same order they are added to the Problem object builder.
* Extra coefficients are ignored and, if there are less
* coefficients than variables, missing coefficients
* are set to zero
*
* @since 1.0.0
*/
public ConstraintBuilder (double ... coeffs) {

this.coeffs = coeffs;
}

/**
* Creates an inequation constraint (≤ k)
*
* @param free The free term of the inequation, k
* @return a less-than type constraint
*
* @since 1.0
*/
* Creates an inequation constraint (≤ k)
*
* @param free The free term of the inequation, k
* @return a less-than type constraint
*
* @since 1.0.0
*/
public Constraint lessThan(double free) {

Constraint constraint = new Constraint();
Expand All @@ -56,13 +55,13 @@ public Constraint lessThan(double free) {
}

/**
* Creates an inequation constraint (≥ k)
*
* @param free The free term of the inequation, k
* @return a greater-than type constraint
*
* @since 1.0
*/
* Creates an inequation constraint (≥ k)
*
* @param free The free term of the inequation, k
* @return a greater-than type constraint
*
* @since 1.0.0
*/
public Constraint greaterThan(double free) {

Constraint constraint = new Constraint();
Expand All @@ -75,13 +74,13 @@ public Constraint greaterThan(double free) {
}

/**
* Creates a equation constraint
*
* @param free The free term of the equation
* @return a equality type constraint
*
* @since 1.0
*/
* Creates a equation constraint
*
* @param free The free term of the equation
* @return a equality type constraint
*
* @since 1.0.0
*/
public Constraint equalTo(double free) {

Constraint constraint = new Constraint();
Expand All @@ -97,29 +96,29 @@ public Constraint equalTo(double free) {

private Constraint() {
}

public double[] getCoeffs() {
return coeffs;
}

private void setCoeffs(double coeffs[]) {
this.coeffs = coeffs;
}

public char getType() {
return type;
}

private void setType(char type) {
this.type = type;
}

public double getFree() {
return free;
}

private void setFree(double free) {
this.free = free;
}

}
}
101 changes: 51 additions & 50 deletions src/main/java/eu/irzinfante/fixedmps/core/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

/**
* @author irzinfante [email protected]
* @version 1.0
* @since 1.0
* @version 1.0.0
* @since 1.0.0
*/
public class Problem {

private Variable columns[];
private Constraint rows[];

/**
* @author irzinfante [email protected]
* @version 1.0
* @since 1.0
* @version 1.0.0
* @since 1.0.0
*/
public static class ProblemBuilder {

Expand All @@ -26,12 +26,12 @@ public static class ProblemBuilder {
private int numVar;

/**
* Initializes a Problem object builder
*
* @param variables Variables of the problem
*
* @since 1.0
*/
* Initializes a Problem object builder
*
* @param variables Variables of the problem
*
* @since 1.0.0
*/
public ProblemBuilder(Variable ... variables) {

this.columns = variables;
Expand All @@ -40,13 +40,13 @@ public ProblemBuilder(Variable ... variables) {
}

/**
* Adds a constraint to the problem
*
* @param constraint Constraint to add to the problem
* @return ProblemBuilder with the added constraint
*
* @since 1.0
*/
* Adds a constraint to the problem
*
* @param constraint Constraint to add to the problem
* @return ProblemBuilder with the added constraint
*
* @since 1.0.0
*/
public ProblemBuilder addConstraint(Constraint constraint) {

double coeffs[] = new double[numVar];
Expand All @@ -55,60 +55,61 @@ public ProblemBuilder addConstraint(Constraint constraint) {
}

switch (constraint.getType()) {
case 'E':
rows.add(new ConstraintBuilder(coeffs).equalTo(constraint.getFree()));
break;
case 'L':
rows.add(new ConstraintBuilder(coeffs).lessThan(constraint.getFree()));
break;
case 'G':
rows.add(new ConstraintBuilder(coeffs).greaterThan(constraint.getFree()));
break;
case 'E':
rows.add(new ConstraintBuilder(coeffs).equalTo(constraint.getFree()));
break;
case 'L':
rows.add(new ConstraintBuilder(coeffs).lessThan(constraint.getFree()));
break;
case 'G':
rows.add(new ConstraintBuilder(coeffs).greaterThan(constraint.getFree()));
break;
}

return this;
}

/**
* Returns the created Problem object
*
* @return LP problem
*
* @since 1.0
*/
* Returns the created Problem object
*
* @return LP problem
*
* @since 1.0.0
*/
public Problem build() {

Problem lp = new Problem();
Constraint rows[] = new Constraint[this.rows.size()];
for(int i = 0; i < this.rows.size(); i++) {
rows[i] = this.rows.get(i);
}

lp.setColumns(this.columns);
lp.setRows(rows);

return lp;
Problem lp = new Problem();
Constraint rows[] = new Constraint[this.rows.size()];
for(int i = 0; i < this.rows.size(); i++) {
rows[i] = this.rows.get(i);
}
lp.setColumns(this.columns);
lp.setRows(rows);
return lp;
}

}

private Problem() {
}

public Variable[] getColumns() {
return columns;
}

private void setColumns(Variable[] columns) {
this.columns = columns;
}

public Constraint[] getRows() {
return rows;
}

private void setRows(Constraint rows[]) {
this.rows = rows;
}
}

}
Loading

0 comments on commit 16ac156

Please sign in to comment.