Пример #1
0
  @SuppressWarnings("unchecked")
  static void copy(
      final ExpressionsBasedModel sourceModel, final QuadraticSolver.Builder destinationBuilder) {

    final List<Variable> tmpFreeVariables = sourceModel.getFreeVariables();
    final Set<Index> 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.selectExpressionsLinearEquality();
    final int tmpEqExprDim = tmpEqExpr.size();

    if (tmpEqExprDim > 0) {

      final PhysicalStore<Double> tmpAE = FACTORY.makeZero(tmpEqExprDim, tmpFreeVarDim);
      final PhysicalStore<Double> tmpBE = FACTORY.makeZero(tmpEqExprDim, 1);

      for (int i = 0; i < tmpEqExprDim; i++) {

        final Expression tmpExpression = tmpEqExpr.get(i);

        for (final Expression.Index tmpKey : tmpExpression.getLinearFactorKeys()) {
          final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
          if (tmpIndex >= 0) {
            tmpAE.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
          }
        }
        tmpBE.set(i, 0, tmpExpression.getCompensatedUpperLimit(tmpFixedVariables));
      }

      destinationBuilder.equalities(tmpAE, tmpBE);
    }

    // Q & C

    final Expression tmpObjExpr = sourceModel.getObjectiveExpression();

    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 Expression.RowColumn tmpKey : tmpObjExpr.getQuadraticFactorKeys()) {
        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 Expression.Index tmpKey : tmpObjExpr.getLinearFactorKeys()) {
          final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
          if (tmpIndex >= 0) {
            tmpC.set(tmpIndex, 0, -tmpObjExpr.getAdjustedLinearFactor(tmpKey));
          }
        }
      } else {
        for (final Expression.Index tmpKey : tmpObjExpr.getLinearFactorKeys()) {
          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.selectExpressionsLinearUpper();
    final int tmpUpExprDim = tmpUpExpr.size();
    final List<Variable> tmpUpVar = sourceModel.selectVariablesFreeUpper();
    final int tmpUpVarDim = tmpUpVar.size();

    final List<Expression> tmpLoExpr = sourceModel.selectExpressionsLinearLower();
    final int tmpLoExprDim = tmpLoExpr.size();
    final List<Variable> tmpLoVar = sourceModel.selectVariablesFreeLower();
    final int tmpLoVarDim = tmpLoVar.size();

    if ((tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim) > 0) {

      final ModelEntity<?>[] tmpEntities =
          new ModelEntity<?>[tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim];

      final PhysicalStore<Double> tmpUAI =
          FACTORY.makeZero(tmpUpExprDim + tmpUpVarDim, tmpFreeVarDim);
      final PhysicalStore<Double> tmpUBI = FACTORY.makeZero(tmpUpExprDim + tmpUpVarDim, 1);

      if (tmpUpExprDim > 0) {
        for (int i = 0; i < tmpUpExprDim; i++) {
          final Expression tmpExpression = tmpUpExpr.get(i);
          for (final Expression.Index tmpKey : tmpExpression.getLinearFactorKeys()) {
            final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
            if (tmpIndex >= 0) {
              tmpUAI.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
            }
          }
          tmpUBI.set(i, 0, tmpExpression.getCompensatedUpperLimit(tmpFixedVariables));
          tmpEntities[i] = tmpExpression;
        }
      }

      if (tmpUpVarDim > 0) {
        for (int i = 0; i < tmpUpVarDim; i++) {
          final Variable tmpVariable = tmpUpVar.get(i);
          tmpUAI.set(
              tmpUpExprDim + i,
              sourceModel.indexOfFreeVariable(tmpVariable),
              tmpVariable.getAdjustmentFactor());
          tmpUBI.set(tmpUpExprDim + i, 0, tmpVariable.getAdjustedUpperLimit());
          tmpEntities[tmpUpExprDim + i] = tmpVariable;
        }
      }

      final PhysicalStore<Double> tmpLAI =
          FACTORY.makeZero(tmpLoExprDim + tmpLoVarDim, tmpFreeVarDim);
      final PhysicalStore<Double> tmpLBI = FACTORY.makeZero(tmpLoExprDim + tmpLoVarDim, 1);

      if (tmpLoExprDim > 0) {
        for (int i = 0; i < tmpLoExprDim; i++) {
          final Expression tmpExpression = tmpLoExpr.get(i);
          for (final Expression.Index tmpKey : tmpExpression.getLinearFactorKeys()) {
            final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
            if (tmpIndex >= 0) {
              tmpLAI.set(i, tmpIndex, -tmpExpression.getAdjustedLinearFactor(tmpKey));
            }
          }
          tmpLBI.set(i, 0, -tmpExpression.getCompensatedLowerLimit(tmpFixedVariables));
          tmpEntities[tmpUpExprDim + tmpUpVarDim + i] = tmpExpression;
        }
      }

      if (tmpLoVarDim > 0) {
        for (int i = 0; i < tmpLoVarDim; i++) {
          final Variable tmpVariable = tmpLoVar.get(i);
          tmpLAI.set(
              tmpLoExprDim + i,
              sourceModel.indexOfFreeVariable(tmpVariable),
              -tmpVariable.getAdjustmentFactor());
          tmpLBI.set(tmpLoExprDim + i, 0, -tmpVariable.getAdjustedLowerLimit());
          tmpEntities[tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + i] = tmpVariable;
        }
      }

      final MatrixStore<Double> tmpAI = tmpLAI.builder().above(tmpUAI).build();
      final MatrixStore<Double> tmpBI = tmpLBI.builder().above(tmpUBI).build();

      destinationBuilder.inequalities(tmpAI, tmpBI, tmpEntities);

      final IndexSelector tmpSelector = new IndexSelector(tmpEntities.length);
      for (int i = 0; i < tmpEntities.length; i++) {
        if (tmpEntities[i].isActiveInequalityConstraint()) {
          tmpSelector.include(i);
        }
      }

      tmpKickStarter.activeSet(tmpSelector.getIncluded());
    }

    destinationBuilder.setKickStarter(tmpKickStarter);
  }
Пример #2
0
  public static QuadraticSolver make(final ExpressionsBasedModel aModel) {

    final QuadraticSolver.Builder tmpBuilder = new QuadraticSolver.Builder(aModel);

    return tmpBuilder.build();
  }