use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class NewIntegerSolver method makeNodeModel.
ExpressionsBasedModel makeNodeModel(final NodeKey nodeKey) {
final ExpressionsBasedModel retVal = this.getIntegerModel().relax(false);
final int[] tmpIntegerIndeces = this.getIntegerIndices();
for (int i = 0; i < tmpIntegerIndeces.length; i++) {
final BigDecimal tmpLowerBound = nodeKey.getLowerBound(i);
final BigDecimal tmpUpperBound = nodeKey.getUpperBound(i);
final Variable tmpVariable = retVal.getVariable(tmpIntegerIndeces[i]);
tmpVariable.lower(tmpLowerBound);
tmpVariable.upper(tmpUpperBound);
BigDecimal tmpValue = tmpVariable.getValue();
if (tmpValue != null) {
if (tmpLowerBound != null) {
tmpValue = tmpValue.max(tmpLowerBound);
}
if (tmpUpperBound != null) {
tmpValue = tmpValue.min(tmpUpperBound);
}
tmpVariable.setValue(tmpValue);
}
}
if (this.isIntegerSolutionFound()) {
final double tmpBestValue = this.getBestResultSoFar().getValue();
final double tmpGap = PrimitiveFunction.ABS.invoke(tmpBestValue * options.mip_gap);
if (retVal.isMinimisation()) {
retVal.limitObjective(null, TypeUtils.toBigDecimal(tmpBestValue - tmpGap, options.feasibility));
} else {
retVal.limitObjective(TypeUtils.toBigDecimal(tmpBestValue + tmpGap, options.feasibility), null);
}
}
return retVal;
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class ComPictetPamBamTest method setupModel.
/**
* @param numberOfVars greater than 6
*/
void setupModel(final int numberOfVars) {
if (numberOfVars < 6) {
throw new IllegalArgumentException("numberOfVars must be >= 6 !!!");
}
//
// variables
//
vars = new Variable[numberOfVars];
vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0));
vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0));
vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0));
vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0));
vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0));
vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0));
for (int i = 6; i < numberOfVars; ++i) {
vars[i] = new Variable("x" + i).level(BigMath.ZERO);
}
//
// minimise distance to this point
//
point = new BigDecimal[numberOfVars];
point[0] = new BigDecimal(1.0);
point[1] = new BigDecimal(25.0);
point[2] = new BigDecimal(33.0);
point[3] = new BigDecimal(29.0);
point[4] = new BigDecimal(9.0);
point[5] = new BigDecimal(2.0);
for (int i = 6; i < numberOfVars; ++i) {
point[i] = new BigDecimal(0.0);
}
//
// model
//
model = new ExpressionsBasedModel(vars);
//
// objective function
//
{
final int tmpLength = model.countVariables();
final Expression retVal = model.addExpression("objective");
for (int ij = 0; ij < tmpLength; ij++) {
retVal.set(ij, ij, ONE);
}
final BigDecimal tmpLinearWeight = TWO.negate();
for (int i = 0; i < tmpLength; i++) {
retVal.set(i, Arrays.asList(point).get(i).multiply(tmpLinearWeight));
}
final Expression e = retVal;
e.weight(BigMath.HALF);
}
//
// sum(xi) = 100.0
//
{
final int tmpLength = model.countVariables();
final Expression retVal = model.addExpression("sum(xi) = 100.0");
for (int i = 0; i < tmpLength; i++) {
retVal.set(i, ONE);
}
final Expression e = retVal;
e.level(BigMath.HUNDRED);
}
//
// x1 + x2 <= 45
//
{
final Expression e = model.addExpression("x1 + x2 <= 45.0");
e.set(1, BigMath.ONE);
e.set(2, BigMath.ONE);
e.lower(BigMath.ZERO).upper(new BigDecimal(45.0));
}
//
// x4 - 2*x5 = 0
//
{
final Expression e = model.addExpression("x4 - 2*x5 = 0");
e.set(4, BigMath.ONE);
e.set(5, BigMath.TWO.negate());
e.level(BigMath.ZERO);
}
// model.options.debug(ConvexSolver.class);
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class ConvexProblems method testP20150720.
/**
* Issue reported at GitHub
* <p>
* apete: I believe there are problems with the models the user supplied, but ojAlgo fails to correctly
* identify and report these problems. Instead ojAlgo struggles and returns different solutions with
* sequential executions. This test is designed to (only) ensure consistency between exections. (I don't
* know what the correct solution is.)
* </p>
*
* @see <a href="https://github.com/optimatika/ojAlgo/issues/5">GitHub Issue 5</a>
*/
@Test
public void testP20150720() {
final ExpressionsBasedModel tmpModel1 = P20150720.buildModel1();
final ExpressionsBasedModel tmpModel2 = P20150720.buildModel2();
final ExpressionsBasedModel tmpModel3 = P20150720.buildModel3();
// The problem is with the ConvexSolver, and it is present without integer constraints
tmpModel1.relax(true);
tmpModel2.relax(true);
tmpModel3.relax(true);
final Result tmpBaseResult1 = tmpModel1.maximise();
final Result tmpBaseResult2 = tmpModel2.maximise();
final Result tmpBaseResult3 = tmpModel3.maximise();
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel1, null);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel2, null);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel3, null);
for (int l = 0; l < 10; l++) {
final Result tmpResult1 = tmpModel1.maximise();
if (OptimisationConvexTests.DEBUG) {
BasicLogger.debug();
BasicLogger.debug("Model 1");
BasicLogger.debug(tmpResult1);
BasicLogger.debug(tmpModel1);
}
TestUtils.assertStateNotLessThanFeasible(tmpResult1);
TestUtils.assertEquals("Model 1 State @" + l, tmpBaseResult1.getState(), tmpResult1.getState());
TestUtils.assertEquals("Model 1 Value @" + l, tmpBaseResult1.getValue(), tmpResult1.getValue());
TestUtils.assertEquals("Model 1 Solution @" + l, tmpBaseResult1, tmpResult1);
final Result tmpResult2 = tmpModel2.maximise();
if (OptimisationConvexTests.DEBUG) {
BasicLogger.debug();
BasicLogger.debug("Model 2");
BasicLogger.debug(tmpResult2);
BasicLogger.debug(tmpModel2);
}
TestUtils.assertStateNotLessThanFeasible(tmpResult2);
TestUtils.assertEquals("Model 2 State @" + l, tmpBaseResult2.getState(), tmpResult2.getState());
TestUtils.assertEquals("Model 2 Value @" + l, tmpBaseResult2.getValue(), tmpResult2.getValue());
TestUtils.assertEquals("Model 2 Solution @" + l, tmpBaseResult2, tmpResult2);
final Result tmpResult3 = tmpModel3.maximise();
if (OptimisationConvexTests.DEBUG) {
BasicLogger.debug();
BasicLogger.debug("Model 3");
BasicLogger.debug(tmpResult3);
BasicLogger.debug(tmpModel3);
}
TestUtils.assertStateNotLessThanFeasible(tmpResult3);
TestUtils.assertEquals("Model 3 State @" + l, tmpBaseResult3.getState(), tmpResult3.getState());
TestUtils.assertEquals("Model 3 Value @" + l, tmpBaseResult3.getValue(), tmpResult3.getValue());
TestUtils.assertEquals("Model 3 Solution @" + l, tmpBaseResult3, tmpResult3);
}
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class ConvexProblems method doEarly2008.
static void doEarly2008(final Variable[] variables, final Access2D<?> covariances, final Access1D<?> expected) {
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(variables);
final Expression tmpVariance = tmpModel.addExpression("Variance");
tmpVariance.setQuadraticFactors(tmpModel.getVariables(), covariances);
tmpVariance.weight(BigMath.PI.multiply(BigMath.E).multiply(BigMath.HALF));
final Expression tmpBalance = tmpModel.addExpression("Balance");
tmpBalance.setLinearFactorsSimple(tmpModel.getVariables());
tmpBalance.level(BigMath.ONE);
// tmpModel.options.debug(ConvexSolver.class);
// tmpModel.options.validate = false;
final Result tmpActualResult = tmpModel.minimise();
final NumberContext tmpAccuracy = StandardType.PERCENT.newPrecision(5);
TestUtils.assertTrue(tmpModel.validate(Array1D.BIG.copy(expected), tmpAccuracy));
TestUtils.assertTrue(tmpModel.validate(tmpActualResult, tmpAccuracy));
final TwiceDifferentiable<Double> tmpObjective = tmpModel.objective().toFunction();
final double tmpExpObjFuncVal = tmpObjective.invoke(Access1D.asPrimitive1D(expected));
final double tmpActObjFuncVal = tmpObjective.invoke(Access1D.asPrimitive1D(tmpActualResult));
TestUtils.assertEquals(tmpExpObjFuncVal, tmpActObjFuncVal, tmpAccuracy);
TestUtils.assertEquals(expected, tmpActualResult, tmpAccuracy);
// Test that the LinearSolver can determine feasibility
final ExpressionsBasedModel relaxedModel = tmpModel.relax(false);
final Optimisation.Result tmpLinearResult = relaxedModel.minimise();
TestUtils.assertStateNotLessThanFeasible(tmpLinearResult);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, tmpAccuracy);
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class ConvexProblems method builAndTestModel.
static void builAndTestModel(final PrimitiveDenseStore[] matrices, final PrimitiveDenseStore expectedSolution, final NumberContext modelValidationContext, final boolean testSolverDirectly) {
final MatrixStore<Double> tmpPartQ = expectedSolution.transpose().multiply(matrices[2].multiply(expectedSolution));
final MatrixStore<Double> tmpPartC = matrices[3].transpose().multiply(expectedSolution);
final double tmpExpectedValue = tmpPartQ.multiply(HALF.doubleValue()).subtract(tmpPartC).doubleValue(0);
final Optimisation.Result tmpExpectedResult = new Optimisation.Result(Optimisation.State.OPTIMAL, tmpExpectedValue, expectedSolution);
final ExpressionsBasedModel tmpModel = ConvexProblems.buildModel(matrices, expectedSolution);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, modelValidationContext);
if (DEBUG) {
tmpModel.options.debug(ConvexSolver.class);
tmpModel.options.validate = false;
}
TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(tmpExpectedResult, modelValidationContext));
// The expected solution is written to the variables
TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(modelValidationContext));
// When/if the correct/optimal solution is used to kickStart ojAlgo should return that solution
final Result tmpInitialisedModelResult = tmpModel.minimise();
TestUtils.assertStateNotLessThanOptimal(tmpInitialisedModelResult);
TestUtils.assertEquals(tmpExpectedResult, tmpInitialisedModelResult, modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpInitialisedModelResult.getValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpInitialisedModelResult).doubleValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
for (final Variable tmpVariable : tmpModel.getVariables()) {
tmpVariable.setValue(null);
}
// Initial variable values have been cleared
final Result tmpUninitialisedModelResult = tmpModel.minimise();
TestUtils.assertStateNotLessThanOptimal(tmpUninitialisedModelResult);
TestUtils.assertEquals(tmpExpectedResult, tmpUninitialisedModelResult, modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpUninitialisedModelResult.getValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpUninitialisedModelResult).doubleValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
if (testSolverDirectly) {
final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(matrices);
final ConvexSolver tmpSolver = tmpBuilder.build();
// tmpSolver.options.debug(ConvexSolver.class);
// tmpSolver.options.validate = false;
final Optimisation.Result tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedResult, tmpResult, NumberContext.getGeneral(2, 4));
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpResult).doubleValue(), NumberContext.getGeneral(4, 8));
}
}
Aggregations