Search in sources :

Example 1 with IntRowColumn

use of org.ojalgo.access.Structure2D.IntRowColumn 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 IntRowColumn

use of org.ojalgo.access.Structure2D.IntRowColumn 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 IntRowColumn

use of org.ojalgo.access.Structure2D.IntRowColumn 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 IntRowColumn

use of org.ojalgo.access.Structure2D.IntRowColumn in project ojAlgo by optimatika.

the class Expression method getAdjustedHessian.

public MatrixStore<Double> getAdjustedHessian() {
    final int tmpCountVariables = myModel.countVariables();
    final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(tmpCountVariables, tmpCountVariables);
    final BinaryFunction<Double> tmpBaseFunc = PrimitiveFunction.ADD;
    UnaryFunction<Double> tmpModFunc;
    for (final IntRowColumn tmpKey : this.getQuadraticKeySet()) {
        tmpModFunc = tmpBaseFunc.second(this.getAdjustedQuadraticFactor(tmpKey));
        retVal.modifyOne(tmpKey.row, tmpKey.column, tmpModFunc);
        retVal.modifyOne(tmpKey.column, tmpKey.row, tmpModFunc);
    }
    return retVal;
}
Also used : IntRowColumn(org.ojalgo.access.Structure2D.IntRowColumn) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 5 with IntRowColumn

use of org.ojalgo.access.Structure2D.IntRowColumn in project ojAlgo by optimatika.

the class Expression method compensate.

/**
 * Will return an Expression with factors corresponding to fixed variables removed, and lower/upper limits
 * compensated for the fixed part of the expression. Factors corresponding to bilinear variables, where
 * one is fixed and the other is not, are linearized.
 *
 * @param fixedVariables A set of (by the presolver) fixed variable indices
 * @return The reduced/modified expression
 */
public Expression compensate(final Set<IntIndex> fixedVariables) {
    if ((fixedVariables.size() == 0) || (!this.isAnyQuadraticFactorNonZero() && Collections.disjoint(fixedVariables, this.getLinearKeySet()))) {
        // No need to copy/compensate anything
        return this;
    } else {
        final ExpressionsBasedModel tmpModel = this.getModel();
        final Expression retVal = new Expression(this.getName(), tmpModel);
        BigDecimal tmpFixedValue = BigMath.ZERO;
        for (final Entry<IntIndex, BigDecimal> tmpEntry : myLinear.entrySet()) {
            final IntIndex tmpKey = tmpEntry.getKey();
            final BigDecimal tmpFactor = tmpEntry.getValue();
            if (fixedVariables.contains(tmpKey)) {
                // Fixed
                final BigDecimal tmpValue = tmpModel.getVariable(tmpKey.index).getValue();
                tmpFixedValue = tmpFixedValue.add(tmpFactor.multiply(tmpValue));
            } else {
                // Not fixed
                retVal.set(tmpKey, tmpFactor);
            }
        }
        for (final Entry<IntRowColumn, BigDecimal> tmpEntry : myQuadratic.entrySet()) {
            final IntRowColumn tmpKey = tmpEntry.getKey();
            final BigDecimal tmpFactor = tmpEntry.getValue();
            final Variable tmpRowVariable = tmpModel.getVariable(tmpKey.row);
            final Variable tmpColVariable = tmpModel.getVariable(tmpKey.column);
            final IntIndex tmpRowKey = tmpRowVariable.getIndex();
            final IntIndex tmpColKey = tmpColVariable.getIndex();
            if (fixedVariables.contains(tmpRowKey)) {
                final BigDecimal tmpRowValue = tmpRowVariable.getValue();
                if (fixedVariables.contains(tmpColKey)) {
                    // Both fixed
                    final BigDecimal tmpColValue = tmpColVariable.getValue();
                    tmpFixedValue = tmpFixedValue.add(tmpFactor.multiply(tmpRowValue).multiply(tmpColValue));
                } else {
                    // Row fixed
                    retVal.add(tmpColKey, tmpFactor.multiply(tmpRowValue));
                }
            } else {
                if (fixedVariables.contains(tmpColKey)) {
                    // Column fixed
                    final BigDecimal tmpColValue = tmpColVariable.getValue();
                    retVal.add(tmpRowKey, tmpFactor.multiply(tmpColValue));
                } else {
                    // Neither fixed
                    retVal.set(tmpKey, tmpFactor);
                }
            }
        }
        if (this.isLowerLimitSet()) {
            retVal.lower(this.getLowerLimit().subtract(tmpFixedValue));
        }
        if (this.isUpperLimitSet()) {
            retVal.upper(this.getUpperLimit().subtract(tmpFixedValue));
        }
        return retVal;
    }
}
Also used : IntRowColumn(org.ojalgo.access.Structure2D.IntRowColumn) BigDecimal(java.math.BigDecimal) IntIndex(org.ojalgo.access.Structure1D.IntIndex)

Aggregations

IntRowColumn (org.ojalgo.access.Structure2D.IntRowColumn)7 IntIndex (org.ojalgo.access.Structure1D.IntIndex)6 BigDecimal (java.math.BigDecimal)4 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)2 Expression (org.ojalgo.optimisation.Expression)1 Variable (org.ojalgo.optimisation.Variable)1