use of org.ojalgo.netio.CharacterRing.PrinterBuffer in project ojAlgo by optimatika.
the class NewIntegerSolver method compute.
void compute(final NodeKey nodeKey) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("\nBranch&Bound Node");
NewIntegerSolver.this.log(nodeKey.toString());
NewIntegerSolver.this.log(NewIntegerSolver.this.toString());
}
if (!NewIntegerSolver.this.isIterationAllowed() || !NewIntegerSolver.this.isIterationNecessary()) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Reached iterations or time limit - stop!");
}
normal &= false;
}
if (!NewIntegerSolver.this.isGoodEnoughToContinueBranching(nodeKey.objective)) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("No longer a relevant node!");
}
normal &= true;
}
ExpressionsBasedModel tmpModel = NewIntegerSolver.this.makeNodeModel(nodeKey);
final Optimisation.Result tmpResult = tmpModel.solve(NewIntegerSolver.this.getBestResultSoFar());
NewIntegerSolver.this.incrementIterationsCount();
if ((tmpModel.options.logger_appender != null) && (tmpModel.options.logger_appender instanceof PrinterBuffer)) {
if (NewIntegerSolver.this.getIntegerModel().options.logger_appender != null) {
((PrinterBuffer) tmpModel.options.logger_appender).flush(NewIntegerSolver.this.getIntegerModel().options.logger_appender);
}
}
if (tmpResult.getState().isOptimal()) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Node solved to optimality!");
}
if (NewIntegerSolver.this.options.validate && !tmpModel.validate(tmpResult)) {
// This should not be possible. There is a bug somewhere.
NewIntegerSolver.this.log("Node solution marked as OPTIMAL, but is actually INVALID/INFEASIBLE/FAILED. Stop this branch!");
// IntegerSolver.this.logDebug(myKey.toString());
// IntegerSolver.this.logDebug(tmpModel.toString());
// final GenericSolver tmpDefaultSolver = tmpModel.getDefaultSolver();
// tmpDefaultSolver.solve();
// IntegerSolver.this.logDebug(tmpDefaultSolver.toString());
normal &= false;
}
final int tmpBranchIndex = NewIntegerSolver.this.identifyNonIntegerVariable(tmpResult, nodeKey);
final double tmpSolutionValue = NewIntegerSolver.this.evaluateFunction(tmpResult);
if (tmpBranchIndex == -1) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Integer solution! Store it among the others, and stop this branch!");
}
final Optimisation.Result tmpIntegerSolutionResult = new Optimisation.Result(Optimisation.State.FEASIBLE, tmpSolutionValue, tmpResult);
NewIntegerSolver.this.markInteger(nodeKey, null, tmpIntegerSolutionResult);
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log(NewIntegerSolver.this.getBestResultSoFar().toString());
}
BasicLogger.debug();
BasicLogger.debug(NewIntegerSolver.this.toString());
// BasicLogger.debug(DaemonPoolExecutor.INSTANCE.toString());
} else {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Not an Integer Solution: " + tmpSolutionValue);
}
final double tmpVariableValue = tmpResult.doubleValue(NewIntegerSolver.this.getGlobalIndex(tmpBranchIndex));
if (NewIntegerSolver.this.isGoodEnoughToContinueBranching(tmpSolutionValue)) {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Still hope, branching on {} @ {} >>> {}", tmpBranchIndex, tmpVariableValue, tmpModel.getVariable(NewIntegerSolver.this.getGlobalIndex(tmpBranchIndex)));
}
tmpModel.dispose();
tmpModel = null;
final NodeKey tmpLowerBranchTask = nodeKey.createLowerBranch(tmpBranchIndex, tmpVariableValue, tmpResult.getValue());
final NodeKey tmpUpperBranchTask = nodeKey.createUpperBranch(tmpBranchIndex, tmpVariableValue, tmpResult.getValue());
this.add(tmpLowerBranchTask);
this.add(tmpUpperBranchTask);
if (DaemonPoolExecutor.isDaemonAvailable()) {
DaemonPoolExecutor.invoke(new NodeWorker());
}
normal &= true;
} else {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Can't find better integer solutions - stop this branch!");
}
}
}
} else {
if (NewIntegerSolver.this.isDebug()) {
NewIntegerSolver.this.log("Failed to solve problem - stop this branch!");
}
}
normal &= true;
}
Aggregations