use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class KnapsackTest method testVaryingMaxWeight3.
@Test
public void testVaryingMaxWeight3() {
ExpressionsBasedModel model = new KnapsackProblemBuilder(10d).addItem(20, 2).addItem(30, 4).build();
model.maximise();
// Expected: both
this.assertOne(model.getVariables().get(0));
this.assertOne(model.getVariables().get(1));
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class KnapsackTest method testVaryingMaxWeight2.
@Test
public void testVaryingMaxWeight2() {
ExpressionsBasedModel model = new KnapsackProblemBuilder(0d).addItem(20, 2).addItem(30, 4).build();
model.maximise();
// Expected: nothing
this.assertZero(model.getVariables().get(0));
this.assertZero(model.getVariables().get(1));
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class KnapsackTest method testVaryingMaxWeight4.
@Test
public void testVaryingMaxWeight4() {
ExpressionsBasedModel model = new KnapsackProblemBuilder(5d).addItem(20, 2).addItem(30, 4).build();
// model.options.debug(IntegerSolver.class);
model.maximise();
// Expected: just second item
this.assertOne(model.getVariables().get(1));
this.assertZero(model.getVariables().get(0));
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class LpsolveSemiContCase method testSemiContNot.
@Test
@Disabled("Underscored before JUnit 5")
public void testSemiContNot() {
final File tmpFile = new File(PATH + "lpsolve_sc_not.mps");
final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
tmpModel.minimise();
TestUtils.assertTrue(tmpModel.validate());
final BigDecimal tmpExpVal = new BigDecimal("3.93333");
final double tmpActVal = tmpModel.minimise().getValue();
if (!tmpModel.validate(PRECISION)) {
TestUtils.fail(SOLUTION_NOT_VALID);
}
TestUtils.assertEquals(tmpExpVal.doubleValue(), tmpActVal, PRECISION);
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class NewIntegerSolver method setup.
void setup() {
normal = true;
final NodeKey[] retVal = new NodeKey[2];
final ExpressionsBasedModel tmpIntegerModel = NewIntegerSolver.this.getIntegerModel();
final List<Variable> tmpIntegerVariables = tmpIntegerModel.getIntegerVariables();
NodeKey myKey;
myKey = new NodeKey(tmpIntegerModel);
final ExpressionsBasedModel tmpRootModel = NewIntegerSolver.this.makeNodeModel(myKey);
final Result tmpRootResult = tmpRootModel.solve(tmpIntegerModel.getVariableValues());
final double tmpRootValue = tmpRootResult.getValue();
double tmpMinValue = PrimitiveMath.MACHINE_LARGEST;
double tmpMaxValue = -PrimitiveMath.MACHINE_LARGEST;
final double tmpBestValue = tmpRootModel.isMinimisation() ? PrimitiveMath.MACHINE_LARGEST : -PrimitiveMath.MACHINE_LARGEST;
final double[] tmpSignificance = new double[tmpIntegerVariables.size()];
for (int i = 0; i < tmpIntegerVariables.size(); i++) {
final int tmpGlobalIndex = NewIntegerSolver.this.getGlobalIndex(i);
final double tmpVariableValue = tmpRootResult.doubleValue(tmpGlobalIndex);
final NodeKey tmpLowerNodeKey = myKey.createLowerBranch(i, tmpVariableValue, tmpRootValue);
final ExpressionsBasedModel tmpLowerModel = NewIntegerSolver.this.makeNodeModel(tmpLowerNodeKey);
final Result tmpLowerResult = tmpLowerModel.solve(tmpRootResult);
final double tmpLowerValue = tmpLowerResult.getValue();
if (tmpLowerValue < tmpMinValue) {
tmpMinValue = tmpLowerValue;
}
if (tmpLowerValue > tmpMaxValue) {
tmpMaxValue = tmpLowerValue;
}
final NodeKey tmpUpperNodeKey = myKey.createUpperBranch(i, tmpVariableValue, tmpRootValue);
final ExpressionsBasedModel tmpUpperModel = NewIntegerSolver.this.makeNodeModel(tmpUpperNodeKey);
final Result tmpUpperResult = tmpUpperModel.solve(tmpRootResult);
final double tmpUpperValue = tmpUpperResult.getValue();
if (tmpUpperValue < tmpMinValue) {
tmpMinValue = tmpUpperValue;
}
if (tmpUpperValue > tmpMaxValue) {
tmpMaxValue = tmpUpperValue;
}
if (tmpLowerResult.getState().isFeasible() && tmpUpperResult.getState().isFeasible()) {
if (tmpRootModel.isMinimisation() && ((tmpLowerValue < tmpBestValue) || (tmpUpperValue < tmpBestValue))) {
retVal[0] = tmpLowerNodeKey;
retVal[1] = tmpUpperNodeKey;
} else if (tmpRootModel.isMaximisation() && ((tmpLowerValue > tmpBestValue) || (tmpUpperValue > tmpBestValue))) {
retVal[0] = tmpLowerNodeKey;
retVal[1] = tmpUpperNodeKey;
}
}
if (!Double.isNaN(tmpUpperValue) && !Double.isNaN(tmpLowerValue)) {
tmpSignificance[i] = PrimitiveFunction.ABS.invoke(tmpUpperValue - tmpLowerValue);
}
}
double tmpScale = tmpMaxValue - tmpMinValue;
final double value = tmpScale;
if (PrimitiveScalar.isSmall(PrimitiveMath.ONE, value)) {
tmpScale = PrimitiveMath.ONE;
}
for (int i = 0; i < tmpSignificance.length; i++) {
final int index = i;
NewIntegerSolver.this.addIntegerSignificance(NewIntegerSolver.this.getGlobalIndex(index), 0.5 + (tmpSignificance[i] / tmpScale));
}
if ((retVal[0] != null) && (retVal[1] != null)) {
NewIntegerSolver.this.add(retVal[0]);
NewIntegerSolver.this.add(retVal[1]);
} else {
NewIntegerSolver.this.add(new NodeKey(tmpIntegerModel));
}
final Future<Boolean> tmpFuture = DaemonPoolExecutor.invoke(new NodeWorker());
try {
normal = normal && tmpFuture.get();
} catch (InterruptedException | ExecutionException anException) {
normal &= false;
}
}
Aggregations