use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class ExpressionsBasedModel method generateCuts.
/**
* @param constraints Linear constraints with all binary variables
*/
private void generateCuts(final Set<Expression> constraints) {
if ((constraints != null) && (constraints.size() > 0)) {
final List<Variable> posBinVar = new ArrayList<>();
final List<Variable> negBinVar = new ArrayList<>();
final Set<IntIndex> indices = new HashSet<>();
final Set<IntIndex> fixedVariables = this.getFixedVariables();
for (final Expression tmpExpression : constraints) {
posBinVar.clear();
negBinVar.clear();
indices.clear();
indices.addAll(tmpExpression.getLinearKeySet());
final int countExprVars = indices.size();
indices.removeAll(fixedVariables);
for (final IntIndex tmpIndex : indices) {
final Variable tmpVariable = this.getVariable(tmpIndex);
if (tmpVariable.isBinary()) {
final BigDecimal tmpFactor = tmpExpression.get(tmpIndex);
if (tmpFactor.signum() == 1) {
posBinVar.add(tmpVariable);
} else if (tmpFactor.signum() == -1) {
negBinVar.add(tmpVariable);
}
}
}
if ((posBinVar.size() == indices.size()) && (posBinVar.size() != countExprVars) && (posBinVar.size() != 0)) {
// All remaining (not fixed) variables are binary with positive constraint factors
final BigDecimal ul = tmpExpression.getUpperLimit();
if ((ul != null) && (ul.signum() != -1)) {
posBinVar.sort((v1, v2) -> tmpExpression.get(v1.getIndex()).compareTo(tmpExpression.get(v2.getIndex())));
BigDecimal accum = BigMath.ZERO;
int count = 0;
for (final Variable tmpVariable : posBinVar) {
accum = accum.add(tmpExpression.get(tmpVariable));
if (accum.compareTo(ul) > 0) {
final Expression tmpNewCut = this.addExpression("Cut-" + tmpExpression.getName());
tmpNewCut.setLinearFactorsSimple(posBinVar);
tmpNewCut.upper(new BigDecimal(count));
break;
}
count++;
}
}
}
if (posBinVar.size() == indices.size()) {
// All remaining (not fixed) variables are binary with negative constraint factors
}
}
}
}
use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class ExpressionsBasedModel method objective.
/**
* This is generated on demend – you should not cache this. More specifically, modifications made to this
* expression will not be part of the optimisation model. You define the objective by setting the
* {@link Variable#weight(Number)}/{@link Expression#weight(Number)} on one or more variables and/or
* expressions.
*
* @return The generated/aggregated objective function
*/
public Expression objective() {
final Expression retVal = new Expression(OBJECTIVE, this);
Variable tmpVariable;
for (int i = 0; i < myVariables.size(); i++) {
tmpVariable = myVariables.get(i);
if (tmpVariable.isObjective()) {
retVal.set(i, tmpVariable.getContributionWeight());
}
}
BigDecimal tmpOldVal = null;
BigDecimal tmpDiff = null;
BigDecimal tmpNewVal = null;
for (final Expression tmpExpression : myExpressions.values()) {
if (tmpExpression.isObjective()) {
final BigDecimal tmpContributionWeight = tmpExpression.getContributionWeight();
// To avoid multiplication by 1.0
final boolean tmpNotOne = tmpContributionWeight.compareTo(ONE) != 0;
if (tmpExpression.isAnyLinearFactorNonZero()) {
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
tmpOldVal = retVal.get(tmpKey);
tmpDiff = tmpExpression.get(tmpKey);
tmpNewVal = tmpOldVal.add(tmpNotOne ? tmpContributionWeight.multiply(tmpDiff) : tmpDiff);
final Number value = tmpNewVal;
retVal.set(tmpKey, value);
}
}
if (tmpExpression.isAnyQuadraticFactorNonZero()) {
for (final IntRowColumn tmpKey : tmpExpression.getQuadraticKeySet()) {
tmpOldVal = retVal.get(tmpKey);
tmpDiff = tmpExpression.get(tmpKey);
tmpNewVal = tmpOldVal.add(tmpNotOne ? tmpContributionWeight.multiply(tmpDiff) : tmpDiff);
final Number value = tmpNewVal;
retVal.set(tmpKey, value);
}
}
}
}
return retVal;
}
use of org.ojalgo.access.Structure1D.IntIndex 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.access.Structure1D.IntIndex 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.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class MarketShareCase method testRedundant.
private void testRedundant(final String constraint) {
final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
final Expression tmpExpression = tmpModel.getExpression(constraint);
if (DEBUG) {
BasicLogger.debug("Fix count: {}", tmpExpression.getLinearKeySet().size());
}
for (final IntIndex tmpIndex : tmpExpression.getLinearKeySet()) {
final Variable tmpVariable = tmpModel.getVariable(tmpIndex.index);
final String tmpName = tmpVariable.getName();
tmpVariable.level(SOLUTION.get(tmpName));
}
final Result tmpResult = tmpModel.minimise();
final NumberContext tmpContext = new NumberContext(8, 13);
TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), tmpContext);
for (final Variable tmpVariable : tmpModel.getVariables()) {
TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), tmpContext);
}
}
Aggregations