Search in sources :

Example 1 with NonNegativeConstraint

use of org.apache.commons.math3.optim.linear.NonNegativeConstraint in project neqsim by equinor.

the class LinearProgrammingChemicalEquilibrium method generateInitialEstimates.

// Method added by Neeraj
/*
     * public double[] generateInitialEstimates(SystemInterface system, double[] bVector, double
     * inertMoles, int phase){ int i,j; double[] n = new double[components.length]; Matrix atemp,
     * btemp; Matrix mutemp = new
     * Matrix(chemRefPot,1).times(1.0/(R*system.getPhase(phase).getTemperature())). copy(); Matrix
     * ntemp; atemp = new Matrix(7,7); btemp = new Matrix(1,7); //for (i=0;i<4;i++) for
     * (i=0;i<5;i++) { for (j=0;j<7;j++) atemp.set(i,j,Amatrix[i][j]); btemp.set(0,i,bVector[i]);
     * 
     * } atemp.set(5,4,1); atemp.set(6,5,1); //atemp.set(4,4,1); //atemp.set(5,5,1);
     * //atemp.set(6,1,1); //atemp.print(5,1); //btemp.print(5,5); //mutemp.print(5,5); ntemp =
     * atemp.solve(btemp.transpose()); ntemp.print(5,5); for (i=0;i<7;i++) n[i] = ntemp.get(i,0);
     * int rank = atemp.rank(); return n;
     * 
     * 
     * }
     */
// Method updated to use Apache Commons Math 3 by Marlene 07.12.18
/**
 * <p>
 * generateInitialEstimates.
 * </p>
 *
 * @param system a {@link neqsim.thermo.system.SystemInterface} object
 * @param bVector an array of {@link double} objects
 * @param inertMoles a double
 * @param phase a int
 * @return an array of {@link double} objects
 */
public double[] generateInitialEstimates(SystemInterface system, double[] bVector, double inertMoles, int phase) {
    int i, j;
    double rhs = 0.0;
    Matrix mutemp = new Matrix(chemRefPot, 1).times(1.0 / (R * system.getPhase(phase).getTemperature())).copy();
    double[] v = new double[components.length + 1];
    for (i = 0; i < components.length; i++) {
        v[i + 1] = mutemp.get(0, i);
    }
    LinearObjectiveFunction f = new LinearObjectiveFunction(v, 0.0);
    List<LinearConstraint> cons = new ArrayList<LinearConstraint>();
    for (j = 0; j < bVector.length; j++) {
        for (i = 0; i < components.length; i++) {
            v[i + 1] = Amatrix[j][i];
        }
        rhs = bVector[j];
        cons.add(new LinearConstraint(v, Relationship.EQ, rhs));
    }
    NonNegativeConstraint nonneg = new NonNegativeConstraint(true);
    LinearConstraintSet consSet = new LinearConstraintSet(cons);
    SimplexSolver solver = new SimplexSolver();
    PointValuePair optimal = null;
    try {
        optimal = solver.optimize(new MaxIter(1000), f, consSet, GoalType.MINIMIZE, nonneg);
    } catch (NoFeasibleSolutionException exp) {
        System.out.println("no feasible solution");
        return null;
    } catch (Exception exp) {
        System.out.println("linear optimization failed");
        return null;
    }
    int compNumb = system.getPhase(phase).getNumberOfComponents();
    double[] lp_solution = new double[compNumb];
    double[] temp = optimal.getPoint();
    for (i = 0; i < compNumb - (compNumb - components.length); i++) {
        lp_solution[i] = temp[i + 1];
    }
    return lp_solution;
}
Also used : NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) NoFeasibleSolutionException(org.apache.commons.math3.optim.linear.NoFeasibleSolutionException) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) NoFeasibleSolutionException(org.apache.commons.math3.optim.linear.NoFeasibleSolutionException) PointValuePair(org.apache.commons.math3.optim.PointValuePair) Matrix(Jama.Matrix) SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) MaxIter(org.apache.commons.math3.optim.MaxIter)

Example 2 with NonNegativeConstraint

use of org.apache.commons.math3.optim.linear.NonNegativeConstraint in project java by gunnarfloetteroed.

the class TestApacheLP method main.

public static void main(String[] args) {
    Random rnd = new Random();
    int decVarCnt = 10 * 1000;
    int constrCnt = 1000;
    double constrDensity = 0.01;
    System.out.println("STARTED ...");
    for (int _M = 1000; _M >= 1; _M--) {
        double[] objFctCoeffs = new double[decVarCnt];
        double[][] constrCoeffs = new double[constrCnt][decVarCnt];
        for (int i = 0; i < decVarCnt; i++) {
            objFctCoeffs[i] = -Math.log(Math.max(1e-8, rnd.nextDouble()));
        }
        for (int j = 0; j < constrCnt; j++) {
            // if (j % 1000 == 0) {System.out.println(j);};
            double[] array = constrCoeffs[j];
            for (int i = 0; i < decVarCnt; i++) {
                if (rnd.nextDouble() < constrDensity) {
                    array[i] = 1.0;
                }
            }
        }
        final LinearObjectiveFunction objFct = new LinearObjectiveFunction(objFctCoeffs, 0.0);
        final List<LinearConstraint> constraints = new ArrayList<LinearConstraint>(decVarCnt + constrCnt);
        for (int i = 0; i < decVarCnt; i++) {
            double[] lhs = new double[decVarCnt];
            lhs[i] = 1.0;
            constraints.add(new LinearConstraint(lhs, Relationship.LEQ, 1.0));
        }
        for (int j = 0; j < constrCnt; j++) {
            final LinearConstraint constr = new LinearConstraint(constrCoeffs[j], Relationship.LEQ, _M);
            constraints.add(constr);
        }
        final double[] result = (new SimplexSolver()).optimize(objFct, new LinearConstraintSet(constraints), new NonNegativeConstraint(true), GoalType.MAXIMIZE).getPoint();
        double lambdaRealized = Arrays.stream(result).average().getAsDouble();
        System.out.println(_M + "\t" + lambdaRealized);
    }
    System.out.println("... DONE");
}
Also used : Random(java.util.Random) SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint)

Example 3 with NonNegativeConstraint

use of org.apache.commons.math3.optim.linear.NonNegativeConstraint in project java by gunnarfloetteroed.

the class LeBudgeteur method solve.

public void solve(double[] salaries, int firstYear) {
    final int dim = (2 * _P() + 1) * _Y();
    final double _M = 1000;
    prnLabels();
    final LinearObjectiveFunction objFct;
    {
        final double[] coeffs = new double[dim];
        for (int y = 0; y < this.duration; y++) {
            double timeWeight = Math.exp(-y);
            for (Project proj : this.label2proj.values()) {
                coeffs[x_index(proj, y)] = 0;
                coeffs[z_index(proj, y)] = timeWeight;
            }
            coeffs[d_index(y)] = _M;
        }
        prn("objFctCoeffs", coeffs);
        objFct = new LinearObjectiveFunction(coeffs, 0);
    }
    final List<LinearConstraint> constraints;
    {
        constraints = new ArrayList<LinearConstraint>();
        for (int y = 0; y < this.duration; y++) {
            for (Project proj : this.label2proj.values()) {
                {
                    // keep track of deferred funding
                    double[] coeffs = new double[dim];
                    coeffs[x_index(proj, y)] = 1.0;
                    coeffs[z_index(proj, y)] = 1.0;
                    if (y > 0) {
                        coeffs[z_index(proj, y - 1)] = -1.0;
                    }
                    prn("defer(p=" + proj.getIndex() + ",y=" + y + ")", coeffs);
                    constraints.add(new LinearConstraint(coeffs, Relationship.EQ, proj.getAvailableFunding(y)));
                // constraints.add(new LinearConstraint(coeffs, Relationship.EQ,
                // proj.getTargetFunding(y)));
                }
                {
                    // min-consumptions
                    double[] coeffs = new double[dim];
                    coeffs[x_index(proj, y)] = 1.0;
                    prn("min-cons(p=" + proj.getIndex() + ",y=" + y + ")", coeffs);
                    // constraints.add(new LinearConstraint(coeffs, Relationship.GEQ,
                    // proj.getMinConsumption(y)));
                    constraints.add(new LinearConstraint(coeffs, Relationship.GEQ, proj.getMinOwnConsumption(y)));
                }
                {
                    // max-deferral
                    double[] coeffs = new double[dim];
                    coeffs[z_index(proj, y)] = 1.0;
                    if (this.verbose) {
                        prn("max-defer(p=" + proj.getIndex() + ",y=" + y + ")", coeffs);
                    }
                    constraints.add(new LinearConstraint(coeffs, Relationship.LEQ, proj.getMaxDeferral(y)));
                }
            }
            {
                // cover salary
                double[] coeffs = new double[dim];
                for (Project proj : this.label2proj.values()) {
                    coeffs[x_index(proj, y)] = 1.0;
                }
                coeffs[d_index(y)] = 1.0;
                prn("salary(y=" + y + ")", coeffs);
                constraints.add(new LinearConstraint(coeffs, Relationship.EQ, salaries[y]));
            }
        }
    }
    final double[] result = (new SimplexSolver()).optimize(objFct, new LinearConstraintSet(constraints), new NonNegativeConstraint(true), GoalType.MINIMIZE).getPoint();
    prnLabels();
    prn("solution", result);
    // REPORT
    System.out.println();
    System.out.print("Year\t");
    for (String label : this.label2proj.keySet()) {
        System.out.print(label + "(target)\t");
        System.out.print(label + "(used, other)\t");
        System.out.print(label + "(used, self)\t");
        System.out.print(label + "(deferred)\t");
    }
    System.out.println("salary\tdeficit");
    for (int y = 0; y < this.duration; y++) {
        System.out.print((firstYear + y) + "\t");
        for (Project proj : this.label2proj.values()) {
            roundPrintTab(proj.getTargetFunding(y));
            roundPrintTab(proj.getOtherConsumptions(y));
            roundPrintTab(result[x_index(proj, y)]);
            roundPrintTab(result[z_index(proj, y)]);
        }
        roundPrintTab(salaries[y]);
        roundPrintTab(result[d_index(y)]);
        System.out.println();
    }
}
Also used : SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint)

Aggregations

ArrayList (java.util.ArrayList)3 LinearConstraint (org.apache.commons.math3.optim.linear.LinearConstraint)3 LinearConstraintSet (org.apache.commons.math3.optim.linear.LinearConstraintSet)3 LinearObjectiveFunction (org.apache.commons.math3.optim.linear.LinearObjectiveFunction)3 NonNegativeConstraint (org.apache.commons.math3.optim.linear.NonNegativeConstraint)3 SimplexSolver (org.apache.commons.math3.optim.linear.SimplexSolver)3 Matrix (Jama.Matrix)1 Random (java.util.Random)1 MaxIter (org.apache.commons.math3.optim.MaxIter)1 PointValuePair (org.apache.commons.math3.optim.PointValuePair)1 NoFeasibleSolutionException (org.apache.commons.math3.optim.linear.NoFeasibleSolutionException)1