use of org.ojalgo.optimisation.Variable 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);
}
}
use of org.ojalgo.optimisation.Variable 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;
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class SimplexSolver method build.
static SimplexTableau build(final ExpressionsBasedModel model) {
final List<Variable> tmpPosVariables = model.getPositiveVariables();
final List<Variable> tmpNegVariables = model.getNegativeVariables();
final Set<IntIndex> tmpFixVariables = model.getFixedVariables();
final Expression tmpObjFunc = model.objective().compensate(tmpFixVariables);
final List<Expression> tmpExprsEq = model.constraints().filter(c -> c.isEqualityConstraint() && !c.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final List<Expression> tmpExprsLo = model.constraints().filter(c -> c.isLowerConstraint() && !c.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final List<Expression> tmpExprsUp = model.constraints().filter(c -> c.isUpperConstraint() && !c.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final List<Variable> tmpVarsPosLo = model.bounds().filter(v -> v.isPositive() && v.isLowerConstraint() && (v.getLowerLimit().signum() > 0)).collect(Collectors.toList());
final List<Variable> tmpVarsPosUp = model.bounds().filter(v -> v.isPositive() && v.isUpperConstraint() && (v.getUpperLimit().signum() > 0)).collect(Collectors.toList());
final List<Variable> tmpVarsNegLo = model.bounds().filter(v -> v.isNegative() && v.isLowerConstraint() && (v.getLowerLimit().signum() < 0)).collect(Collectors.toList());
final List<Variable> tmpVarsNegUp = model.bounds().filter(v -> v.isNegative() && v.isUpperConstraint() && (v.getUpperLimit().signum() < 0)).collect(Collectors.toList());
final int tmpConstraiCount = tmpExprsEq.size() + tmpExprsLo.size() + tmpExprsUp.size() + tmpVarsPosLo.size() + tmpVarsPosUp.size() + tmpVarsNegLo.size() + tmpVarsNegUp.size();
final int tmpProblVarCount = tmpPosVariables.size() + tmpNegVariables.size();
final int tmpSlackVarCount = tmpExprsLo.size() + tmpExprsUp.size() + tmpVarsPosLo.size() + tmpVarsPosUp.size() + tmpVarsNegLo.size() + tmpVarsNegUp.size();
final SimplexTableau retVal = SimplexTableau.make(tmpConstraiCount, tmpProblVarCount, tmpSlackVarCount);
final int tmpPosVarsBaseIndex = 0;
final int tmpNegVarsBaseIndex = tmpPosVarsBaseIndex + tmpPosVariables.size();
final int tmpSlaVarsBaseIndex = tmpNegVarsBaseIndex + tmpNegVariables.size();
for (final IntIndex tmpKey : tmpObjFunc.getLinearKeySet()) {
final double tmpFactor = model.isMaximisation() ? -tmpObjFunc.getAdjustedLinearFactor(tmpKey) : tmpObjFunc.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.objective().set(tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.objective().set(tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
}
int tmpConstrBaseIndex = 0;
int tmpCurrentSlackVarIndex = tmpSlaVarsBaseIndex;
final int tmpExprsEqLength = tmpExprsEq.size();
for (int c = 0; c < tmpExprsEqLength; c++) {
final Expression tmpExpr = tmpExprsEq.get(c).compensate(tmpFixVariables);
final double tmpRHS = tmpExpr.getAdjustedLowerLimit();
if (tmpRHS < ZERO) {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, -tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, -tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, tmpFactor);
}
}
} else {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
}
}
}
tmpConstrBaseIndex += tmpExprsEqLength;
final int tmpExprsLoLength = tmpExprsLo.size();
for (int c = 0; c < tmpExprsLoLength; c++) {
final Expression tmpExpr = tmpExprsLo.get(c).compensate(tmpFixVariables);
final double tmpRHS = tmpExpr.getAdjustedLowerLimit();
if (tmpRHS < ZERO) {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, -tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, -tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, tmpFactor);
}
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, ONE);
} else {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, NEG);
}
}
tmpConstrBaseIndex += tmpExprsLoLength;
final int tmpExprsUpLength = tmpExprsUp.size();
for (int c = 0; c < tmpExprsUpLength; c++) {
final Expression tmpExpr = tmpExprsUp.get(c).compensate(tmpFixVariables);
final double tmpRHS = tmpExpr.getAdjustedUpperLimit();
if (tmpRHS < ZERO) {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, -tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, -tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, tmpFactor);
}
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, NEG);
} else {
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, tmpRHS);
for (final IntIndex tmpKey : tmpExpr.getLinearKeySet()) {
final double tmpFactor = tmpExpr.getAdjustedLinearFactor(tmpKey);
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey.index);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey.index);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, ONE);
}
}
tmpConstrBaseIndex += tmpExprsUpLength;
final int tmpVarsPosLoLength = tmpVarsPosLo.size();
for (int c = 0; c < tmpVarsPosLoLength; c++) {
final Variable tmpVar = tmpVarsPosLo.get(c);
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, tmpVar.getAdjustedLowerLimit());
final int tmpKey = model.indexOf(tmpVar);
final double tmpFactor = tmpVar.getAdjustmentFactor();
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, NEG);
}
tmpConstrBaseIndex += tmpVarsPosLoLength;
final int tmpVarsPosUpLength = tmpVarsPosUp.size();
for (int c = 0; c < tmpVarsPosUpLength; c++) {
final Variable tmpVar = tmpVarsPosUp.get(c);
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, tmpVar.getAdjustedUpperLimit());
final int tmpKey = model.indexOf(tmpVar);
final double tmpFactor = tmpVar.getAdjustmentFactor();
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, -tmpFactor);
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, ONE);
}
tmpConstrBaseIndex += tmpVarsPosUpLength;
final int tmpVarsNegLoLength = tmpVarsNegLo.size();
for (int c = 0; c < tmpVarsNegLoLength; c++) {
final Variable tmpVar = tmpVarsNegLo.get(c);
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, -tmpVar.getAdjustedLowerLimit());
final int tmpKey = model.indexOf(tmpVar);
final double tmpFactor = tmpVar.getAdjustmentFactor();
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, -tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, tmpFactor);
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, ONE);
}
tmpConstrBaseIndex += tmpVarsNegLoLength;
final int tmpVarsNegUpLength = tmpVarsNegUp.size();
for (int c = 0; c < tmpVarsNegUpLength; c++) {
final Variable tmpVar = tmpVarsNegUp.get(c);
retVal.constraintsRHS().set(tmpConstrBaseIndex + c, -tmpVar.getAdjustedUpperLimit());
final int tmpKey = model.indexOf(tmpVar);
final double tmpFactor = tmpVar.getAdjustmentFactor();
final int tmpPosInd = model.indexOfPositiveVariable(tmpKey);
if (tmpPosInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpPosVarsBaseIndex + tmpPosInd, -tmpFactor);
}
final int tmpNegInd = model.indexOfNegativeVariable(tmpKey);
if (tmpNegInd >= 0) {
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpNegVarsBaseIndex + tmpNegInd, tmpFactor);
}
retVal.constraintsBody().set(tmpConstrBaseIndex + c, tmpCurrentSlackVarIndex++, NEG);
}
tmpConstrBaseIndex += tmpVarsNegUpLength;
if (retVal.getOvercapacity() <= OjAlgoUtils.ENVIRONMENT.getCacheElements(8L)) {
return retVal.toDense();
} else {
return retVal;
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class ConvexProblems method buildModel.
static ExpressionsBasedModel buildModel(final PrimitiveDenseStore[] matrices, final PrimitiveDenseStore expectedSolution) {
final ExpressionsBasedModel retVal = new ExpressionsBasedModel();
final int tmpNumberOfVariables = (int) matrices[3].count();
for (int v = 0; v < tmpNumberOfVariables; v++) {
final Variable tmpVariable = Variable.make("X" + v);
tmpVariable.setValue(BigDecimal.valueOf(expectedSolution.doubleValue(v)));
retVal.addVariable(tmpVariable);
}
if ((matrices[0] != null) && (matrices[1] != null)) {
for (int e = 0; e < matrices[0].countRows(); e++) {
final Expression tmpExpression = retVal.addExpression("E" + e);
for (int v = 0; v < tmpNumberOfVariables; v++) {
tmpExpression.set(v, matrices[0].get(e, v));
}
tmpExpression.level(matrices[1].doubleValue(e));
}
}
if ((matrices[4] != null) && (matrices[5] != null)) {
for (int i = 0; i < matrices[4].countRows(); i++) {
final Expression tmpExpression = retVal.addExpression("I" + i);
for (int v = 0; v < tmpNumberOfVariables; v++) {
tmpExpression.set(v, matrices[4].get(i, v));
}
tmpExpression.upper(matrices[5].doubleValue(i));
}
}
final Expression tmpObjQ = retVal.addExpression("Q");
for (int r = 0; r < tmpNumberOfVariables; r++) {
for (int v = 0; v < tmpNumberOfVariables; v++) {
tmpObjQ.set(r, v, matrices[2].doubleValue(r, v));
}
}
tmpObjQ.weight(HALF);
final Expression tmpObjC = retVal.addExpression("C");
for (int v = 0; v < tmpNumberOfVariables; v++) {
tmpObjC.set(v, matrices[3].doubleValue(v));
}
tmpObjC.weight(NEG);
return retVal;
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class ConvexProblems method testInfeasibleCase.
/**
* Just make sure an obviously infeasible problem is recognised as such - this has been a problem in the
* past
*/
@Test
public void testInfeasibleCase() {
final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ONE).upper(TWO).weight(ONE), new Variable("X2").lower(ONE).upper(TWO).weight(TWO), new Variable("X3").lower(ONE).upper(TWO).weight(THREE) };
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
final Expression tmpExprQ = tmpModel.addExpression("Q1");
for (int i = 0; i < tmpModel.countVariables(); i++) {
for (int j = 0; j < tmpModel.countVariables(); j++) {
tmpExprQ.set(i, i, Math.random());
}
}
// May not be positive definite, but infeasibillity should be realised before that becomes a problem
tmpExprQ.weight(TEN);
// tmpModel.options.debug(ConvexSolver.class);
final Expression tmpExprC1 = tmpModel.addExpression("C1");
for (int i = 0; i < tmpModel.countVariables(); i++) {
tmpExprC1.set(i, ONE);
}
tmpExprC1.upper(TWO);
Optimisation.Result tmpResult = tmpModel.maximise();
TestUtils.assertFalse(tmpResult.getState().isFeasible());
tmpExprC1.upper(null);
tmpExprC1.lower(SEVEN);
tmpResult = tmpModel.maximise();
TestUtils.assertFalse(tmpResult.getState().isFeasible());
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, null);
}
Aggregations