Search in sources :

Example 6 with LinearConstraint

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

the class LeastAbsoluteDeviations method solve.

public void solve() {
    if (this.xList.isEmpty()) {
        return;
    }
    /*
		 * Decision variables in the linear program are
		 * 
		 * [u(1) ... u(N) b(1) ... b(M)]
		 * 
		 * with u being N=this.size() dummies representing absolute errors per
		 * measurement and b being M=this.xDim() actual regression variables.
		 */
    /*
		 * Create objective function.
		 */
    final LinearObjectiveFunction objFct;
    {
        final double[] objFctCoeffs = new double[this.size() + this.xDim()];
        for (int i = 0; i < this.size(); i++) {
            objFctCoeffs[i] = 1.0;
        }
        objFct = new LinearObjectiveFunction(objFctCoeffs, 0.0);
    }
    /*
		 * Create constraints.
		 */
    final List<LinearConstraint> constraints = new ArrayList<LinearConstraint>(3 * this.size() + (this.lowerBounds == null ? 0 : this.xDim()) + (this.upperBounds == null ? 0 : this.xDim()));
    {
        /*
			 * Three constraints per measurement that couple the dummy decision
			 * variables to regression residuals.
			 */
        for (int i = 0; i < this.size(); i++) {
            final double[] constrCoeffs1 = new double[this.size() + this.xDim()];
            final double[] constrCoeffs2 = new double[this.size() + this.xDim()];
            final double[] constrCoeffs3 = new double[this.size() + this.xDim()];
            constrCoeffs1[i] = 1.0;
            constrCoeffs2[i] = 1.0;
            constrCoeffs3[i] = 1.0;
            for (int j = 0; j < this.xDim(); j++) {
                constrCoeffs1[this.size() + j] = +this.xList.get(i).get(j);
                constrCoeffs2[this.size() + j] = -this.xList.get(i).get(j);
            }
            constraints.add(new LinearConstraint(constrCoeffs1, Relationship.GEQ, +this.yList.get(i) - this.deltaList.get(i)));
            constraints.add(new LinearConstraint(constrCoeffs2, Relationship.GEQ, -this.yList.get(i) - this.deltaList.get(i)));
            constraints.add(new LinearConstraint(constrCoeffs3, Relationship.GEQ, 0.0));
        }
        /*
			 * If applicable, one constraint per lower bound per regression
			 * model coefficient.
			 */
        if (this.lowerBounds != null) {
            for (int i = 0; i < this.xDim(); i++) {
                final double[] constrCoeffs = new double[this.size() + this.xDim()];
                constrCoeffs[this.size() + i] = 1.0;
                constraints.add(new LinearConstraint(constrCoeffs, Relationship.GEQ, this.lowerBounds.get(i)));
            }
        }
        /*
			 * If applicable, one constraint per upper bound per regression
			 * model coefficient.
			 */
        if (this.upperBounds != null) {
            for (int i = 0; i < this.xDim(); i++) {
                final double[] constrCoeffs = new double[this.size() + this.xDim()];
                constrCoeffs[this.size() + i] = 1.0;
                constraints.add(new LinearConstraint(constrCoeffs, Relationship.LEQ, this.upperBounds.get(i)));
            }
        }
    }
    /*
		 * Solve the linear program and take over the result.
		 */
    final PointValuePair result = (new SimplexSolver()).optimize(objFct, new LinearConstraintSet(constraints));
    this.coeffs = new Vector(this.xDim());
    for (int i = 0; i < this.xDim(); i++) {
        this.coeffs.set(i, result.getPoint()[this.size() + i]);
    }
    this.error = result.getValue();
}
Also used : SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) Vector(floetteroed.utilities.math.Vector) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 7 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint in project ipss-plugin by InterPSS-Project.

the class ApacheLPSolver method outputMatrix.

private void outputMatrix(String file) throws Exception {
    RealVector f = objFunc.getCoefficients();
    Array2DRowRealMatrix Aeq = new Array2DRowRealMatrix(numOfBus, numOfVar + numOfGen);
    ArrayRealVector beq = new ArrayRealVector(numOfBus);
    Array2DRowRealMatrix Aiq = new Array2DRowRealMatrix(constraintCollection.size() - numOfBus, numOfVar + numOfGen);
    ArrayRealVector biq = new ArrayRealVector(constraintCollection.size() - numOfBus);
    int eqcnt = 0;
    int iqcnt = 0;
    // this syntax is more easy to read
    for (LinearConstraint con : constraintCollection) {
        RealVector coe = con.getCoefficients();
        if (con.getRelationship().equals(Relationship.EQ)) {
            Aeq.setRowVector(eqcnt, coe);
            beq.setEntry(eqcnt++, con.getValue());
        } else if (con.getRelationship().equals(Relationship.LEQ)) {
            Aiq.setRowVector(iqcnt, coe);
            biq.setEntry(iqcnt++, con.getValue());
        } else {
            OPFLogger.getLogger().severe("Relationship laggerthan needs to be convertted to lessthan." + "for the " + iqcnt + " inequality constraint.");
        }
    }
    writeMatlabInputFile(file, f, Aeq, beq, Aiq, biq);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) SparseRealVector(org.apache.commons.math3.linear.SparseRealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) LinearConstraint(org.apache.commons.math3.optimization.linear.LinearConstraint) OpfConstraint(org.interpss.plugin.opf.constraint.OpfConstraint) LinearConstraint(org.apache.commons.math3.optimization.linear.LinearConstraint)

Example 8 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint in project ipss-plugin by InterPSS-Project.

the class ApacheLPSolver method printHardCodedData.

public void printHardCodedData(String fileName) {
    if (objFunc == null) {
        this.build(cstContainer);
    }
    FileWriter fstream = null;
    try {
        fstream = new FileWriter(fileName);
        BufferedWriter out = new BufferedWriter(fstream);
        RealVector f = objFunc.getCoefficients();
        out.write("LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] ");
        out.append("{ ");
        for (int i = 0; i < f.getDimension(); i++) {
            out.append(Double.toString(f.getEntry(i)));
            out.append(", ");
        }
        out.append("}, ");
        out.append(Double.toString(objFunc.getConstantTerm()));
        out.append(");\n ");
        Iterator<LinearConstraint> it = constraintCollection.iterator();
        out.append("Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); ");
        out.append("\n");
        while (it.hasNext()) {
            out.append("constraints.add(new LinearConstraint(new double[] ");
            out.append("{ ");
            LinearConstraint con = it.next();
            RealVector vec = con.getCoefficients();
            for (int i = 0; i < vec.getDimension(); i++) {
                out.append(Double.toString(vec.getEntry(i)));
                out.append(", ");
            }
            out.append("}, Relationship.");
            String rela = con.getRelationship().toString();
            if (rela.equals("=")) {
                out.append("EQ");
            } else {
                out.append("LEQ");
            }
            out.append(" , ");
            out.append(Double.toString(con.getValue()));
            out.append("));\n ");
        }
        out.close();
        OPFLogger.getLogger().info("Hard-coded data saved to: " + fileName);
    } catch (IOException e) {
        OPFLogger.getLogger().severe(e.toString());
    }
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) SparseRealVector(org.apache.commons.math3.linear.SparseRealVector) FileWriter(java.io.FileWriter) LinearConstraint(org.apache.commons.math3.optimization.linear.LinearConstraint) IOException(java.io.IOException) OpfConstraint(org.interpss.plugin.opf.constraint.OpfConstraint) LinearConstraint(org.apache.commons.math3.optimization.linear.LinearConstraint) BufferedWriter(java.io.BufferedWriter)

Example 9 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint in project vsp-playgrounds by matsim-vsp.

the class LeastAbsoluteDeviations method solve.

public void solve() {
    if (this.xList.isEmpty()) {
        return;
    }
    /*
		 * Decision variables in the linear program are
		 * 
		 * [u(1) ... u(N) b(1) ... b(M)]
		 * 
		 * with u being N=this.size() dummies representing absolute errors per
		 * measurement and b being M=this.xDim() actual regression variables.
		 */
    /*
		 * Create objective function.
		 */
    final LinearObjectiveFunction objFct;
    {
        final double[] objFctCoeffs = new double[this.size() + this.xDim()];
        for (int i = 0; i < this.size(); i++) {
            objFctCoeffs[i] = 1.0;
        }
        objFct = new LinearObjectiveFunction(objFctCoeffs, 0.0);
    }
    /*
		 * Create constraints.
		 */
    final List<LinearConstraint> constraints = new ArrayList<LinearConstraint>(3 * this.size() + (this.lowerBounds == null ? 0 : this.xDim()) + (this.upperBounds == null ? 0 : this.xDim()));
    {
        /*
			 * Three constraints per measurement that couple the dummy decision
			 * variables to regression residuals.
			 */
        for (int i = 0; i < this.size(); i++) {
            final double[] constrCoeffs1 = new double[this.size() + this.xDim()];
            final double[] constrCoeffs2 = new double[this.size() + this.xDim()];
            final double[] constrCoeffs3 = new double[this.size() + this.xDim()];
            constrCoeffs1[i] = 1.0;
            constrCoeffs2[i] = 1.0;
            constrCoeffs3[i] = 1.0;
            for (int j = 0; j < this.xDim(); j++) {
                constrCoeffs1[this.size() + j] = +this.xList.get(i).get(j);
                constrCoeffs2[this.size() + j] = -this.xList.get(i).get(j);
            }
            constraints.add(new LinearConstraint(constrCoeffs1, Relationship.GEQ, +this.yList.get(i) - this.deltaList.get(i)));
            constraints.add(new LinearConstraint(constrCoeffs2, Relationship.GEQ, -this.yList.get(i) - this.deltaList.get(i)));
            constraints.add(new LinearConstraint(constrCoeffs3, Relationship.GEQ, 0.0));
        }
        /*
			 * If applicable, one constraint per lower bound per regression
			 * model coefficient.
			 */
        if (this.lowerBounds != null) {
            for (int i = 0; i < this.xDim(); i++) {
                final double[] constrCoeffs = new double[this.size() + this.xDim()];
                constrCoeffs[this.size() + i] = 1.0;
                constraints.add(new LinearConstraint(constrCoeffs, Relationship.GEQ, this.lowerBounds.get(i)));
            }
        }
        /*
			 * If applicable, one constraint per upper bound per regression
			 * model coefficient.
			 */
        if (this.upperBounds != null) {
            for (int i = 0; i < this.xDim(); i++) {
                final double[] constrCoeffs = new double[this.size() + this.xDim()];
                constrCoeffs[this.size() + i] = 1.0;
                constraints.add(new LinearConstraint(constrCoeffs, Relationship.LEQ, this.upperBounds.get(i)));
            }
        }
    }
    /*
		 * Solve the linear program and take over the result.
		 */
    final PointValuePair result = (new SimplexSolver()).optimize(objFct, new LinearConstraintSet(constraints));
    this.coeffs = new Vector(this.xDim());
    for (int i = 0; i < this.xDim(); i++) {
        this.coeffs.set(i, result.getPoint()[this.size() + i]);
    }
    this.error = result.getValue();
}
Also used : SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) Vector(floetteroed.utilities.math.Vector) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 10 with LinearConstraint

use of org.apache.commons.math3.optim.linear.LinearConstraint in project s1tbx by senbox-org.

the class ArcDataIntegration method solveLinearProgram.

/**
 * Solves the linear program:
 * ```
 *      min {c' x}
 *      s.t.
 *      G x <= h
 * ```
 *
 * @param c optimization objective coefficients.
 * @param G constraint coefficients.
 * @param h constraint value.
 */
public static PointValuePair solveLinearProgram(double[] c, double[][] G, double[] h) {
    // describe the optimization problem
    LinearObjectiveFunction objective = new LinearObjectiveFunction(c, 0);
    int noOfConstraints = G.length;
    Collection<LinearConstraint> constraints = new ArrayList<>();
    for (int i = 0; i < noOfConstraints; i++) {
        constraints.add(new LinearConstraint(G[i], Relationship.LEQ, h[i]));
    }
    // create and run solver
    PointValuePair solution = new SimplexSolver().optimize(objective, new LinearConstraintSet(constraints), GoalType.MINIMIZE);
    return solution;
}
Also used : SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) 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) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

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