From 4c8487c78779a1f872b4687009f7b61a8f750d5f Mon Sep 17 00:00:00 2001 From: Iurii Drozd Date: Thu, 28 Nov 2013 10:35:35 +0200 Subject: [PATCH 1/4] Implemented optimizer pattern and first in the set: Nonlinear Conjugate Gradient Optimizer (see http://en.wikipedia.org/wiki/Nonlinear_conjugate_gradient_method) --- src/main/java/org/la4j/LinearAlgebra.java | 13 +++++++++++++ .../java/org/la4j/linear/LinearSystemSolver.java | 2 +- src/main/java/org/la4j/matrix/AbstractMatrix.java | 6 ++++++ .../java/org/la4j/matrix/AbstractSafeMatrix.java | 6 ++++++ src/main/java/org/la4j/matrix/Matrix.java | 10 ++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/la4j/LinearAlgebra.java b/src/main/java/org/la4j/LinearAlgebra.java index 8ce4b1d8..235aee68 100644 --- a/src/main/java/org/la4j/LinearAlgebra.java +++ b/src/main/java/org/la4j/LinearAlgebra.java @@ -41,6 +41,8 @@ import org.la4j.linear.JacobiSolver; import org.la4j.linear.LeastSquaresSolver; import org.la4j.linear.LinearSystemSolver; +import org.la4j.optimization.LinearSystemOptimizer; +import org.la4j.optimization.NonlinearConjugateGradientOptimizer; import org.la4j.linear.SeidelSolver; import org.la4j.linear.SquareRootSolver; import org.la4j.linear.SweepSolver; @@ -153,6 +155,17 @@ public LinearSystemSolver create(Matrix matrix) { public abstract LinearSystemSolver create(Matrix matrix); } + + public static enum OptimizerFactory{ + NLCG { + @Override + public LinearSystemOptimizer create(Matrix matrix){ + return new NonlinearConjugateGradientOptimizer(matrix); + } + }; + + public abstract LinearSystemOptimizer create(Matrix matrix); + } /** * References to the Gaussian solver factory. diff --git a/src/main/java/org/la4j/linear/LinearSystemSolver.java b/src/main/java/org/la4j/linear/LinearSystemSolver.java index ba3209f2..49a070e3 100644 --- a/src/main/java/org/la4j/linear/LinearSystemSolver.java +++ b/src/main/java/org/la4j/linear/LinearSystemSolver.java @@ -58,7 +58,7 @@ public interface LinearSystemSolver extends Serializable { Matrix self(); /** - * Returns the number os unknowns in this solver. + * Returns the number of unknowns in this solver. * * @return */ diff --git a/src/main/java/org/la4j/matrix/AbstractMatrix.java b/src/main/java/org/la4j/matrix/AbstractMatrix.java index a44ca5cb..db9b738d 100644 --- a/src/main/java/org/la4j/matrix/AbstractMatrix.java +++ b/src/main/java/org/la4j/matrix/AbstractMatrix.java @@ -41,6 +41,7 @@ import org.la4j.matrix.functor.MatrixFunction; import org.la4j.matrix.functor.MatrixPredicate; import org.la4j.matrix.functor.MatrixProcedure; +import org.la4j.optimization.LinearSystemOptimizer; import org.la4j.vector.Vector; public abstract class AbstractMatrix implements Matrix { @@ -1055,6 +1056,11 @@ public Vector toColumnVector(Factory factory) { @Override public LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory) { return factory.create(this); + } + + @Override + public LinearSystemOptimizer withOptimizer(LinearAlgebra.OptimizerFactory factory){ + return factory.create(this); } @Override diff --git a/src/main/java/org/la4j/matrix/AbstractSafeMatrix.java b/src/main/java/org/la4j/matrix/AbstractSafeMatrix.java index eb5ea095..fc0e53d0 100644 --- a/src/main/java/org/la4j/matrix/AbstractSafeMatrix.java +++ b/src/main/java/org/la4j/matrix/AbstractSafeMatrix.java @@ -37,6 +37,7 @@ import org.la4j.matrix.functor.MatrixFunction; import org.la4j.matrix.functor.MatrixPredicate; import org.la4j.matrix.functor.MatrixProcedure; +import org.la4j.optimization.LinearSystemOptimizer; import org.la4j.vector.Vector; public abstract class AbstractSafeMatrix implements Matrix { @@ -581,6 +582,11 @@ public Vector toColumnVector(Factory factory) { public LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory) { return self.withSolver(factory); } + + @Override + public LinearSystemOptimizer withOptimizer(LinearAlgebra.OptimizerFactory factory){ + return self.withOptimizer(factory); + } @Override public MatrixInverter withInverter(LinearAlgebra.InverterFactory factory) { diff --git a/src/main/java/org/la4j/matrix/Matrix.java b/src/main/java/org/la4j/matrix/Matrix.java index adbf3ae2..d582f18b 100644 --- a/src/main/java/org/la4j/matrix/Matrix.java +++ b/src/main/java/org/la4j/matrix/Matrix.java @@ -31,6 +31,7 @@ import org.la4j.factory.Factory; import org.la4j.inversion.MatrixInverter; import org.la4j.linear.LinearSystemSolver; +import org.la4j.optimization.LinearSystemOptimizer; import org.la4j.matrix.functor.AdvancedMatrixPredicate; import org.la4j.matrix.functor.MatrixAccumulator; import org.la4j.matrix.functor.MatrixFunction; @@ -876,6 +877,15 @@ public interface Matrix extends Externalizable { */ LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory); + /** + * Creates a new optimizer by given {@code factory} of this matrix. + * + * @param factory + * @return + */ + LinearSystemOptimizer withOptimizer(LinearAlgebra.OptimizerFactory factory); + + /** * Creates a new inverter by given {@code factory} of this matrix. * From 97cfbae82d88d21d192a07de525c0de9101b0cd2 Mon Sep 17 00:00:00 2001 From: Iurii Drozd Date: Thu, 28 Nov 2013 10:57:30 +0200 Subject: [PATCH 2/4] Added tests for precise solving and one lost file from previous commit. TODO: Add tests for optimizing. --- src/main/java/org/la4j/LinearAlgebra.java | 1 + .../la4j/optimization/AbstractOptimizer.java | 47 +++++++++++ .../optimization/LinearSystemOptimizer.java | 59 ++++++++++++++ .../NonlinearConjugateGradientOptimizer.java | 81 +++++++++++++++++++ .../optimization/AbstractOptimizerTest.java | 27 +++++++ ...nlinearConjugateGradientOptimizerTest.java | 45 +++++++++++ 6 files changed, 260 insertions(+) create mode 100644 src/main/java/org/la4j/optimization/AbstractOptimizer.java create mode 100644 src/main/java/org/la4j/optimization/LinearSystemOptimizer.java create mode 100644 src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java create mode 100644 src/test/java/org/la4j/optimization/AbstractOptimizerTest.java create mode 100644 src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java diff --git a/src/main/java/org/la4j/LinearAlgebra.java b/src/main/java/org/la4j/LinearAlgebra.java index 235aee68..41da7cc9 100644 --- a/src/main/java/org/la4j/LinearAlgebra.java +++ b/src/main/java/org/la4j/LinearAlgebra.java @@ -43,6 +43,7 @@ import org.la4j.linear.LinearSystemSolver; import org.la4j.optimization.LinearSystemOptimizer; import org.la4j.optimization.NonlinearConjugateGradientOptimizer; +import org.la4j.optimization.NonlinearConjugateGradientOptimizerTest; import org.la4j.linear.SeidelSolver; import org.la4j.linear.SquareRootSolver; import org.la4j.linear.SweepSolver; diff --git a/src/main/java/org/la4j/optimization/AbstractOptimizer.java b/src/main/java/org/la4j/optimization/AbstractOptimizer.java new file mode 100644 index 00000000..fafaf3f6 --- /dev/null +++ b/src/main/java/org/la4j/optimization/AbstractOptimizer.java @@ -0,0 +1,47 @@ +package org.la4j.optimization; + +import org.la4j.matrix.Matrix; +import org.la4j.vector.Vector; + +public abstract class AbstractOptimizer implements LinearSystemOptimizer { + + protected Matrix matrix; + + protected int unknowns; + protected int equations; + + protected AbstractOptimizer(Matrix a) { + if (!applicableTo(a)) { + fail("Given coefficient matrix can not be used with this solver."); + } + + this.matrix = a; + this.unknowns = a.columns(); + this.equations = a.rows(); + } + + @Override + public Vector solve(Vector b) { + return solve(b, b.factory()); + } + + @Override + public Matrix self() { + return matrix; + } + + @Override + public int unknowns() { + return unknowns; + } + + @Override + public int equations() { + return equations; + } + + protected void fail(String message) { + throw new IllegalArgumentException(message); + } + +} diff --git a/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java b/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java new file mode 100644 index 00000000..c23da218 --- /dev/null +++ b/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java @@ -0,0 +1,59 @@ +package org.la4j.optimization; + +import java.io.Serializable; + +import org.la4j.factory.Factory; +import org.la4j.matrix.Matrix; +import org.la4j.vector.Vector; + +/** + * Linear System Optimizator interface; + * This class implements Strategy design pattern; + */ +public interface LinearSystemOptimizer extends Serializable{ + /** + * Optimize the system A*x = b. + * + * @param b + * @return + */ + Vector solve(Vector b); + + /** + * Optimize the system A*x = b. + * + * @param b + * @param factory + * @return + */ + Vector solve(Vector b, Factory factory); + + /** + * Returns the self matrix of the optimizator. + * + * @return + */ + Matrix self(); + + /** + * Returns the number of unknowns in this optimizator. + * + * @return + */ + int unknowns(); + + /** + * Returns the number of equations in this optimizator. + * + * @return + */ + int equations(); + + /** + * Checks whether this optimizator applicable to given {@code matrix} or not. + * + * @param matrix + */ + boolean applicableTo(Matrix matrix); + +} diff --git a/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java b/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java new file mode 100644 index 00000000..9927a076 --- /dev/null +++ b/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java @@ -0,0 +1,81 @@ +package org.la4j.optimization; + +import org.la4j.LinearAlgebra; +import org.la4j.factory.Factory; +import org.la4j.matrix.Matrix; +import org.la4j.vector.Vector; + +public class NonlinearConjugateGradientOptimizer extends AbstractOptimizer implements LinearSystemOptimizer{ + + private static final long serialVersionUID = -6730752031320270935L; + + private Vector start; + private int maxIterations = 10000; + private double eps = 1e-5; + + public NonlinearConjugateGradientOptimizer(Matrix a) { + super(a); + start = LinearAlgebra.BASIC1D_FACTORY.createVector(this.unknowns); + } + + @Override + public Vector solve(Vector b, Factory factory) { + Vector oldX = start.copy(); + Vector oldGradient = formGradient(matrix, start, b).multiply(-1.0); + Vector newX = alphaLineSearch(matrix, b, oldX, oldGradient); + Vector newGradient = formGradient(matrix, start, b).multiply(-1.0); + Vector direction = newGradient.copy(); + + int k = 0; + double beta = 1; + + while ((k++ < maxIterations) && (formGradient(matrix, oldX, b).norm() > eps)){ + newGradient = formGradient(matrix, newX, b).multiply(-1.0); + beta = Math.pow(formGradient(matrix, newX, b).norm(), 2) / Math.pow(formGradient(matrix, oldX, b).norm(), 2); + direction = newGradient.add(direction.multiply(beta)); + oldX = newX.copy(); + newX = alphaLineSearch(matrix, b, oldX, direction); + } + + if (k >= maxIterations){ + System.out.println("Reached " + maxIterations + " iterations." ); + } + + return oldX; + } + + @Override + public boolean applicableTo(Matrix matrix) { + return true; + } + + private double form(Matrix A, Vector x, Vector b){ + return Math.pow(A.multiply(x).add(b.multiply(-1.0)).norm(), 2); + } + + private Vector formGradient(Matrix A, Vector x, Vector b){ + return A.transpose().multiply(A.multiply(x).add(b.multiply(-1.0))).multiply(2); + } + + private Vector alphaLineSearch(Matrix A, Vector b, Vector x, Vector s){ + int k = 0; + + Vector newX = x.copy(); + Vector oldX = x.copy(); + double newF = form(A, x, b); + double oldF = form(A, x, b); + double alpha = eps; + + do{ + k++; + oldX = newX; + newX = oldX.add(s.multiply(alpha)); + oldF = newF; + newF = form(A, newX, b); + + }while((newF < oldF) && (k < maxIterations)); + + return oldX; + + } +} diff --git a/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java new file mode 100644 index 00000000..6b8bf01d --- /dev/null +++ b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java @@ -0,0 +1,27 @@ +package org.la4j.optimization; + +import org.la4j.LinearAlgebra; +import org.la4j.factory.Factory; +import org.la4j.linear.LinearSystemSolver; +import org.la4j.matrix.Matrix; +import org.la4j.vector.MockVector; +import org.la4j.vector.Vector; + +import junit.framework.TestCase; + +public class AbstractOptimizerTest extends TestCase { + public void performTest(LinearAlgebra.OptimizerFactory optimizerFactory, + double coefficientMatrix[][], double rightHandVector[]) { + + for (Factory factory : LinearAlgebra.FACTORIES) { + + Matrix a = factory.createMatrix(coefficientMatrix); + Vector b = factory.createVector(rightHandVector); + + LinearSystemOptimizer solver = a.withOptimizer(optimizerFactory); + Vector x = solver.solve(b, factory); + + assertEquals(new MockVector(b), new MockVector(a.multiply(x))); + } + } +} diff --git a/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java b/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java new file mode 100644 index 00000000..86c0384d --- /dev/null +++ b/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java @@ -0,0 +1,45 @@ +package org.la4j.optimization; + +import org.la4j.LinearAlgebra; + +public class NonlinearConjugateGradientOptimizerTest extends AbstractOptimizerTest { + + //Ensure optimizer solves good linear systems as well. + public void testSolve_1x1() { + + double a[][] = new double[][] { + { 99.0 } + }; + + double b[] = new double[] { -33.0 }; + + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + } + + //Ensure optimizer solves good linear systems as well. + public void testSolve_2x2() { + + double a[][] = new double[][] { + { 5.0, 10.0 }, + { 15.0, -20.0 } + }; + + double b[] = new double[] { 21.0, -37.0 }; + + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + } + + //Ensure optimizer solves good linear systems as well. + public void testSolve_3x3() { + + double a[][] = new double[][] { + { -8.0, 4.0, 2.0 }, + { 10.0, 18.0, -70.0 }, + { 3.0, -54.0, 19.0 } + }; + + double b[] = new double[] { 7.0, -85.5, 0.5 }; + + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + } +} From f19993679932bc45cc2e11e580b4a6cfcd92f643 Mon Sep 17 00:00:00 2001 From: Iurii Drozd Date: Thu, 28 Nov 2013 11:30:27 +0200 Subject: [PATCH 3/4] Added possibility to set accuracy for optimizing. Added test. --- src/main/java/org/la4j/matrix/Matrix.java | 4 ++-- .../la4j/optimization/AbstractOptimizer.java | 10 ++++++++-- .../optimization/LinearSystemOptimizer.java | 14 +++++++++++--- .../NonlinearConjugateGradientOptimizer.java | 12 +++++++----- .../optimization/AbstractOptimizerTest.java | 11 ++++++++--- ...nlinearConjugateGradientOptimizerTest.java | 19 ++++++++++++++++--- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/la4j/matrix/Matrix.java b/src/main/java/org/la4j/matrix/Matrix.java index d582f18b..5edb4a92 100644 --- a/src/main/java/org/la4j/matrix/Matrix.java +++ b/src/main/java/org/la4j/matrix/Matrix.java @@ -878,9 +878,9 @@ public interface Matrix extends Externalizable { LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory); /** - * Creates a new optimizer by given {@code factory} of this matrix. - * + * Creates a new optimizer with given {@code accuracy} by {@code factory} of this matrix * @param factory + * @param accourancy * @return */ LinearSystemOptimizer withOptimizer(LinearAlgebra.OptimizerFactory factory); diff --git a/src/main/java/org/la4j/optimization/AbstractOptimizer.java b/src/main/java/org/la4j/optimization/AbstractOptimizer.java index fafaf3f6..b2fb4cda 100644 --- a/src/main/java/org/la4j/optimization/AbstractOptimizer.java +++ b/src/main/java/org/la4j/optimization/AbstractOptimizer.java @@ -17,12 +17,18 @@ protected AbstractOptimizer(Matrix a) { this.matrix = a; this.unknowns = a.columns(); - this.equations = a.rows(); + this.equations = a.rows(); + } + + + @Override + public Vector solve(Vector b, double accuracy) { + return solve(b, b.factory(), accuracy); } @Override public Vector solve(Vector b) { - return solve(b, b.factory()); + return solve(b, b.factory(), 1e-7); } @Override diff --git a/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java b/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java index c23da218..64f6dcb2 100644 --- a/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java +++ b/src/main/java/org/la4j/optimization/LinearSystemOptimizer.java @@ -12,13 +12,21 @@ */ public interface LinearSystemOptimizer extends Serializable{ /** - * Optimize the system A*x = b. + * Optimize the system A*x = b with default accuracy 1e-7; + * + * @param b + * @return + */ + Vector solve(Vector b, double accuracy); + + /** + * Optimize the system A*x = b with given {@code accuracy} accuracy * * @param b * @return */ Vector solve(Vector b); - + /** * Optimize the system A*x = b. * @@ -26,7 +34,7 @@ public interface LinearSystemOptimizer extends Serializable{ * @param factory * @return */ - Vector solve(Vector b, Factory factory); + Vector solve(Vector b, Factory factory, double accuracy); /** * Returns the self matrix of the optimizator. diff --git a/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java b/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java index 9927a076..742400dd 100644 --- a/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java +++ b/src/main/java/org/la4j/optimization/NonlinearConjugateGradientOptimizer.java @@ -11,15 +11,16 @@ public class NonlinearConjugateGradientOptimizer extends AbstractOptimizer imple private Vector start; private int maxIterations = 10000; - private double eps = 1e-5; + private double step; + public NonlinearConjugateGradientOptimizer(Matrix a) { super(a); start = LinearAlgebra.BASIC1D_FACTORY.createVector(this.unknowns); } @Override - public Vector solve(Vector b, Factory factory) { + public Vector solve(Vector b, Factory factory, double accuracy) { Vector oldX = start.copy(); Vector oldGradient = formGradient(matrix, start, b).multiply(-1.0); Vector newX = alphaLineSearch(matrix, b, oldX, oldGradient); @@ -27,9 +28,10 @@ public Vector solve(Vector b, Factory factory) { Vector direction = newGradient.copy(); int k = 0; - double beta = 1; + double beta = 1; + step = accuracy; - while ((k++ < maxIterations) && (formGradient(matrix, oldX, b).norm() > eps)){ + while ((k++ < maxIterations) && (formGradient(matrix, oldX, b).norm() > accuracy)){ newGradient = formGradient(matrix, newX, b).multiply(-1.0); beta = Math.pow(formGradient(matrix, newX, b).norm(), 2) / Math.pow(formGradient(matrix, oldX, b).norm(), 2); direction = newGradient.add(direction.multiply(beta)); @@ -64,7 +66,7 @@ private Vector alphaLineSearch(Matrix A, Vector b, Vector x, Vector s){ Vector oldX = x.copy(); double newF = form(A, x, b); double oldF = form(A, x, b); - double alpha = eps; + double alpha = step; do{ k++; diff --git a/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java index 6b8bf01d..9eae8477 100644 --- a/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java +++ b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java @@ -10,8 +10,11 @@ import junit.framework.TestCase; public class AbstractOptimizerTest extends TestCase { + + private double absoluteEps = 1e-7; + public void performTest(LinearAlgebra.OptimizerFactory optimizerFactory, - double coefficientMatrix[][], double rightHandVector[]) { + double coefficientMatrix[][], double rightHandVector[], double accuracy) { for (Factory factory : LinearAlgebra.FACTORIES) { @@ -19,9 +22,11 @@ public void performTest(LinearAlgebra.OptimizerFactory optimizerFactory, Vector b = factory.createVector(rightHandVector); LinearSystemOptimizer solver = a.withOptimizer(optimizerFactory); - Vector x = solver.solve(b, factory); + Vector x = solver.solve(b, factory, accuracy); - assertEquals(new MockVector(b), new MockVector(a.multiply(x))); + double eps = (new MockVector(b)).add((new MockVector(a.multiply(x)).multiply(-1.0))).max(); + + assertTrue(Math.abs(eps) <= accuracy); } } } diff --git a/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java b/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java index 86c0384d..334132c7 100644 --- a/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java +++ b/src/test/java/org/la4j/optimization/NonlinearConjugateGradientOptimizerTest.java @@ -4,6 +4,8 @@ public class NonlinearConjugateGradientOptimizerTest extends AbstractOptimizerTest { + private double epsForSolving = 1e-7; + //Ensure optimizer solves good linear systems as well. public void testSolve_1x1() { @@ -13,7 +15,7 @@ public void testSolve_1x1() { double b[] = new double[] { -33.0 }; - performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b, epsForSolving); } //Ensure optimizer solves good linear systems as well. @@ -26,7 +28,7 @@ public void testSolve_2x2() { double b[] = new double[] { 21.0, -37.0 }; - performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b, epsForSolving); } //Ensure optimizer solves good linear systems as well. @@ -40,6 +42,17 @@ public void testSolve_3x3() { double b[] = new double[] { 7.0, -85.5, 0.5 }; - performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b); + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b, epsForSolving); } + + public void testSolve_3x4 () { + double[][] a = new double[][] { + { 1.0, 2.0, 3.0, 4.0 }, + { 4.0, 5.0, 6.0, 7.0 }, + { 7.0, 8.0, 9.0, 10.0 } + }; + double[] b = new double[] { 1.0, 2.0, 3.0 }; + + performTest(LinearAlgebra.OptimizerFactory.NLCG, a, b, 0.001); + } } From 8d6db5f58ca7e79a9489550187f1c9219ce05e6f Mon Sep 17 00:00:00 2001 From: yuronew Date: Thu, 28 Nov 2013 17:23:48 +0200 Subject: [PATCH 4/4] Made AbstractOptimizerTest abstract, because otherwise maven build was failing --- .gitignore | 3 ++- src/main/java/org/la4j/LinearAlgebra.java | 1 - .../org/la4j/optimization/AbstractOptimizerTest.java | 11 ++++------- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 86521443..d7e4e7df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.class *.iml *.classpath -*.project \ No newline at end of file +*.project +/target diff --git a/src/main/java/org/la4j/LinearAlgebra.java b/src/main/java/org/la4j/LinearAlgebra.java index 41da7cc9..235aee68 100644 --- a/src/main/java/org/la4j/LinearAlgebra.java +++ b/src/main/java/org/la4j/LinearAlgebra.java @@ -43,7 +43,6 @@ import org.la4j.linear.LinearSystemSolver; import org.la4j.optimization.LinearSystemOptimizer; import org.la4j.optimization.NonlinearConjugateGradientOptimizer; -import org.la4j.optimization.NonlinearConjugateGradientOptimizerTest; import org.la4j.linear.SeidelSolver; import org.la4j.linear.SquareRootSolver; import org.la4j.linear.SweepSolver; diff --git a/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java index 9eae8477..a2dce99d 100644 --- a/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java +++ b/src/test/java/org/la4j/optimization/AbstractOptimizerTest.java @@ -2,20 +2,17 @@ import org.la4j.LinearAlgebra; import org.la4j.factory.Factory; -import org.la4j.linear.LinearSystemSolver; import org.la4j.matrix.Matrix; import org.la4j.vector.MockVector; import org.la4j.vector.Vector; import junit.framework.TestCase; -public class AbstractOptimizerTest extends TestCase { - - private double absoluteEps = 1e-7; - +public abstract class AbstractOptimizerTest extends TestCase { + public void performTest(LinearAlgebra.OptimizerFactory optimizerFactory, - double coefficientMatrix[][], double rightHandVector[], double accuracy) { - + double coefficientMatrix[][], double rightHandVector[], double accuracy) { + for (Factory factory : LinearAlgebra.FACTORIES) { Matrix a = factory.createMatrix(coefficientMatrix);