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

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

Introduction

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

Prototype

public abstract double getEntry(int index) throws OutOfRangeException;

Source Link

Document

Return the entry at the specified index.

Usage

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

/**
 * Problem in the form//from w ww  .j a  v  a 2 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 ww w.  j ava 2s  . c  o  m*/
 * min(c.x) s.t.
 * A.x = b
 * lb <= x <= ub
 * 
 * This problem involves recursive column duplicate reductions.
 * This is a good for testing with a small size problem.
 */
public void testCAbLbUb5() throws Exception {
    log.debug("testCAbLbUb5");

    String problemId = "5";

    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 www .  j a v a2s .co 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 ww . j  av a  2  s  .co 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:monitor.processing.OLD.SoccerField.java

public void drawTree(BaseNode n, RealVector v) {

    Color c = Color.WHITE;

    if (n instanceof TransformNode) {
        v = ((TransformNode) n).getTrasformMatrix().operate(v);

        c = Color.ORANGE;//  w w w.ja  v  a 2s  .  c om
    } else if (n instanceof StaticMesh) {
        StaticMesh sm = (StaticMesh) n;

        if (sm.isTransparent()) {
            c = Color.BLUE;
        } else if (sm.isVisible()) {
            //                System.out.println(n.getInfo());
            //                System.out.println(v);
            c = Color.RED;
            if (sm.getModel().contains("naohead") || sm.getModel().contains("naobody")) {
                c = Color.PINK;
            }
        } else {
            c = Color.CYAN;
        }

    } else if (n instanceof StaticMeshNode) {
        StaticMeshNode smn = (StaticMeshNode) n;

        if (smn.isTransparent()) {
            c = Color.GREEN;
            //                cameraSceneX = (float)v.getEntry(0);
            //                cameraSceneY = (float)v.getEntry(1);
            //                cameraSceneZ = (float)v.getEntry(2);
        } else if (smn.isVisible()) {
            c = Color.YELLOW;
        } else {
            c = Color.MAGENTA;
        }

    }

    pushMatrix();
    fill(100);

    //        c = Color.getHSBColor(n.getAddress()*0.05f, 1, 1);
    stroke(c.getRed(), c.getGreen(), c.getBlue());
    translate((float) (scale * v.getEntry(0)), (float) (-scale * v.getEntry(1)),
            (float) (scale * v.getEntry(2)));
    box(5);
    popMatrix();

    for (int a = n.getChildren().size() - 1; a >= 0; a--) {
        drawTree(((ArrayList<BaseNode>) n.getChildren()).get(a), v);
    }

}

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

/**
 * Problem in the form/*  w  w  w.ja  v a 2 s  .c om*/
 * 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/*from  w  ww.  j av a2s  .  c om*/
 * 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   ww  w  .  j a  v a  2s  .  c om*/
 * 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/*from   w w w .  j a  va2s . 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.JOptimizerTest.java

/**
 * Quadratic objective, no constraints./*from   w  w  w  . j  a  va2s . c o  m*/
 */
public void testNewtownUnconstrained() throws Exception {
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "testNewtownUnconstrained");
    RealMatrix PMatrix = new Array2DRowRealMatrix(
            new double[][] { { 1.68, 0.34, 0.38 }, { 0.34, 3.09, -1.59 }, { 0.38, -1.59, 1.54 } });
    RealVector qVector = new ArrayRealVector(new double[] { 0.018, 0.025, 0.01 });

    // Objective function.
    double theta = 0.01522;
    RealMatrix P = PMatrix.scalarMultiply(theta);
    RealVector q = qVector.mapMultiply(-1);
    PDQuadraticMultivariateRealFunction objectiveFunction = new PDQuadraticMultivariateRealFunction(P.getData(),
            q.toArray(), 0);

    OptimizationRequest or = new OptimizationRequest();
    or.setF0(objectiveFunction);
    or.setInitialPoint(new double[] { 0.04, 0.50, 0.46 });

    //optimization
    JOptimizer opt = new JOptimizer();
    opt.setOptimizationRequest(or);
    int returnCode = opt.optimize();

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

    OptimizationResponse response = opt.getOptimizationResponse();
    double[] sol = response.getSolution();
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "sol   : " + ArrayUtils.toString(sol));
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "value : " + objectiveFunction.value(sol));

    // we already know the analytic solution of the problem
    // sol = -invQ * C
    RealMatrix QInv = Utils.squareMatrixInverse(P);
    RealVector benchSol = QInv.operate(q).mapMultiply(-1);
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "benchSol   : " + ArrayUtils.toString(benchSol.toArray()));
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "benchValue : " + objectiveFunction.value(benchSol.toArray()));

    assertEquals(benchSol.getEntry(0), sol[0], 0.000000000000001);
    assertEquals(benchSol.getEntry(1), sol[1], 0.000000000000001);
    assertEquals(benchSol.getEntry(2), sol[2], 0.000000000000001);
}