diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalProgramParameters.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalProgramParameters.java index e3746ca2edb6c95448d58f6713912b9a7392f7a0..2161372aebe27fcbc652c4229a7e644be1b82f90 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalProgramParameters.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalProgramParameters.java @@ -4,23 +4,28 @@ package plugins.nchenouard.linearprogrammingfullsimplex; * Parameters used for the simplex algorithm * * By definition, the optimization problem is of the form: + * + * <pre> * min_x c'*x or max_x c'*x - * A*x <= b - * with x >=0 - * where x, b, c are column vectors and c' is the transpose of c. * stands for the matrix multiplication. - * Here each constraint A[i]*x can be either "equal" or "lower than or equal" constraints - * based on the boolean value in the equalityConstraints vector: + * A*x <= b + * with x >=0 + * </pre> + * + * where x, b, c are column vectors and c' is the transpose of c. * stands for + * the matrix multiplication. + * <p> + * Here each constraint A[i]*x can be either "equal" or "lower than or equal" + * constraints based on the boolean value in the equalityConstraints vector: + * <p> * A[i]*x == b if equalityConstraints[i] - * A[i]*x <= b if equalityConstraints[i] + * <p> + * A[i]*x <= b if equalityConstraints[i] + * <p> * * * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class CanonicalProgramParameters @@ -42,10 +47,13 @@ public class CanonicalProgramParameters * The value for each constraint. */ public double[] b; + /** * Defines the type of constraint + * <p> * A[i]*x == b if equalityConstraints[i] - * A[i]*x <= b if equalityConstraints[i] + * <p> + * A[i]*x <= b if equalityConstraints[i] */ public boolean[] equalityConstraints; } \ No newline at end of file diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalSimplexProgram.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalSimplexProgram.java index bf3791c92db2a3ca726a1b8e9916b47d6d671676..451e9c12f39a09e2cff73125a30a3e67f472b34b 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalSimplexProgram.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/CanonicalSimplexProgram.java @@ -10,26 +10,27 @@ import java.io.IOException; import java.io.InputStreamReader; /** - * Linear program for the Simplex algorithm. Inheriting classes should implement the pivoting rule. + * Linear program for the Simplex algorithm. Inheriting classes should implement + * the pivoting rule. * - * By definition, the optimization problem is of the form: - * min_x c'*x or max_x c'*x - * A*x <= b - * with x >=0 - * where x, b, c are column vectors and c' is the transpose of c. * stands for the matrix multiplication. - * Here each constraint A[i]*x can be either "equal" or "lower than or equal" constraints - * based on the boolean value in the equalityConstraints vector: - * A[i]*x == b if equalityConstraints[i] - * A[i]*x <= b if equalityConstraints[i] + * By definition, the optimization problem is of the form: min_x c'*x or max_x + * c'*x A*x <= b with x >=0 where x, b, c are column vectors and c' is the + * transpose of c. + * + * * stands for the matrix multiplication. * + * Here each constraint <code>A[i]*x</code> can be either "equal" or "lower than + * or equal" constraints based on the boolean value in the equality constraints + * vector: + * <p> + * A[i]*x == b if equalityConstraints[i] + * <p> + * A[i]*x <= b if equalityConstraints[i] + * <p> * * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public abstract class CanonicalSimplexProgram @@ -65,13 +66,20 @@ public abstract class CanonicalSimplexProgram /** * Constructor specifying parameters of the linear programming problem. - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ - public CanonicalSimplexProgram(double[][] A, double[] b, double[] c, boolean maximization, boolean[] equality) + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ + public CanonicalSimplexProgram(final double[][] A, final double[] b, final double[] c, final boolean maximization, final boolean[] equality) { this.parameters = new CanonicalProgramParameters(); this.parameters.A = A; @@ -86,7 +94,7 @@ public abstract class CanonicalSimplexProgram * Constructor specifying parameters of the linear programming problem. * @param p Parameters of the linear programming problem. */ - public CanonicalSimplexProgram(CanonicalProgramParameters p) + public CanonicalSimplexProgram(final CanonicalProgramParameters p) { this.parameters = p; this.numVariables = p.c.length; @@ -99,13 +107,13 @@ public abstract class CanonicalSimplexProgram * */ public boolean solvePrimalSimplex() { - double[][] A = parameters.A; - double[] b = parameters.b; - boolean[] eq = parameters.equalityConstraints; - double[] c = parameters.c; + final double[][] A = parameters.A; + final double[] b = parameters.b; + final boolean[] eq = parameters.equalityConstraints; + final double[] c = parameters.c; boolean onlyInequality = true; - for (boolean e:eq) + for (final boolean e:eq) { if (e) { @@ -133,10 +141,10 @@ public abstract class CanonicalSimplexProgram if(hasNegativeRHS) { // add slack variables and multiply by -1 when corresponding to a negative right-hand-side - double[][] A2 = new double[numConstraints][numConstraints + numVariables]; - double[] c2 = new double[numConstraints + numVariables]; + final double[][] A2 = new double[numConstraints][numConstraints + numVariables]; + final double[] c2 = new double[numConstraints + numVariables]; System.arraycopy(c, 0, c2, 0, numVariables); // no cost for slack variables - boolean[] equality = new boolean[numConstraints]; + final boolean[] equality = new boolean[numConstraints]; for (int i = 0; i < numConstraints; i++) { equality[i] = true; @@ -159,7 +167,7 @@ public abstract class CanonicalSimplexProgram // we have now a problem with only equality constraints and non-negative right-hand-side if(solveWithEqualities(false)) { - double[] tmpSolution = this.solution; + final double[] tmpSolution = this.solution; this.solution = new double[numVariables]; System.arraycopy(tmpSolution, 0, solution, 0, numVariables); } @@ -175,9 +183,9 @@ public abstract class CanonicalSimplexProgram { // we need to solve first a problem to find the initial tableau // we will add slack variables and minimize the cost of the sum of the slack variables for the equality constraints - boolean maximization = false; - boolean[] eq2 = new boolean[numConstraints]; - boolean[] isNegativeConstraint = new boolean[numConstraints]; + final boolean maximization = false; + final boolean[] eq2 = new boolean[numConstraints]; + final boolean[] isNegativeConstraint = new boolean[numConstraints]; for (int i = 0; i < numConstraints; i++) { if (b[i] < 0) @@ -188,7 +196,7 @@ public abstract class CanonicalSimplexProgram A[i][j] = -A[i][j]; } } - CanonicalSimplexProgram modifiedProgram = createNewProgram(A, b, c, maximization, eq2); + final CanonicalSimplexProgram modifiedProgram = createNewProgram(A, b, c, maximization, eq2); if (modifiedProgram.solvePhaseICanonicalSimplex()) { modifiedProgram.tableau0.setUnitScoreToSlackVars(eq2); @@ -221,7 +229,7 @@ public abstract class CanonicalSimplexProgram * Solve a problem that contains only inequality constraints * @return true if optimization succeeded, false otherwise. * */ - private boolean solveWithInequalities(boolean compareWithLPSolve) + private boolean solveWithInequalities(final boolean compareWithLPSolve) { // Canonical form: initial tableau is built by introducing unit vectors solvePhaseICanonicalSimplex(); @@ -238,7 +246,7 @@ public abstract class CanonicalSimplexProgram * Solve a problem that contains some equality constraints * @return true if optimization succeeded, false otherwise. * */ - private boolean solveWithEqualities(boolean compareWithLPSolve) + private boolean solveWithEqualities(final boolean compareWithLPSolve) { tableau0 = new TableauMinSlackObjective(this); if (verbose) @@ -261,7 +269,7 @@ public abstract class CanonicalSimplexProgram // } // } // } - boolean success = solvePhaseIICanonicalSimplex(tableau0, false); // minimization of the init tableau + final boolean success = solvePhaseIICanonicalSimplex(tableau0, false); // minimization of the init tableau // { // double c[] = new double[tableau0.numCol]; // for (int i = 0; i < numConstraints; i ++) @@ -315,7 +323,7 @@ public abstract class CanonicalSimplexProgram * Solve the phase II of the Simplex algorithm for canonical problem: no equality constraints nor negative constraints * @return true if optimization succeeded, false otherwise. * */ - public boolean solvePhaseIICanonicalSimplex(TableauWithSlackVariables tableau, boolean max) + public boolean solvePhaseIICanonicalSimplex(final TableauWithSlackVariables tableau, final boolean max) { if (tableau == null) return false; @@ -327,7 +335,7 @@ public abstract class CanonicalSimplexProgram while(feasible && !reachedOptimum) { // start optimization - int colIdx = getPivotColumn(tableau, max); + final int colIdx = getPivotColumn(tableau, max); if (colIdx < 0)// optimal solution achieved { reachedOptimum = true; @@ -335,7 +343,7 @@ public abstract class CanonicalSimplexProgram } else { - int rowIdx = getRowidx(tableau, colIdx); + final int rowIdx = getRowidx(tableau, colIdx); if (rowIdx < 0) // not feasible { feasible = false; @@ -374,12 +382,19 @@ public abstract class CanonicalSimplexProgram /** * Duplicate the program - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ protected abstract CanonicalSimplexProgram createNewProgram(double[][] A, double[] b, double[] c, boolean maximization, boolean[] equality); /** @@ -403,8 +418,8 @@ public abstract class CanonicalSimplexProgram * */ public void checkSolution() { - double[][] A0 = parameters.A; - double[] b0 = parameters.b; + final double[][] A0 = parameters.A; + final double[] b0 = parameters.b; if (solution != null) { for (int i = 0; i < numConstraints; i++) @@ -465,7 +480,7 @@ public abstract class CanonicalSimplexProgram /** * Set the parameters of the linear programming problem * */ - public void setParameters(CanonicalProgramParameters parameters) { + public void setParameters(final CanonicalProgramParameters parameters) { this.parameters = parameters; } @@ -545,7 +560,7 @@ public abstract class CanonicalSimplexProgram * */ public static String getHelp() { - String helpString = "Run the solver for example problems or for a user-defined system\n" + final String helpString = "Run the solver for example problems or for a user-defined system\n" + "Empty arguments to use the default example.\n" + "Enter an integer from 0 to 3 for different example scenarios.\n" + "Enter -1 for a user-defined scenario through text files.\n " @@ -591,7 +606,7 @@ public abstract class CanonicalSimplexProgram * For the equality file '0' stands for 'false' and '1' for true. * * */ - public static void main(String [] args) + public static void main(final String [] args) { int scenario = 0; if (args.length > 0) @@ -606,7 +621,7 @@ public abstract class CanonicalSimplexProgram try { scenario = Integer.parseInt(args[0]); } - catch (NumberFormatException e) + catch (final NumberFormatException e) { System.out.println("Invalid input argument"); System.out.println("Use input option -help to display the manual of the software"); @@ -754,14 +769,14 @@ public abstract class CanonicalSimplexProgram equality = new boolean[tab.length-1]; for (int i = 0; i < tab.length-1; i++) { - double d = Double.parseDouble(tab[i]); + final double d = Double.parseDouble(tab[i]); equality[i] = d > 0; } reader.close(); fis.close(); - int numRow = b.length; - int numCol = c.length; + final int numRow = b.length; + final int numCol = c.length; file = new File(fileA); fis = new FileInputStream(file); @@ -781,10 +796,10 @@ public abstract class CanonicalSimplexProgram } reader.close(); fis.close(); - } catch (FileNotFoundException e) { + } catch (final FileNotFoundException e) { e.printStackTrace(); return; - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); return; } @@ -856,14 +871,14 @@ public abstract class CanonicalSimplexProgram System.out.println("Use input option -help to display the manual of the software"); return; } - CanonicalSimplexProgram program = new SimplexLEXICO(A, b, c, maximization, equality); + final CanonicalSimplexProgram program = new SimplexLEXICO(A, b, c, maximization, equality); if (verbose) { program.displayProblem(); } if(program.solvePrimalSimplex()) { - double[] solution = program.solution; + final double[] solution = program.solution; if (verbose) { System.out.print("Computed solution = ["); @@ -897,7 +912,7 @@ public abstract class CanonicalSimplexProgram out.write("\nscore\n"); out.write(Double.toString(program.tableau0.scoreValue)); out.close(); - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); return; } @@ -911,7 +926,7 @@ public abstract class CanonicalSimplexProgram } } - public static void runExampleScenario(int scenario) + public static void runExampleScenario(final int scenario) { if (scenario < 0 || scenario > 3) { @@ -924,7 +939,7 @@ public abstract class CanonicalSimplexProgram double[] c = null; boolean[] equality = null; double[][] A = null; - boolean verbose = true; + final boolean verbose = true; switch (scenario) { case 0: { @@ -987,7 +1002,7 @@ public abstract class CanonicalSimplexProgram break; } } - CanonicalSimplexProgram program = new SimplexLEXICO(A, b, c, maximization, equality); + final CanonicalSimplexProgram program = new SimplexLEXICO(A, b, c, maximization, equality); if (verbose) { System.out.println(); @@ -995,7 +1010,7 @@ public abstract class CanonicalSimplexProgram } if(program.solvePrimalSimplex()) { - double[] solution = program.solution; + final double[] solution = program.solution; if (verbose) { System.out.println(); diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexBLAND.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexBLAND.java index 74228c55ec4554121d467b06620f6d5ee7471c20..706a0d531e43b98d76f9ee70ab20c9df91cc316c 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexBLAND.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexBLAND.java @@ -8,30 +8,33 @@ package plugins.nchenouard.linearprogrammingfullsimplex; * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class SimplexBLAND extends CanonicalSimplexProgram { /** * Constructor specifying parameters of the linear programming problem. - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ - public SimplexBLAND(double[][] A, double[] b, double[] c, - boolean maximization, boolean[] equality) { + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ + public SimplexBLAND(final double[][] A, final double[] b, final double[] c, + final boolean maximization, final boolean[] equality) { super(A, b, c, maximization, equality); } /** * Constructor specifying parameters of the linear programming problem. * @param p Parameters of the linear programming problem. */ - public SimplexBLAND(CanonicalProgramParameters p) { + public SimplexBLAND(final CanonicalProgramParameters p) { super(p); } /** @@ -41,13 +44,13 @@ public class SimplexBLAND extends CanonicalSimplexProgram * @return the row index in the tableau * */ @Override - protected int getRowidx(TableauWithSlackVariables tableau, int colIdx) { + protected int getRowidx(final TableauWithSlackVariables tableau, final int colIdx) { boolean found = false; double maxVal = -1; int rowIdx = -1; for (int i = 0; i < tableau.xCol.length; i++) { - double val = tableau.tableau[i][colIdx]; + final double val = tableau.tableau[i][colIdx]; if (val > tol ) { if (!found) @@ -75,7 +78,7 @@ public class SimplexBLAND extends CanonicalSimplexProgram * @return the column index in the tableau * */ @Override - protected int getPivotColumn(TableauWithSlackVariables tableau, boolean max) { + protected int getPivotColumn(final TableauWithSlackVariables tableau, final boolean max) { if (max) { int idx = -1; @@ -99,17 +102,25 @@ public class SimplexBLAND extends CanonicalSimplexProgram return idx; } } + /** * Duplicate the program - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ @Override - protected CanonicalSimplexProgram createNewProgram(double[][] A, - double[] b, double[] c, boolean maximization, boolean[] equality) { + protected CanonicalSimplexProgram createNewProgram(final double[][] A, + final double[] b, final double[] c, final boolean maximization, final boolean[] equality) { return new SimplexBLAND(A, b, c, maximization, equality); } } \ No newline at end of file diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexLEXICO.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexLEXICO.java index 76d5bb267be1b9fe7256b788c1f904f0ca572a49..690331f06554ab781991b24688b7322c5c72627e 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexLEXICO.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexLEXICO.java @@ -8,30 +8,33 @@ import java.util.ArrayList; * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class SimplexLEXICO extends CanonicalSimplexProgram { /** * Constructor specifying parameters of the linear programming problem. - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ - public SimplexLEXICO(double[][] A, double[] b, double[] c, - boolean maximization, boolean[] equality) { + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ + public SimplexLEXICO(final double[][] A, final double[] b, final double[] c, + final boolean maximization, final boolean[] equality) { super(A, b, c, maximization, equality); } /** * Constructor specifying parameters of the linear programming problem. * @param p Parameters of the linear programming problem. */ - public SimplexLEXICO(CanonicalProgramParameters p) { + public SimplexLEXICO(final CanonicalProgramParameters p) { super(p); } @@ -42,7 +45,7 @@ public class SimplexLEXICO extends CanonicalSimplexProgram * @return the column index in the tableau * */ @Override - protected int getPivotColumn(TableauWithSlackVariables tableau, boolean max) { + protected int getPivotColumn(final TableauWithSlackVariables tableau, final boolean max) { // max score choice strategy if (max) { @@ -86,22 +89,22 @@ public class SimplexLEXICO extends CanonicalSimplexProgram * @return the row index in the tableau * */ @Override - protected int getRowidx(TableauWithSlackVariables tableau, int colIdx) + protected int getRowidx(final TableauWithSlackVariables tableau, final int colIdx) { int rowIdx = -1; int numBasicRow = 0; - ArrayList<Integer> basicCols = new ArrayList<Integer>(); + final ArrayList<Integer> basicCols = new ArrayList<Integer>(); for (int i = 0; i < tableau.originalColOrder.length; i++) if (tableau.originalColOrder[i] < 0) { basicCols.add(i); numBasicRow ++; } - double[] minLexico = new double[numBasicRow + 1]; + final double[] minLexico = new double[numBasicRow + 1]; boolean found = false; for (int i = 0; i < tableau.xCol.length; i++) { - double val = tableau.tableau[i][colIdx]; + final double val = tableau.tableau[i][colIdx]; if (val > tol) { int k = 0; @@ -116,9 +119,9 @@ public class SimplexLEXICO extends CanonicalSimplexProgram if (tableau.xCol[i]/val == minLexico[k]) { k++; - for (Integer ii:basicCols) + for (final Integer ii:basicCols) { - double t = tableau.tableau[i][ii]/val; + final double t = tableau.tableau[i][ii]/val; if (t > minLexico[k]) { isMinLexico = false; @@ -153,17 +156,25 @@ public class SimplexLEXICO extends CanonicalSimplexProgram } return rowIdx; } + /** * Duplicate the program - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ @Override - protected CanonicalSimplexProgram createNewProgram(double[][] A, - double[] b, double[] c, boolean maximization, boolean[] equality) { + protected CanonicalSimplexProgram createNewProgram(final double[][] A, + final double[] b, final double[] c, final boolean maximization, final boolean[] equality) { return new SimplexLEXICO(A, b, c, maximization, equality); } } \ No newline at end of file diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexMAX.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexMAX.java index 403fa849c3686ca28e73ef794f8bbb74f07596d4..705ddab138910d7f4802c08a59432769f9030aae 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexMAX.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/SimplexMAX.java @@ -8,30 +8,33 @@ package plugins.nchenouard.linearprogrammingfullsimplex; * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class SimplexMAX extends CanonicalSimplexProgram { /** * Constructor specifying parameters of the linear programming problem. - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ - public SimplexMAX(double[][] A, double[] b, double[] c, - boolean maximization, boolean[] equality) { + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ + public SimplexMAX(final double[][] A, final double[] b, final double[] c, + final boolean maximization, final boolean[] equality) { super(A, b, c, maximization, equality); } /** * Constructor specifying parameters of the linear programming problem. * @param p Parameters of the linear programming problem. */ - public SimplexMAX(CanonicalProgramParameters p) + public SimplexMAX(final CanonicalProgramParameters p) { super(p); } @@ -43,7 +46,7 @@ public class SimplexMAX extends CanonicalSimplexProgram * @return the column index in the tableau * */ @Override - protected int getPivotColumn(TableauWithSlackVariables tableau, boolean max) + protected int getPivotColumn(final TableauWithSlackVariables tableau, final boolean max) { if (max) { @@ -83,14 +86,14 @@ public class SimplexMAX extends CanonicalSimplexProgram * @return the row index in the tableau * */ @Override - protected int getRowidx(TableauWithSlackVariables tableau, int colIdx) + protected int getRowidx(final TableauWithSlackVariables tableau, final int colIdx) { boolean found = false; double maxVal = -1; int rowIdx = -1; for (int i = 0; i < tableau.xCol.length; i++) { - double val = tableau.tableau[i][colIdx]; + final double val = tableau.tableau[i][colIdx]; if (val > tol) { if (!found) @@ -111,17 +114,25 @@ public class SimplexMAX extends CanonicalSimplexProgram } return rowIdx; } + /** * Duplicate the program - * @param A constraint matrix - * @param b constraint values - * @param c linear objective function weights - * @param maximization maximization problem if true, minimization else. - * @param equality indicates whether each constraint is an equality. Else it is <=. - * */ + * + * @param A + * constraint matrix + * @param b + * constraint values + * @param c + * linear objective function weights + * @param maximization + * maximization problem if true, minimization else. + * @param equality + * indicates whether each constraint is an equality. Else it is + * <=. + */ @Override - protected CanonicalSimplexProgram createNewProgram(double[][] A, - double[] b, double[] c, boolean maximization, boolean[] equality) { + protected CanonicalSimplexProgram createNewProgram(final double[][] A, + final double[] b, final double[] c, final boolean maximization, final boolean[] equality) { return new SimplexMAX(A, b, c, maximization, equality); } } \ No newline at end of file diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauMinSlackObjective.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauMinSlackObjective.java index 1529bc281af79379e764b4515a059e717f3c23a5..229fc310425747f79541fbb6dfec99f370a43f03 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauMinSlackObjective.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauMinSlackObjective.java @@ -7,28 +7,26 @@ package plugins.nchenouard.linearprogrammingfullsimplex; * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class TableauMinSlackObjective extends TableauWithSlackVariables { /** - * Create the tableau based on a linear program. - * The scores in the tableau will not be initialized to their standard value but set to handle equality constraints. + * Create the tableau based on a linear program. The scores in the tableau + * will not be initialized to their standard value but set to handle + * equality constraints. * - * @param parameters parameters defining the linear optimization problem. + * @param simplexProgram + * parameters defining the linear optimization problem. */ - public TableauMinSlackObjective(CanonicalSimplexProgram simplexProgram) { + public TableauMinSlackObjective(final CanonicalSimplexProgram simplexProgram) { super(simplexProgram.parameters.A); // now make the slacks feasible xCol = simplexProgram.parameters.b.clone(); // equal to b since we start with unit vectors as initial left vectors // build the score row scoreRow = new double[numCol]; // add a score for slacks corresponding to equality constraints - double[] slackScores = new double[simplexProgram.parameters.equalityConstraints.length]; + final double[] slackScores = new double[simplexProgram.parameters.equalityConstraints.length]; this.scoreValue = 0; for (int i = 0; i < slackScores.length; i++) if (simplexProgram.parameters.equalityConstraints[i]) @@ -59,9 +57,9 @@ public class TableauMinSlackObjective extends TableauWithSlackVariables * @param c standard score for variables. * @param slackScore score values for slack variables. */ - public void initScores(double[] c, double[] slackScore) + public void initScores(final double[] c, final double[] slackScore) { - double[] scoreValues = new double[numCol]; + final double[] scoreValues = new double[numCol]; for (int i = 0; i < slackScore.length; i++) scoreValues[i] = slackScore[i]; for (int i = 0; i < numCol; i++) diff --git a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauWithSlackVariables.java b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauWithSlackVariables.java index 6fb4246099e744ac9df59a9a3913abb511e943a8..bb5799272aef235bc45b337a000f0325bf79d295 100644 --- a/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauWithSlackVariables.java +++ b/src/main/java/plugins/nchenouard/linearprogrammingfullsimplex/TableauWithSlackVariables.java @@ -8,10 +8,6 @@ package plugins.nchenouard.linearprogrammingfullsimplex; * Part of the Linear Programming plugin for ICY http://icy.bioimageanalysis.org * * @author Nicolas Chenouard (nicolas.chenouard.dev@gmail.com) - * @version 1.0 - * @date 2014-04-21 - * @license gpl v3.0 - * */ public class TableauWithSlackVariables { /** @@ -65,7 +61,7 @@ public class TableauWithSlackVariables { * * @param A table of linear combinations of the variable, each defining a constraint. */ - public TableauWithSlackVariables(double[][] A) + public TableauWithSlackVariables(final double[][] A) { createVectors(A); } @@ -75,7 +71,7 @@ public class TableauWithSlackVariables { * * @param parameters parameters defining the linear optimization problem. */ - public TableauWithSlackVariables(CanonicalProgramParameters parameters) + public TableauWithSlackVariables(final CanonicalProgramParameters parameters) { createVectors(parameters.A); // constraint value column xCol = parameters.b.clone(); // equal to b since we start with unit vectors as initial left vectors @@ -88,9 +84,9 @@ public class TableauWithSlackVariables { * * @param changeSlack indicate whether the score of each slack variable should be changed */ - protected void setUnitScoreToSlackVars(boolean[] changeSlack) + protected void setUnitScoreToSlackVars(final boolean[] changeSlack) { - double[] scoreValues = new double[numCol]; + final double[] scoreValues = new double[numCol]; // recompute scoreValues based on c int k = 0; for (int i = 0; i < originalColOrder.length; i++) @@ -134,9 +130,9 @@ public class TableauWithSlackVariables { * Create column and basis vectors based on the constraint matrix * @param A Constraint matrix. Each element A[i] defines a linear combination of the variable corresponding to a constraint. * */ - protected void createVectors(double[][] A) + protected void createVectors(final double[][] A) { - int numConstraints = A.length; + final int numConstraints = A.length; numVariables = A[0].length; numCol = numConstraints + numVariables; numRows = numConstraints; @@ -173,7 +169,7 @@ public class TableauWithSlackVariables { System.out.println("Column vectors"); for (int i = 0; i < columnVectors.length; i++) { - double[] vector = columnVectors[i]; + final double[] vector = columnVectors[i]; System.out.print("a"+i+" = ["); for (int j = 0; j < vector.length; j++) System.out.print(vector[j]+", "); @@ -182,7 +178,7 @@ public class TableauWithSlackVariables { System.out.println("Basis vectors"); for (int i = 0; i < leftVectorsIdx.length; i++) { - double[] vector = columnVectors[leftVectorsIdx[i]]; + final double[] vector = columnVectors[leftVectorsIdx[i]]; System.out.print("v"+i+" = ["); for (int j = 0; j < vector.length; j++) System.out.print(vector[j]+", "); @@ -216,16 +212,16 @@ public class TableauWithSlackVariables { * @param colIdx index of the pivoting column in the tableau * @param rowIdx index of the pivoting row in the tableau */ - public void pivot(int colIdx, int rowIdx) + public void pivot(final int colIdx, final int rowIdx) { leftVectorsIdx[rowIdx] = colIdx; - double pivotVal = tableau[rowIdx][colIdx]; - double[] pivotRow = tableau[rowIdx]; - double[] pivotCol = new double[numRows]; + final double pivotVal = tableau[rowIdx][colIdx]; + final double[] pivotRow = tableau[rowIdx]; + final double[] pivotCol = new double[numRows]; for (int i = 0; i < numRows; i++) pivotCol[i] = tableau[i][colIdx]; - double scorePivot = scoreRow[colIdx]; + final double scorePivot = scoreRow[colIdx]; // update the score row for (int j = 0; j < colIdx; j++) @@ -271,10 +267,10 @@ public class TableauWithSlackVariables { * */ public double[] getSolution() { - double[] solution = new double[numVariables]; + final double[] solution = new double[numVariables]; for (int i = 0; i < leftVectorsIdx.length; i++) { - int originalIdx = originalColOrder[leftVectorsIdx[i]]; + final int originalIdx = originalColOrder[leftVectorsIdx[i]]; if (originalIdx >= 0) solution[originalIdx] = xCol[i]; @@ -284,9 +280,9 @@ public class TableauWithSlackVariables { /** * Modify scores in the tableau according to a set of scores for the constraints * */ - public void modifyScores(double[] c) + public void modifyScores(final double[] c) { - double[] scoreValues = new double[numCol]; + final double[] scoreValues = new double[numCol]; for (int i = 0; i < numCol; i++) { if (originalColOrder[i] >= 0) @@ -322,9 +318,9 @@ public class TableauWithSlackVariables { * Modify scores in the tableau according to a set of scores for the constraints * Same as modifyScoresBis. Just a backup function. Still working on it. * */ - public void modifyScoresBis(double[] c) + public void modifyScoresBis(final double[] c) { - double[] scoreValues = new double[numCol]; + final double[] scoreValues = new double[numCol]; for (int i = 0; i < numCol; i++) { if (originalColOrder[i] >= 0)