use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class IntegerProblems method testP20130409b.
/**
* Test case sent in by the user / problem reporter
* <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
*/
@Test
public void testP20130409b() {
final Variable x1 = Variable.make("x1");
final Variable x2013 = Variable.make("x2013");
final Variable x2014 = Variable.make("x2014");
final Variable x2015 = Variable.make("x2015");
x2013.setInteger(true);
x2014.setInteger(true);
x2015.setInteger(true);
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
tmpModel.addVariable(x1);
tmpModel.addVariable(x2013);
tmpModel.addVariable(x2014);
tmpModel.addVariable(x2015);
final Expression obj = tmpModel.addExpression("obj");
obj.set(x1, 1);
obj.weight(BigDecimal.valueOf(1));
final Expression c1 = tmpModel.addExpression("c1");
c1.set(x1, 1);
c1.lower(BigDecimal.valueOf(0));
final Expression c2 = tmpModel.addExpression("c2");
c2.set(x2014, -5000);
c2.set(x2013, 5100);
c2.set(x1, -1);
c2.upper(BigDecimal.valueOf(0));
final Expression c3 = tmpModel.addExpression("c3");
c3.set(x2014, -5000);
c3.set(x2013, 5100);
c3.set(x1, 1);
c3.lower(BigDecimal.valueOf(0));
final Expression c4 = tmpModel.addExpression("c4");
c4.set(x2014, 150);
c4.set(x2013, 5100);
c4.set(x2015, -5000);
c4.set(x1, -1);
c4.upper(BigDecimal.valueOf(0));
final Expression c5 = tmpModel.addExpression("c5");
c5.set(x2014, 150);
c5.set(x2013, 5100);
c5.set(x2015, -5000);
c5.set(x1, 1);
c5.lower(BigDecimal.valueOf(0));
final Expression c6 = tmpModel.addExpression("c6");
c6.set(x2015, 5000);
c6.set(x2014, 5000);
c6.set(x2013, 5000);
c6.level(BigDecimal.valueOf(19105000));
final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4849.999999997941), BigDecimal.valueOf(1245), BigDecimal.valueOf(1269), BigDecimal.valueOf(1307) });
TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
// tmpModel.options.debug(IntegerSolver.class);
// tmpModel.options.problem = NumberContext.getGeneral(8);
final Result tmpResult = tmpModel.minimise();
// BasicLogger.debug(tmpResult.toString());
TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class IntegerProblems method testP20140819.
/**
* <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=211">BugZilla-211</a>
*/
@Test
public void testP20140819() {
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
final double[] tmpWeights = new double[] { 2691.5357279536333, 2600.760150603986, 2605.8958795795374, 2606.7208332501104, 2715.0757845953835, 2602.194912040238, 2606.0069468717575, 2609.0385816244316, 2750.0520522057927, 2602.048261785581, 2600.507229973181, 2602.046307869504, 2721.343937605796, 2601.7367414553805, 2600.595318433882, 2599.405979211142 };
for (int v = 0; v < tmpWeights.length; v++) {
tmpModel.addVariable(Variable.make("x" + v).integer(true).lower(0).upper(414).weight(tmpWeights[v]));
}
// 117 <= 30 30 30 30 0 4 0 0 0 4 0 0 0 4 0 0 <= 14868
// 36 <= 0 4 0 0 40 40 40 40 0 0 4 0 0 0 4 0 <= 170569
// 341 <= 0 0 8 0 0 0 8 0 68 68 68 68 0 0 0 5 <= 140833
// 413 <= 0 0 0 8 0 0 0 9 0 0 0 6 59 59 59 59 <= 48321
final int[] tmpLower = new int[] { 117, 36, 341, 413 };
final int[] tmpUpper = new int[] { 14868, 170569, 140833, 48321 };
final int[][] tmpFactors = new int[4][];
tmpFactors[0] = new int[] { 30, 30, 30, 30, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0 };
tmpFactors[1] = new int[] { 0, 4, 0, 0, 40, 40, 40, 40, 0, 0, 4, 0, 0, 0, 4, 0 };
tmpFactors[2] = new int[] { 0, 0, 8, 0, 0, 0, 8, 0, 68, 68, 68, 68, 0, 0, 0, 5 };
tmpFactors[3] = new int[] { 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 6, 59, 59, 59, 59 };
for (int c = 0; c < tmpFactors.length; c++) {
final Expression tmpExpr = tmpModel.addExpression("C" + c);
tmpExpr.lower(tmpLower[c]).upper(tmpUpper[c]);
for (int v = 0; v < tmpFactors[c].length; v++) {
tmpExpr.set(v, tmpFactors[c][v]);
}
}
// tmpModel.options.debug(IntegerSolver.class);
final Result tmpResult = tmpModel.minimise();
if (OptimisationIntegerTests.DEBUG) {
BasicLogger.debug(tmpResult);
BasicLogger.debug(tmpModel);
}
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertTrue(tmpModel.validate(tmpResult));
}
use of org.ojalgo.optimisation.Expression in project cogcomp-nlp by CogComp.
the class OJalgoHook method addGreaterThanConstraint.
public void addGreaterThanConstraint(int[] i, double[] a, double b) {
if (log) {
System.out.print("OJalgoHook: addGreaterThanConstraint(");
for (int idx = 0; idx < i.length; idx++) System.out.print("(i=" + i[idx] + ", a=" + a[idx] + ") ");
System.out.println("b= " + b + ")");
}
numConstraints++;
Expression constraint = model.addExpression("GreaterThanConstraint: " + Integer.toString(numConstraints));
constraint.lower(b);
for (int ind = 0; ind < i.length; ind++) {
constraint.setLinearFactor(i[ind], a[ind]);
// in jdk8:
// constraint.set(i[ind], a[ind]);
}
}
use of org.ojalgo.optimisation.Expression in project cogcomp-nlp by CogComp.
the class OJalgoHook method addEqualityConstraint.
public void addEqualityConstraint(int[] i, double[] a, double b) {
if (log) {
System.out.print("OJalgoHook: addEqualityConstraint(");
for (int idx = 0; idx < i.length; idx++) System.out.print("(i=" + i[idx] + ", a=" + a[idx] + ") ");
System.out.println("b= " + b + ")");
}
numConstraints++;
Expression constraint = model.addExpression("EqualityConstraint: " + Integer.toString(numConstraints));
constraint.level(b);
for (int ind = 0; ind < i.length; ind++) {
constraint.setLinearFactor(i[ind], a[ind]);
// in jdk8:
// constraint.set(i[ind], a[ind])
}
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class ConvexSolver method copy.
public static void copy(final ExpressionsBasedModel sourceModel, final ConvexSolver.Builder destinationBuilder) {
destinationBuilder.reset();
final List<Variable> tmpFreeVariables = sourceModel.getFreeVariables();
final Set<IntIndex> tmpFixedVariables = sourceModel.getFixedVariables();
final int tmpFreeVarDim = tmpFreeVariables.size();
// final Array1D<Double> tmpCurrentSolution = Array1D.PRIMITIVE.makeZero(tmpFreeVarDim);
// for (int i = 0; i < tmpFreeVariables.size(); i++) {
// final BigDecimal tmpValue = tmpFreeVariables.get(i).getValue();
// if (tmpValue != null) {
// tmpCurrentSolution.set(i, tmpValue.doubleValue());
// }
// }
// final Optimisation.Result tmpKickStarter = new Optimisation.Result(Optimisation.State.UNEXPLORED, Double.NaN, tmpCurrentSolution);
// AE & BE
final List<Expression> tmpEqExpr = sourceModel.constraints().filter((final Expression c) -> c.isEqualityConstraint() && !c.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpEqExprDim = tmpEqExpr.size();
if (tmpEqExprDim > 0) {
final SparseStore<Double> tmpAE = SparseStore.PRIMITIVE.make(tmpEqExprDim, tmpFreeVarDim);
final PhysicalStore<Double> tmpBE = FACTORY.makeZero(tmpEqExprDim, 1);
for (int i = 0; i < tmpEqExprDim; i++) {
final Expression tmpExpression = tmpEqExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAE.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBE.set(i, 0, tmpExpression.getAdjustedUpperLimit());
}
destinationBuilder.equalities(tmpAE, tmpBE);
}
// Q & C
final Expression tmpObjExpr = sourceModel.objective().compensate(tmpFixedVariables);
PhysicalStore<Double> tmpQ = null;
if (tmpObjExpr.isAnyQuadraticFactorNonZero()) {
tmpQ = FACTORY.makeZero(tmpFreeVarDim, tmpFreeVarDim);
final BinaryFunction<Double> tmpBaseFunc = sourceModel.isMaximisation() ? SUBTRACT : ADD;
UnaryFunction<Double> tmpModifier;
for (final IntRowColumn tmpKey : tmpObjExpr.getQuadraticKeySet()) {
final int tmpRow = sourceModel.indexOfFreeVariable(tmpKey.row);
final int tmpColumn = sourceModel.indexOfFreeVariable(tmpKey.column);
if ((tmpRow >= 0) && (tmpColumn >= 0)) {
tmpModifier = tmpBaseFunc.second(tmpObjExpr.getAdjustedQuadraticFactor(tmpKey));
tmpQ.modifyOne(tmpRow, tmpColumn, tmpModifier);
tmpQ.modifyOne(tmpColumn, tmpRow, tmpModifier);
}
}
}
PhysicalStore<Double> tmpC = null;
if (tmpObjExpr.isAnyLinearFactorNonZero()) {
tmpC = FACTORY.makeZero(tmpFreeVarDim, 1);
if (sourceModel.isMinimisation()) {
for (final IntIndex tmpKey : tmpObjExpr.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpC.set(tmpIndex, 0, -tmpObjExpr.getAdjustedLinearFactor(tmpKey));
}
}
} else {
for (final IntIndex tmpKey : tmpObjExpr.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpC.set(tmpIndex, 0, tmpObjExpr.getAdjustedLinearFactor(tmpKey));
}
}
}
}
destinationBuilder.objective(tmpQ, tmpC);
// AI & BI
final List<Expression> tmpUpExpr = sourceModel.constraints().filter((final Expression c2) -> c2.isUpperConstraint() && !c2.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpUpExprDim = tmpUpExpr.size();
final List<Variable> tmpUpVar = sourceModel.bounds().filter((final Variable c4) -> c4.isUpperConstraint()).collect(Collectors.toList());
final int tmpUpVarDim = tmpUpVar.size();
final List<Expression> tmpLoExpr = sourceModel.constraints().filter((final Expression c1) -> c1.isLowerConstraint() && !c1.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpLoExprDim = tmpLoExpr.size();
final List<Variable> tmpLoVar = sourceModel.bounds().filter((final Variable c3) -> c3.isLowerConstraint()).collect(Collectors.toList());
final int tmpLoVarDim = tmpLoVar.size();
if ((tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim) > 0) {
final SparseStore<Double> tmpAI = SparseStore.PRIMITIVE.make(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim, tmpFreeVarDim);
final PhysicalStore<Double> tmpBI = FACTORY.makeZero(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim, 1);
if (tmpUpExprDim > 0) {
for (int i = 0; i < tmpUpExprDim; i++) {
final Expression tmpExpression = tmpUpExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAI.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBI.set(i, 0, tmpExpression.getAdjustedUpperLimit());
}
}
if (tmpUpVarDim > 0) {
for (int i = 0; i < tmpUpVarDim; i++) {
final Variable tmpVariable = tmpUpVar.get(i);
tmpAI.set(tmpUpExprDim + i, sourceModel.indexOfFreeVariable(tmpVariable), tmpVariable.getAdjustmentFactor());
tmpBI.set(tmpUpExprDim + i, 0, tmpVariable.getAdjustedUpperLimit());
}
}
if (tmpLoExprDim > 0) {
for (int i = 0; i < tmpLoExprDim; i++) {
final Expression tmpExpression = tmpLoExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAI.set(tmpUpExprDim + tmpUpVarDim + i, tmpIndex, -tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBI.set(tmpUpExprDim + tmpUpVarDim + i, 0, -tmpExpression.getAdjustedLowerLimit());
}
}
if (tmpLoVarDim > 0) {
for (int i = 0; i < tmpLoVarDim; i++) {
final Variable tmpVariable = tmpLoVar.get(i);
tmpAI.set(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + i, sourceModel.indexOfFreeVariable(tmpVariable), -tmpVariable.getAdjustmentFactor());
tmpBI.set(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + i, 0, -tmpVariable.getAdjustedLowerLimit());
}
}
destinationBuilder.inequalities(tmpAI, tmpBI);
}
}
Aggregations