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();
}
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);
}
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());
}
}
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();
}
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;
}
Aggregations