예제 #1
0
VariablePtr Transformer::newVar_(CGraphPtr cg, ProblemPtr newp)
{
  VariablePtr iv;
  VariablePtr ov = VariablePtr(); // NULL
  FunctionPtr f;
  LinearFunctionPtr lf;
  ConstraintPtr cnew;
  assert(cg);

  if (OpSumList!=cg->getOut()->getOp()) {
    ov = yUniExprs_->findY(cg);
  } 

  if (!ov) {
    ov = newp->newVariable(VarTran);
    lf = (LinearFunctionPtr) new LinearFunction();
    lf->addTerm(ov, -1.0);
    f = (FunctionPtr) new Function(lf, cg);
    cnew = newp->newConstraint(f, 0.0, 0.0);
#if SPEW
      logger_->msgStream(LogDebug) << me_ << "added new constraint "
                                   << std::endl;
      cnew->write(logger_->msgStream(LogDebug));
#endif 
    assignHandler_(cg, cnew);

    yUniExprs_->insert(ov, cg);
  }
  return ov;
}
예제 #2
0
VariablePtr Transformer::newVar_(VariablePtr iv, double d, ProblemPtr newp)
{
  if (fabs(d)<zTol_) {
    return iv;
  } else {
    LinearFunctionPtr lf = (LinearFunctionPtr) new LinearFunction();
    VariablePtr ov;
    FunctionPtr f;
    ConstraintPtr cnew;

    ov = yVars_->findY(iv, d);
    if (!ov) {
      ov = newp->newVariable(VarTran);
      yVars_->insert(ov, iv, d);
      lf->addTerm(iv, 1.0);
      lf->addTerm(ov, -1.0);
      f = (FunctionPtr) new Function(lf);
      cnew = newp->newConstraint(f, -d, -d);
#if SPEW
      logger_->msgStream(LogDebug) << me_ << "added new constraint "
                                   << std::endl;
      cnew->write(logger_->msgStream(LogDebug));
#endif 
      lHandler_->addConstraint(cnew);
    }
    return ov;
  }
}
예제 #3
0
VariablePtr Transformer::newVar_(LinearFunctionPtr lf, double d,
                                 ProblemPtr newp)
{
  VariablePtr ov;
  FunctionPtr f;
  ConstraintPtr cnew;

  ov = yLfs_->findY(lf, d);
  if (!ov) {
    LinearFunctionPtr lf2 = lf->clone();
    ov = newp->newVariable(VarTran);
    yLfs_->insert(ov, lf2, d);
    lf->addTerm(ov, -1.0);
    f = (FunctionPtr) new Function(lf);
    cnew = newp->newConstraint(f, -d, -d);
#if SPEW
    logger_->msgStream(LogDebug) << me_ << "added new constraint "
                                 << std::endl;
    cnew->write(logger_->msgStream(LogDebug));
#endif 

    lHandler_->addConstraint(cnew);
  }
  return ov;
}
예제 #4
0
void Transformer::copyLinear_(ConstProblemPtr p, ProblemPtr newp)
{
  FunctionPtr f;
  LinearFunctionPtr newlf;
  ConstConstraintPtr c;
  ConstraintPtr newc;
  ObjectivePtr obj;

  // copy linear constraints.
  for (ConstraintConstIterator it=p->consBegin(); it!=p->consEnd(); ++it) {
    c = *it;
    if (Linear==c->getFunction()->getType()) {
      // create a clone of this linear function.
      newlf = c->getLinearFunction()->cloneWithVars(newp->varsBegin());
      f = (FunctionPtr) new Function(newlf);
      newc = newp->newConstraint(f, c->getLb(), c->getUb());
      lHandler_->addConstraint(newc);
    }
  }

  // copy linear objective.
  obj = p->getObjective();

  if (!obj) {
    f.reset();
    newp_->newObjective(f, 0.0, Minimize);
  } else {
    switch (obj->getFunctionType()) {
    case (Constant):
      f = FunctionPtr(); // NULL
      newp->newObjective(f, obj->getConstant(), obj->getObjectiveType(),
                         obj->getName());
      break;
    case (Linear):
      newlf = obj->getFunction()->getLinearFunction()->
      cloneWithVars(newp->varsBegin());
      f = (FunctionPtr) new Function(newlf); 
      newp->newObjective(f, obj->getConstant(), obj->getObjectiveType(),
                         obj->getName());
      break;
    default:
      break;
    }
  }
}