Search in sources :

Example 1 with IntIndex

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;
}
Also used : IntRowColumn(org.ojalgo.access.Structure2D.IntRowColumn) BigDecimal(java.math.BigDecimal) IntIndex(org.ojalgo.access.Structure1D.IntIndex)

Example 2 with IntIndex

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;
}
Also used : IntRowColumn(org.ojalgo.access.Structure2D.IntRowColumn) BigDecimal(java.math.BigDecimal) IntIndex(org.ojalgo.access.Structure1D.IntIndex)

Example 3 with IntIndex

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;
}
Also used : IntRowColumn(org.ojalgo.access.Structure2D.IntRowColumn) IntIndex(org.ojalgo.access.Structure1D.IntIndex) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 4 with IntIndex

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));
    }
}
Also used : IntIndex(org.ojalgo.access.Structure1D.IntIndex)

Example 5 with IntIndex

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();
}
Also used : IntIndex(org.ojalgo.access.Structure1D.IntIndex) BigDecimal(java.math.BigDecimal)

Aggregations

IntIndex (org.ojalgo.access.Structure1D.IntIndex)15 BigDecimal (java.math.BigDecimal)9 IntRowColumn (org.ojalgo.access.Structure2D.IntRowColumn)6 Expression (org.ojalgo.optimisation.Expression)3 Variable (org.ojalgo.optimisation.Variable)3 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)2 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)2 NumberContext (org.ojalgo.type.context.NumberContext)2 Arrays (java.util.Arrays)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 OjAlgoUtils (org.ojalgo.OjAlgoUtils)1 Access1D (org.ojalgo.access.Access1D)1 Mutate1D (org.ojalgo.access.Mutate1D)1 Mutate2D (org.ojalgo.access.Mutate2D)1 Array1D (org.ojalgo.array.Array1D)1 SparseArray (org.ojalgo.array.SparseArray)1 PrimitiveMath (org.ojalgo.constant.PrimitiveMath)1 PrimitiveFunction (org.ojalgo.function.PrimitiveFunction)1