use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class Expression method evaluate.
public BigDecimal evaluate(final Access1D<BigDecimal> point) {
BigDecimal retVal = BigMath.ZERO;
BigDecimal tmpFactor;
for (final IntRowColumn tmpKey : this.getQuadraticKeySet()) {
tmpFactor = this.get(tmpKey);
retVal = retVal.add(tmpFactor.multiply(point.get(tmpKey.row)).multiply(point.get(tmpKey.column)));
}
for (final IntIndex tmpKey : this.getLinearKeySet()) {
tmpFactor = this.get(tmpKey);
retVal = retVal.add(tmpFactor.multiply(point.get(tmpKey.index)));
}
return retVal;
}
use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class Expression method calculateFixedValue.
/**
* Calculates this expression's fixed value - the fixed variables' part of this expression. Will never
* return null.
*/
BigDecimal calculateFixedValue(final Collection<IntIndex> fixedVariables) {
BigDecimal retVal = BigMath.ZERO;
if (fixedVariables.size() > 0) {
for (final IntIndex tmpKey : myLinear.keySet()) {
if (fixedVariables.contains(tmpKey)) {
final BigDecimal tmpFactor = this.get(tmpKey);
final BigDecimal tmpValue = myModel.getVariable(tmpKey.index).getValue();
retVal = retVal.add(tmpFactor.multiply(tmpValue));
}
}
for (final IntRowColumn tmpKey : myQuadratic.keySet()) {
if (fixedVariables.contains(new IntIndex(tmpKey.row))) {
if (fixedVariables.contains(new IntIndex(tmpKey.column))) {
final BigDecimal tmpFactor = this.get(tmpKey);
final BigDecimal tmpRowValue = myModel.getVariable(tmpKey.row).getValue();
final BigDecimal tmpColValue = myModel.getVariable(tmpKey.column).getValue();
retVal = retVal.add(tmpFactor.multiply(tmpRowValue).multiply(tmpColValue));
}
}
}
}
return retVal;
}
use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class Expression method getAdjustedGradient.
public MatrixStore<Double> getAdjustedGradient(final Access1D<?> point) {
final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(myModel.countVariables(), 1);
final BinaryFunction<Double> tmpBaseFunc = PrimitiveFunction.ADD;
double tmpAdjustedFactor;
UnaryFunction<Double> tmpModFunc;
for (final IntRowColumn tmpKey : this.getQuadraticKeySet()) {
tmpAdjustedFactor = this.getAdjustedQuadraticFactor(tmpKey);
tmpModFunc = tmpBaseFunc.second(tmpAdjustedFactor * point.doubleValue(tmpKey.column));
retVal.modifyOne(tmpKey.row, 0, tmpModFunc);
tmpModFunc = tmpBaseFunc.second(tmpAdjustedFactor * point.doubleValue(tmpKey.row));
retVal.modifyOne(tmpKey.column, 0, tmpModFunc);
}
for (final IntIndex tmpKey : this.getLinearKeySet()) {
tmpAdjustedFactor = this.getAdjustedLinearFactor(tmpKey);
tmpModFunc = tmpBaseFunc.second(tmpAdjustedFactor);
retVal.modifyOne(tmpKey.index, 0, tmpModFunc);
}
return retVal;
}
use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class ExpressionsBasedModel method addVariable.
public void addVariable(final Variable variable) {
if (myWorkCopy) {
throw new IllegalStateException("This model is a work copy - its set of variables cannot be modified!");
} else {
myVariables.add(variable);
variable.setIndex(new IntIndex(myVariables.size() - 1));
}
}
use of org.ojalgo.access.Structure1D.IntIndex in project ojAlgo by optimatika.
the class ExpressionsBasedModel method presolve.
final void presolve() {
myExpressions.values().forEach(expr -> expr.setRedundant(false));
boolean needToRepeat = false;
do {
final Set<IntIndex> fixedVariables = this.getFixedVariables();
BigDecimal fixedValue;
needToRepeat = false;
for (final Expression expr : this.getExpressions()) {
if (!needToRepeat && expr.isConstraint() && !expr.isInfeasible() && !expr.isRedundant() && (expr.countQuadraticFactors() == 0)) {
fixedValue = options.solution.enforce(expr.calculateFixedValue(fixedVariables));
for (final Presolver presolver : PRESOLVERS) {
if (!needToRepeat) {
needToRepeat |= presolver.simplify(expr, fixedVariables, fixedValue, this::getVariable, options.solution);
}
}
}
}
} while (needToRepeat);
this.categoriseVariables();
}
Aggregations