Example usage for org.apache.commons.math3.linear RealVector getNorm

List of usage examples for org.apache.commons.math3.linear RealVector getNorm

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getNorm.

Prototype

public double getNorm() 

Source Link

Document

Returns the L2 norm of the vector.

Usage

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form/*from  w  w  w. j  a v  a 2 s.  c o m*/
 * min(c.x) s.t.
 * A.x = b
 * lb <= x <= ub
 * 
 * This problem involves column duplicate reduction.
 * This is a good for testing with a small size problem.
 */
public void testCAbLbUb6() throws Exception {
    log.debug("testCAbLbUb6");

    String problemId = "6";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    or.setCheckKKTSolutionAccuracy(true);
    //      or.setToleranceKKT(1.e-7);
    //      or.setToleranceFeas(1.E-7);
    //      or.setTolerance(1.E-7);
    or.setDumpProblem(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(lb.length, sol.length);
    assertEquals(ub.length, sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= x.getEntry(i));
    }
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di >= x.getEntry(i));
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    //check value
    assertEquals(expectedvalue, value, or.getTolerance());
}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form//from w w w  .  java2 s.  c  om
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * 
 * This is the same as testCGhAbLbUb2, but lb and ub are into G.
 * The presolved problem has a deterministic solution, that is, all the variables have a fixed value.
 */
public void testCGhAb3() throws Exception {
    log.debug("testCGhAb3");

    String problemId = "3";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];
    double minLb = 0;
    double maxUb = 1.0E15;//it is so high because of the very high values of the elements of h

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setCheckKKTSolutionAccuracy(true);
    //or.setToleranceKKT(1.e-5);
    //      or.setToleranceFeas(5.E-5);
    //      or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    //or.setPresolvingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod(minLb, maxUb);

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    RealVector Gxh = GMatrix.operate(x).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        assertTrue(Gxh.getEntry(i) <= 0);
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    assertEquals(expectedSol.length, sol.length);
    //      for(int i=0; i<sol.length; i++){
    //         assertEquals(expectedSol[0], sol[0], or.getTolerance());
    //      }
    assertEquals(expectedvalue, value, or.getTolerance());

}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form/*from  www  .j  a  v  a 2  s.  c o m*/
 * min(c.x) s.t.
 * A.x = b
 * lb <= x <= ub
 */
public void testCAbLbUb8() throws Exception {
    log.debug("testCAbLbUb8");

    String problemId = "8";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values
    lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND);
    ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND);

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    or.setCheckKKTSolutionAccuracy(true);
    //      or.setToleranceKKT(1.e-7);
    //      or.setToleranceFeas(1.E-7);
    //      or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    or.setRescalingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(lb.length, sol.length);
    assertEquals(ub.length, sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= x.getEntry(i));
    }
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di >= x.getEntry(i));
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    //check value
    assertEquals(expectedvalue, value, or.getTolerance());
}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form//from w  ww  . j  a  v  a2 s  . c  o  m
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * 
 * This is a good for testing with a small size problem.
 * Submitted 01/09/2013 by Chris Myers.
 * 
 * @see JOptimizerTest#testCGhAb1()
 */
public void testCGhAb1() throws Exception {
    log.debug("testCGhAb1");

    String problemId = "1";

    //the original problem: ok until precision 1.E-7
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    //double norm = MatrixUtils.createRealMatrix(A).operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b)).getNorm();
    //assertTrue(norm < 1.e-10);

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setCheckKKTSolutionAccuracy(true);
    or.setToleranceKKT(1.E-7);
    or.setToleranceFeas(1.E-7);
    or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    or.setAlpha(0.75);
    or.setInitialPoint(new double[] { 0.9999998735888544, -999.0000001264111, 1000.0, 0.9999998735888544, 0.0,
            -999.0000001264111, 0.9999999661257591, 0.9999998735888544, 1000.0, 0.0, 0.9999998735888544, 0.0,
            0.9999998735888544, 0.9999998735888544, 0.9999998735888544, 0.0, 0.0, 0.9999998735888544, -1000.0,
            0.9999999198573067, 9.253690467190285E-8, 1000.0, -999.0000001264111, 0.9999998735888544, -1000.0,
            -1000.0 });

    //optimization
    //LPPrimalDualMethodOLD opt = new LPPrimalDualMethodOLD();
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    RealVector Gxh = GMatrix.operate(x).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        assertTrue(Gxh.getEntry(i) <= 0);//not strictly because some constraint has been treated as a bound
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    //check value
    assertEquals(expectedvalue, value, or.getTolerance());

}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form/*from w w w  .j  a v a  2  s . c o  m*/
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 * 
 */
public void testCGhAbLbUb11() throws Exception {
    log.debug("testCGhAbLbUb11");

    String problemId = "10";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values
    lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND);
    ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND);

    //check expected sol
    RealVector expectedX = MatrixUtils.createRealVector(expectedSol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        assertTrue(lb[i] <= expectedX.getEntry(i));
    }
    RealVector Gxh = GMatrix.operate(expectedX).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        assertTrue(Gxh.getEntry(i) <= 0);
    }
    RealVector Axb = AMatrix.operate(expectedX).subtract(bvector);
    assertEquals(0., Axb.getNorm(), 1.E-6);

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    or.setCheckKKTSolutionAccuracy(true);
    or.setDumpProblem(true);
    //or.setPresolvingDisabled(true);
    //or.setRescalingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value         : " + value);
    log.debug("expectedValue : " + expectedValue);

    assertEquals(expectedValue, value, or.getTolerance());
}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form// www .j  a  v a2s  . c  o  m
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 * 
 */
public void testCGhAbLbUb7() throws Exception {
    log.debug("testCGhAbLbUb7");

    String problemId = "7";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values
    lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND);
    ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND);

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    or.setCheckKKTSolutionAccuracy(true);
    //      or.setToleranceKKT(1.e-7);
    //      or.setToleranceFeas(1.E-7);
    //      or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    //or.setPresolvingDisabled(true);
    //or.setRescalingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(lb.length, sol.length);
    assertEquals(ub.length, sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        assertTrue(lb[i] <= x.getEntry(i));
    }
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= x.getEntry(i));
    }
    RealVector Gxh = GMatrix.operate(x).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di >= x.getEntry(i));
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    assertEquals(expectedSol.length, sol.length);
    for (int i = 0; i < sol.length; i++) {
        assertEquals(expectedSol[0], sol[0], or.getTolerance());
    }
    assertEquals(expectedvalue, value, or.getTolerance());

}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form/*w  w  w .j a  v a 2s.c o  m*/
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 * 
 */
public void testCGhAbLbUb10() throws Exception {
    log.debug("testCGhAbLbUb10");

    String problemId = "10";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];

    //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values
    lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND);
    ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND);

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    or.setCheckKKTSolutionAccuracy(true);
    //      or.setToleranceKKT(1.e-7);
    //      or.setToleranceFeas(1.E-7);
    //      or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    //or.setPresolvingDisabled(true);
    //or.setRescalingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod();

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(lb.length, sol.length);
    assertEquals(ub.length, sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        assertTrue(lb[i] <= x.getEntry(i));
    }
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= x.getEntry(i));
    }
    RealVector Gxh = GMatrix.operate(x).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di >= x.getEntry(i));
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    assertEquals(expectedSol.length, sol.length);
    //      for(int i=0; i<sol.length; i++){
    //         assertEquals(expectedSol[0], sol[0], or.getTolerance());
    //      }
    assertEquals(expectedvalue, value, or.getTolerance());

}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java

/**
 * Problem in the form/*from w  ww . ja va  2s .c o  m*/
 * min(c.x) s.t.
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 * 
 * This is the same as testCGhAb3, but lb and ub are outside G.
 * The presolved problem has a deterministic solution, that is, all the variables have a fixed value.
 * Submitted 01/09/2013 by Chris Myers.
 */
public void testCGhAbLbUb2() throws Exception {
    log.debug("testCGhAbLbUb2");

    String problemId = "2";

    log.debug("problemId: " + problemId);
    double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt");
    double expectedvalue = Utils
            .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0];
    double minLb = 0;
    double maxUb = 1.0E15;//it is do high because of the very high values of the elements of h

    LPOptimizationRequest or = new LPOptimizationRequest();
    or.setC(c);
    or.setG(G);
    or.setH(h);
    or.setA(A);
    or.setB(b);
    or.setLb(lb);
    or.setUb(ub);
    //or.setInitialPoint(new double[] {100000.00000000377, 2000000.0000000752, 100000.00000000095, 2000000.0000000189, 100000.00000000095, 2000000.0000000189, 100000.00000000095, 2000000.0000000189, 100000.00000000095, 2000000.0000000189});
    or.setCheckKKTSolutionAccuracy(true);
    //or.setToleranceKKT(1.e-5);
    //or.setToleranceFeas(5.E-5);
    //or.setTolerance(1.E-7);
    or.setDumpProblem(true);
    //or.setPresolvingDisabled(true);

    //optimization
    LPPrimalDualMethod opt = new LPPrimalDualMethod(minLb, maxUb);

    opt.setLPOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        fail();
    }

    LPOptimizationResponse response = opt.getLPOptimizationResponse();
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(c);
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(lb.length, sol.length);
    assertEquals(ub.length, sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);
    RealMatrix GMatrix = MatrixUtils.createRealMatrix(G);
    RealVector hvector = MatrixUtils.createRealVector(h);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bvector = MatrixUtils.createRealVector(b);
    for (int i = 0; i < lb.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= x.getEntry(i));
    }
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di >= x.getEntry(i));
    }
    RealVector Gxh = GMatrix.operate(x).subtract(hvector);
    for (int i = 0; i < Gxh.getDimension(); i++) {
        assertTrue(Gxh.getEntry(i) < 0);
    }
    RealVector Axb = AMatrix.operate(x).subtract(bvector);
    assertEquals(0., Axb.getNorm(), or.getToleranceFeas());

    //      assertEquals( expectedSol.length, sol.length);
    //      for(int i=0; i<sol.length; i++){
    //         assertEquals(expectedSol[0], sol[0], 1.e-7);
    //      }

    log.debug("expectedvalue : " + expectedvalue);
    //assertEquals(expectedvalue, value, or.getTolerance());
    assertTrue(expectedvalue > value);

}

From source file:com.joptimizer.optimizers.LPPresolver.java

/**
 * This method is just for testing scope.
 *//* ww  w.j  a v a 2  s. c o m*/
private void checkProgress(DoubleMatrix1D c, DoubleMatrix2D A, DoubleMatrix1D b, DoubleMatrix1D lb,
        DoubleMatrix1D ub, DoubleMatrix1D ylb, DoubleMatrix1D yub, DoubleMatrix1D zlb, DoubleMatrix1D zub) {

    if (this.expectedSolution == null) {
        return;
    }

    if (Double.isNaN(this.expectedTolerance)) {
        //for this to work properly, this method must be called at least one time before presolving operations start
        RealVector X = MatrixUtils.createRealVector(expectedSolution);
        RealMatrix AMatrix = MatrixUtils.createRealMatrix(A.toArray());
        RealVector Bvector = MatrixUtils.createRealVector(b.toArray());
        RealVector Axb = AMatrix.operate(X).subtract(Bvector);
        double norm = Axb.getNorm();
        this.expectedTolerance = Math.max(1.e-7, 1.5 * norm);
    }

    double tolerance = this.expectedTolerance;
    log.debug("tolerance: " + tolerance);

    RealVector X = MatrixUtils.createRealVector(expectedSolution);
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A.toArray());
    RealVector Bvector = MatrixUtils.createRealVector(b.toArray());
    //logger.debug("A.X-b: " + ArrayUtils.toString(originalA.operate(X).subtract(originalB)));

    //nz rows
    for (int i = 0; i < vRowPositions.length; i++) {
        short[] vRowPositionsI = vRowPositions[i];
        for (short nzJ : vRowPositionsI) {
            if (Double.compare(A.getQuick(i, nzJ), 0.) == 0) {
                log.debug("entry " + i + "," + nzJ + " est zero: " + A.getQuick(i, nzJ));
                throw new IllegalStateException();
            }
        }
    }

    //nz columns
    for (int j = 0; j < vColPositions.length; j++) {
        short[] vColPositionsJ = vColPositions[j];
        for (short nzI : vColPositionsJ) {
            if (Double.compare(A.getQuick(nzI, j), 0.) == 0) {
                log.debug("entry (" + nzI + "," + j + ") est zero: " + A.getQuick(nzI, j));
                throw new IllegalStateException();
            }
        }
    }

    //nz Aij
    for (int i = 0; i < A.rows(); i++) {
        short[] vRowPositionsI = vRowPositions[i];
        for (int j = 0; j < A.columns(); j++) {
            if (Double.compare(Math.abs(A.getQuick(i, j)), 0.) != 0) {
                if (!ArrayUtils.contains(vRowPositionsI, (short) j)) {
                    log.debug("entry " + i + "," + j + " est non-zero: " + A.getQuick(i, j));
                    throw new IllegalStateException();
                }
                if (!ArrayUtils.contains(vColPositions[j], (short) i)) {
                    log.debug("entry " + i + "," + j + " est non-zero: " + A.getQuick(i, j));
                    throw new IllegalStateException();
                }
            }
        }
    }

    //      //boolean deepCheckA = true;
    //      boolean deepCheckA = false;
    //      if(deepCheckA){
    //         //check for 0-rows
    //         List<Integer> zeroRows = new ArrayList<Integer>();
    //         for(int i=0; i<A.rows(); i++){
    //            boolean isNotZero = false;
    //            for(int j=0;!isNotZero && j<A.columns(); j++){
    //               isNotZero = Double.compare(0., A.getQuick(i, j))!=0;
    //            }
    //            if(!isNotZero){
    //               zeroRows.add(zeroRows.size(), i);
    //            }
    //         }
    //         if(!zeroRows.isEmpty()){
    //            log.debug("All 0 entries in rows " + ArrayUtils.toString(zeroRows));
    //            //log.debug(ArrayUtils.toString(A.toArray()));
    //            throw new IllegalStateException();
    //         }
    //         
    //         //check for 0-columns
    //         List<Integer> zeroCols = new ArrayList<Integer>();
    //         for(int j=0; j<A.columns(); j++){
    //            boolean isNotZero = false;
    //            for(int i=0;!isNotZero && i<A.rows(); i++){
    //               isNotZero = Double.compare(0., A.getQuick(i, j))!=0;
    //            }
    //            if(!isNotZero){
    //               zeroCols.add(zeroCols.size(), j);
    //            }
    //         }
    //         if(!zeroCols.isEmpty()){
    //            log.debug("All 0 entries in columns " + ArrayUtils.toString(zeroCols));
    //            //log.debug(ArrayUtils.toString(A.toArray()));
    //            throw new IllegalStateException();
    //         }
    //         
    //         // check rank(A): must be A pXn with rank(A)=p < n
    //         QRSparseFactorization qr = null;
    //         boolean factOK = true;
    //         try{
    //            qr = new QRSparseFactorization((SparseDoubleMatrix2D)A);
    //            qr.factorize();
    //         }catch(Exception e){
    //            factOK = false;
    //            log.warn("Warning", e);
    //         }
    //         if(factOK){
    //            log.debug("p        : " + AMatrix.getRowDimension());
    //            log.debug("n        : " + AMatrix.getColumnDimension());
    //            log.debug("full rank: " + qr.hasFullRank());
    //            if(!(A.rows() < A.columns())){
    //               log.debug("!( p < n )");
    //               throw new IllegalStateException();
    //            }
    //            if(!qr.hasFullRank()){
    //               log.debug("not full rank A matrix");
    //               throw new IllegalStateException();
    //            }
    //         }
    //      }

    //A.x = b
    RealVector Axb = AMatrix.operate(X).subtract(Bvector);
    double norm = Axb.getNorm();
    log.debug("|| A.x-b ||: " + norm);
    if (norm > tolerance) {
        //where is the error?
        for (int i = 0; i < Axb.getDimension(); i++) {
            if (Math.abs(Axb.getEntry(i)) > tolerance) {
                log.debug("entry " + i + ": " + Axb.getEntry(i));
                throw new IllegalStateException();
            }
        }
        throw new IllegalStateException();
    }

    //upper e lower
    for (int i = 0; i < X.getDimension(); i++) {
        if (X.getEntry(i) + tolerance < lb.getQuick(i)) {
            log.debug("lower bound " + i + " not respected: lb=" + lb.getQuick(i) + ", value=" + X.getEntry(i));
            throw new IllegalStateException();
        }
        if (X.getEntry(i) > ub.getQuick(i) + tolerance) {
            log.debug("upper bound " + i + " not respected: ub=" + ub.getQuick(i) + ", value=" + X.getEntry(i));
            throw new IllegalStateException();
        }
    }
}

From source file:com.joptimizer.optimizers.LPPrimalDualMethodNetlibTest.java

private void checkSolution(LPNetlibProblem problem, LPOptimizationRequest or, LPOptimizationResponse response)
        throws Exception {

    double expectedvalue = problem.optimalValue;
    log.debug("expectedvalue : " + expectedvalue);
    double[] sol = response.getSolution();
    RealVector cVector = new ArrayRealVector(or.getC().toArray());
    RealVector solVector = new ArrayRealVector(sol);
    double value = cVector.dotProduct(solVector);
    log.debug("sol   : " + ArrayUtils.toString(sol));
    log.debug("value : " + value);

    //check constraints
    assertEquals(or.getLb().size(), sol.length);
    assertEquals(or.getUb().size(), sol.length);
    RealVector x = MatrixUtils.createRealVector(sol);

    //x >= lb/*ww w. j  a  v a2 s  .  c om*/
    double maxLbmx = -Double.MAX_VALUE;
    for (int i = 0; i < or.getLb().size(); i++) {
        double di = Double.isNaN(or.getLb().getQuick(i)) ? -Double.MAX_VALUE : or.getLb().getQuick(i);
        maxLbmx = Math.max(maxLbmx, di - x.getEntry(i));
        assertTrue(di <= x.getEntry(i) + or.getTolerance());
    }
    log.debug("max(lb - x): " + maxLbmx);

    //x <= ub
    double maxXmub = -Double.MAX_VALUE;
    for (int i = 0; i < or.getUb().size(); i++) {
        double di = Double.isNaN(or.getUb().getQuick(i)) ? Double.MAX_VALUE : or.getUb().getQuick(i);
        maxXmub = Math.max(maxXmub, x.getEntry(i) - di);
        assertTrue(di + or.getTolerance() >= x.getEntry(i));
    }
    log.debug("max(x - ub): " + maxXmub);

    //G.x <h
    if (or.getG() != null && or.getG().rows() > 0) {
        RealMatrix GMatrix = MatrixUtils.createRealMatrix(or.getG().toArray());
        RealVector hvector = MatrixUtils.createRealVector(or.getH().toArray());
        RealVector Gxh = GMatrix.operate(x).subtract(hvector);
        double maxGxh = -Double.MAX_VALUE;
        for (int i = 0; i < Gxh.getDimension(); i++) {
            maxGxh = Math.max(maxGxh, Gxh.getEntry(i));
            assertTrue(Gxh.getEntry(i) - or.getTolerance() <= 0);
        }
        log.debug("max(G.x - h): " + maxGxh);
    }

    //A.x = b
    if (or.getA() != null && or.getA().rows() > 0) {
        RealMatrix AMatrix = MatrixUtils.createRealMatrix(or.getA().toArray());
        RealVector bvector = MatrixUtils.createRealVector(or.getB().toArray());
        RealVector Axb = AMatrix.operate(x).subtract(bvector);
        double norm = Axb.getNorm();
        log.debug("||A.x -b||: " + norm);
        assertEquals(0., norm, or.getToleranceFeas());
    }

    double percDelta = Math.abs((expectedvalue - value) / expectedvalue);
    log.debug("percDelta: " + percDelta);
    //assertEquals(0., percDelta, or.getTolerance());
    //assertEquals(expectedvalue, value, or.getTolerance());
    assertTrue(value < expectedvalue + or.getTolerance());//can even beat other optimizers! the rebel yell...
}