Search in sources :

Example 11 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint 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 12 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint 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)8 LinearConstraint (org.apache.commons.math3.optim.linear.LinearConstraint)8 LinearConstraintSet (org.apache.commons.math3.optim.linear.LinearConstraintSet)8 LinearObjectiveFunction (org.apache.commons.math3.optim.linear.LinearObjectiveFunction)7 SimplexSolver (org.apache.commons.math3.optim.linear.SimplexSolver)7 PointValuePair (org.apache.commons.math3.optim.PointValuePair)5 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)4 RealVector (org.apache.commons.math3.linear.RealVector)4 LinearConstraint (org.apache.commons.math3.optimization.linear.LinearConstraint)4 OpfConstraint (org.interpss.plugin.opf.constraint.OpfConstraint)4 SparseRealVector (org.apache.commons.math3.linear.SparseRealVector)3 NonNegativeConstraint (org.apache.commons.math3.optim.linear.NonNegativeConstraint)3 Vector (floetteroed.utilities.math.Vector)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 Matrix (Jama.Matrix)1 DoubleArrayList (cern.colt.list.DoubleArrayList)1 IntArrayList (cern.colt.list.IntArrayList)1 OpfConstraintType (com.interpss.opf.datatype.OpfConstraintType)1