当前位置: 首页>>代码示例>>C++>>正文


C++ VariablePtr类代码示例

本文整理汇总了C++中VariablePtr的典型用法代码示例。如果您正苦于以下问题:C++ VariablePtr类的具体用法?C++ VariablePtr怎么用?C++ VariablePtr使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了VariablePtr类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: assert

void ParQGHandler::linearizeObj_()
{
  ObjectivePtr o = minlp_->getObjective();
  FunctionType fType = o->getFunctionType();
  if (!o) {
    assert(!"need objective in QG!");
  } else if (fType != Linear && fType != Constant) {
    oNl_ = true;
    FunctionPtr f;
    VariablePtr vPtr;
    ObjectiveType objType = o->getObjectiveType();
    LinearFunctionPtr lf = (LinearFunctionPtr) new LinearFunction();
    for (VariableConstIterator viter=rel_->varsBegin(); viter!=rel_->varsEnd();
         ++viter) {
      vPtr = *viter;
      if (vPtr->getName() == "eta") {
        assert(o->getObjectiveType()==Minimize);
        rel_->removeObjective();
        lf->addTerm(vPtr, 1.0);
        f = (FunctionPtr) new Function(lf);
        rel_->newObjective(f, 0.0, objType);
        objVar_ = vPtr;
        break;
      }
    }
  }
  return;
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:28,代码来源:ParQGHandler.cpp

示例2: size

	AllocaInsn::AllocaInsn(const VariablePtr& variable, const ValuePtr& size, const ValueList& dimensions) :
		Insn(IC_Stack, IT_Alloca), size(size), variable(variable), dimensions(dimensions) {
		assert(variable && "variable must not be null");
		assert(variable->getValueType() == Value::VT_Memory && "variable must not be a temporary");
		assert(size && "size must not be null");
		assert(size->getType()->isInt() && "size must be of type int");

		if (analysis::types::isArray(variable->getType())) {
			auto type = cast<ArrayType>(variable->getType());
			assert(type->getNumOfDimensions() == dimensions.size() && "dimension information mismatch with type");
		}
	}
开发者ID:sp4r74n-117,项目名称:mC,代码行数:12,代码来源:core.cpp

示例3: getBranches

Branches MultilinearTermsHandler::getBranches(BrCandPtr cand, DoubleVector & x,
                                         RelaxationPtr, SolutionPoolPtr)
{
  BrVarCandPtr vcand = boost::dynamic_pointer_cast <BrVarCand> (cand);
  VariablePtr v = vcand->getVar();
  double value = x[v->getIndex()];

  // can't branch on something that is at its bounds.
  if (!(value > v->getLb()+1e-8 && value < v->getUb()-1e-8)) {
    cerr << "Warning!  Branching on variable with bounds/value: [" << 
      v->getLb() << " , " << value << "  " << v->getUb() << " ]" << endl;
    //assert(value > v->getLb()+1e-8 && value < v->getUb()-1e-8);
  }

  Branches branches = (Branches) new BranchPtrVector();
  BranchPtr branch;
  branch = doBranch_(DownBranch, v, value);
  branches->push_back(branch);
  branch = doBranch_(UpBranch, v, value);
  branches->push_back(branch);


  logger_->msgStream(LogDebug2) << "branching on " << v->getName();
  logger_->msgStream(LogDebug2) << " <= " << value << " or " 
    << " >= " << value << endl;

#if defined(DEBUG_MULTILINEARTERMS_HANDLER)  
  cout << "branching on " << v->getName();
  cout << " <= " << value << " or " << " >= " << value << endl;
#endif

  return branches;
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:33,代码来源:MultilinearTermsHandler.cpp

示例4: BoundsOnSquare

SecantMod::SecantMod(ConstraintPtr con, LinearFunctionPtr new_lf,
                     double new_rhs, VariablePtr x, BoundType lu, double new_b,
                     VariablePtr y)
{
  double y_lb, y_ub, b2;
  if (lu==Lower) {
    b2 = x->getUb();
    BoundsOnSquare(new_b, b2, y_lb, y_ub);
  } else {
    b2 = x->getLb();
    BoundsOnSquare(b2, new_b, y_lb, y_ub);
  }
  ymod_ = (VarBoundMod2Ptr) new VarBoundMod2(y, y_lb, y_ub);
  xmod_ = (VarBoundModPtr) new VarBoundMod(x, lu, new_b);
  lmod_ = (LinConModPtr) new LinConMod(con, new_lf, -INFINITY, new_rhs);
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:16,代码来源:SecantMod.cpp

示例5: if

bool SOS1Handler::isGUB(SOS *sos)
{
  VariablePtr var;
  int nz = sos->getNz();
  ConstConstraintPtr c;
  FunctionPtr f;
  LinearFunctionPtr lf;
  bool isgub = false;


  for (VariableConstIterator viter = sos->varsBegin(); viter!=sos->varsEnd();
       ++viter) {
    var = *viter;
    if (Binary!=var->getType() || ImplBin!=var->getType()) {
      return false;
    }
  }

  // If we are here, then it means all variables are binary. Check if their sum
  // is <= 1.
  var = *(sos->varsBegin());
  for (ConstrSet::iterator it=var->consBegin();
       it!=var->consEnd() && false==isgub; ++it) {
    c = *it;
    f = c->getFunction();
    isgub = true;
    if (Linear!=f->getType()) {
      isgub = false;
    } else if ((UInt) nz==f->getNumVars()) {
      isgub = false;
    } else if (fabs(c->getUb()-1.0)>zTol_) {
      isgub = false;
    } else {
      lf = f->getLinearFunction();
      for (VariableConstIterator viter = sos->varsBegin();
           viter!=sos->varsEnd(); ++viter) {
        if ((lf->getWeight(*viter)-1.0)>zTol_) {
          isgub = false;
          break;
        }
      }
    }
  }

  return isgub;
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:46,代码来源:SOS1Handler.cpp

示例6:

void Sgd<T>::update_impl(const string &key, VariablePtr param) {
  Size_t size = param->size();

  const T *grad = param->get_grad_pointer<T>(this->ctx_);
  T *data = param->cast_data_and_get_pointer<T>(this->ctx_);
  std::transform(grad, grad + size, data, data,
                 [this](T g, T x) { return x - lr_ * g; });
}
开发者ID:zwsong,项目名称:nnabla,代码行数:8,代码来源:sgd.cpp

示例7: fixInts_

void ParQGHandler::fixInts_(const double *x)
{
  double xval;
  VariablePtr v;
  VarBoundMod2 *m = 0;
  for (VariableConstIterator vit=minlp_->varsBegin(); vit!=minlp_->varsEnd();
       ++vit) {
    v = *vit;
    if (v->getType()==Binary || v->getType()==Integer) {
      xval = x[v->getIndex()];
      xval = floor(xval + 0.5);
      m = new VarBoundMod2(v, xval, xval);
      m->applyToProblem(minlp_);
      nlpMods_.push(m);
    }
  }
  return;
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:18,代码来源:ParQGHandler.cpp

示例8: constructObj_

void LinFeasPump::constructObj_(ProblemPtr, ConstSolutionPtr)
{
  double value, lb, ub;
  VariablePtr variable;
  UInt i;
  //double obj_relaxation;
  double obj_weight;
  double constant;
  FunctionPtr f;
  LinearFunctionPtr olf_mod;

  //obj_relaxation = sol->getPrimal()[r_->getNumVars()-1];
  obj_weight     = 0; //std::min(1/(fabs(obj_relaxation)+1e-7), 0.1);
  obj_weight     = 0.0;
  i              = 0;
  constant       = 0;
  olf_mod        = olfClone_->clone();
  (*olf_mod)    *= obj_weight;

  for (VariableConstIterator v_iter=r_->varsBegin();
      v_iter!=r_->varsEnd(); ++v_iter, ++i) {
    variable = *v_iter;
    if (variable->getType() == Binary) {
      value  = roundedSol_[i];
      lb     = variable->getLb();
      ub     = variable->getUb();
      if (fabs(value - lb) > intTol_) { 
        olf_mod->addTerm(variable,-1.0);
        constant += ub;
      } else if (fabs(value - ub) > intTol_) {
        olf_mod->addTerm(variable,1.0);
        constant -= lb;
      } else {
        // add a new variable with coeff 1
        // add two constraints for absolute value
      }      
    }
  }
  f = (FunctionPtr) new Function(olf_mod);
  r_->changeObj(f, constant);
}
开发者ID:devanandR,项目名称:minotaur,代码行数:41,代码来源:LinFeasPump.cpp

示例9: Index

int VariableLuaMetaData::Index(lua_State * L, const VariablePtr var,
								const std::string & index)
{
	///*if (index == "Const")
	//{
	//	lua_pushboolean(L, var->GetType()->IsConst() ? 1 : 0);
	//	return 1;
	//}
	//else if (index == "ConstPointer")
	//{
	//	lua_pushboolean(L, var->GetType()->IsConstPointer() ? 1 : 0);
	//	return 1;
	//}
	//else if (index == "Pointer")
	//{
	//	lua_pushboolean(L, var->GetType()->IsPointer() ? 1 : 0);
	//	return 1;
	//}
	//else if (index == "Reference")
	//{
	//	lua_pushboolean(L, var->GetType()->IsReference() ? 1 : 0);
	//	return 1;
	//}
	//else if (index == "TypeNode")
	//{
	//	Model::NodeLuaMetaData::PutInstanceOnStack(L, var->GetType()->GetNode());
	//	return 1;
	//}*/
	if (index == "Initializer")
	{
		lua_pushstring(L, var->GetInitializer().c_str());
		return 1;
	}
	else if (index == "Type")
	{
		Model::TypeLuaMetaData::PutInstanceOnStack(L, var->GetType());
		return 1;
	}
	return 0;
}
开发者ID:bowlofstew,项目名称:Macaroni,代码行数:40,代码来源:VariableLua.cpp

示例10: setUpVariablesAndObj

// -----------------------------------------------------------------------------------
void CBCSolver::setUpVariablesAndObj(std::vector<VariablePtr>& variables, 
                                     ObjFuncPtr& obj) {
  for (int i = 0; i < variables.size(); i++) {
    VariablePtr v = variables.at(i);
    switch(v->type()) {
    case Variable::INT:
      builder_.setColumnBounds(i,intBound(v->lbound()),intBound(v->ubound()));
      builder_.setInteger(i);
      break;
    case Variable::LINEAR:
      builder_.setColumnBounds(i,doubleBound(v->lbound()),doubleBound(v->ubound()));
      break;
    }
    builder_.setObjective(i,obj->getModifier(v));
  }
}
开发者ID:rjgeringer,项目名称:cyclopts,代码行数:17,代码来源:CBCSolver.cpp

示例11: getBranches

// Implement Handler::getBranches().
Branches CxUnivarHandler::getBranches(BrCandPtr cand, DoubleVector &x,
                                      RelaxationPtr , SolutionPoolPtr )
{
  double minFromBds = 0.1;
  BrVarCandPtr vcand = boost::dynamic_pointer_cast <BrVarCand> (cand);
  VariablePtr v = vcand->getVar();

  double xval = x[v->getIndex()];
  double value = xval;  // Make sure branch value is not too close to an end point
  double len = v->getUb() - v->getLb();
  if (value < v->getLb() + minFromBds*len) {
    value = v->getLb() + minFromBds*len;
  } else if (value > v->getUb() - minFromBds*len) {
    value = v->getUb() - minFromBds*len; 
  }

  // can't branch on something that is at its bounds.
  if (!(value > v->getLb()+1e-8 && value < v->getUb()-1e-8)) {
    std::cerr << "Warning!  Branching on variable with bounds/value: [" << 
      v->getLb() << " , " << value << "  " << v->getUb() << " ]" << std::endl;
    //assert(value > v->getLb()+1e-8 && value < v->getUb()-1e-8);
  }

  Branches branches = (Branches) new BranchPtrVector();

  BranchPtr branch = (BranchPtr) new Branch();
  VarBoundModPtr mod = (VarBoundModPtr) new VarBoundMod(v, Upper, value);
  assert(!"add Mod correctly here.");
  branch->addPMod(mod);
  branch->setActivity((v->getUb()-value)/len);
  branches->push_back(branch);

  branch = (BranchPtr) new Branch();
  mod = (VarBoundModPtr) new VarBoundMod(v, Lower, value);
  assert(!"add Mod correctly here.");
  branch->addPMod(mod);
  branch->setActivity((value - v->getLb())/len);
  branches->push_back(branch);


  logger_->msgStream(LogDebug2) << "branching on " << v->getName();
  logger_->msgStream(LogDebug2) << " <= " << value << " or " 
    << " >= " << value << std::endl;

#if defined(DEBUG_CXUNIVARHANDLER)  
  std::cout << "branching on " << v->getName();
  std::cout << " <= " << value << " or " << " >= " << value << std::endl;
#endif
  
  return branches;

}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:53,代码来源:CxUnivarHandler.cpp

示例12: LinMods

// Implement Handler::getBrMod().
ModificationPtr CxUnivarHandler::getBrMod(BrCandPtr cand, DoubleVector &x, 
                                          RelaxationPtr ,
                                          BranchDirection brdir) 
{

  // This method is used in Reliability branching.
  //XXX If you want it to be more accurate, you should add the new secant and linearizations
  //   into lmods.

  LinModsPtr lmods;
  lmods = (LinModsPtr) new LinMods();

  double minFromBds = 0.1;
  BrVarCandPtr vcand = boost::dynamic_pointer_cast <BrVarCand> (cand);
  VariablePtr v = vcand->getVar();

#if defined(DEBUG_CXUNIVARHANDLER)  
  std::cout << "Branching mod candidate (working problem) ID: " << v->getId() 
            << " address: " << (  v.get() ) << std::endl;
#endif  

  // x is a *relaxation* solution, while we have put the *original* (or
  // working) problem variables into the BrCandPtr, so we need to
  // update our value appropriately...
  
  double xval = x[v->getIndex()];
  double value = xval;  // Make sure branch value is not too close to an end point
  double len = v->getUb() - v->getLb();
  if (value < v->getLb() + minFromBds*len) {
    value = v->getLb() + minFromBds*len;
  } else if (value > v->getUb() - minFromBds*len) {
    value = v->getUb() - minFromBds*len; 
  }

  // can't branch on something that is at its bounds.
  if (!(value > v->getLb()+1e-8 && value < v->getUb()-1e-8)) {
    std::cerr << "Warning!  Branching on variable with bounds/value: [" << 
      v->getLb() << " , " << value << "  " << v->getUb() << " ]" << std::endl;
    //assert(value > v->getLb()+1e-8 && value < v->getUb()-1e-8);
  }

  if (brdir == DownBranch) {
    // down branch
    VarBoundModPtr mod = (VarBoundModPtr) new VarBoundMod(v, Upper, value);
    lmods->insert(mod);
  } else if (brdir ==  UpBranch) {
  // up branch
    VarBoundModPtr mod    = (VarBoundModPtr) new VarBoundMod(v, Lower, value);
    lmods->insert(mod);
  }

  return lmods;

}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:55,代码来源:CxUnivarHandler.cpp

示例13: presolve_

bool IpoptEngine::presolve_()
{
  bool should_stop = false;
  VariablePtr v;
  double diff;
  bool all_fixed = true;
  int e=0;

  status_ = EngineUnknownStatus;
  // visit each variable and check bounds. Stop if bad bounds are found or if
  // all variables are fixed.
  for (VariableConstIterator it=problem_->varsBegin(); it!=problem_->varsEnd(); 
      ++it) {
    v = *it;
    diff = v->getUb() - v->getLb();
    if (diff < -etol_) {
      status_ = ProvenLocalInfeasible;
      return true;
    }
    if (fabs(diff)>etol_) {
      all_fixed = false;
    }
  }

  if (all_fixed == true) {
    double obj_value;
    double *x;
    double vio = -INFINITY;
    double act;

    x = new double[problem_->getNumVars()];
    should_stop = true;
    for (VariableConstIterator it=problem_->varsBegin();
        it!=problem_->varsEnd(); ++it) {
      v = *it;
      x[v->getIndex()] = v->getUb();
    }
    obj_value = problem_->getObjValue(x, &e);

    // check if constraints are violated.
    for (ConstraintConstIterator it=problem_->consBegin(); 
        it!=problem_->consEnd(); ++it) {
      act = (*it)->getActivity(x, &e);
      vio = std::max(act-(*it)->getUb(), (*it)->getLb()-act);
      vio = std::max(vio, 0.);
      if (vio>etol_ || e!=0) {
        break;
      }
    }

   // sol_ must be set even if the problem is infeasible because QG handler
   // may need this point for cut generation etc.
    sol_->setPrimal(x); 
    if (vio>etol_) {
      status_ = ProvenLocalInfeasible;
    } else {
      sol_->setObjValue(obj_value);
      mynlp_->setSolution(sol_);
      status_ = ProvenLocalOptimal;
    }
    delete [] x;
  }
  return should_stop;
}
开发者ID:ashutoshmahajan,项目名称:minotaur,代码行数:64,代码来源:IpoptEngine.cpp

示例14: if

void
MultilinearFormulation::makeTermByTerm()
{

  // First we do some processing of the instance to determine how many 
  //  multilinear or quadratic constraints, and we store the indicies
  // in the instance for later    
  vector<int> lcid;
  vector<int> mlcid;
  for(ConstConstraintIterator it = originalInstance_->consBegin(); 
      it != originalInstance_->consEnd(); ++it) {
    FunctionType ft = (*it)->getFunctionType();
    if (ft == Multilinear) {
      mlcid.push_back((*it)->getId());
    }    
    else if (ft == Bilinear) {
      mlcid.push_back((*it)->getId());
    }
    else if (ft == Linear) {
      lcid.push_back((*it)->getId());
    }
  }

  // add x variables
  vector<double> lb;
  vector<double> ub;

  vector<VariablePtr> xvars;
  int nv = 0;
  for (ConstVariableIterator it = originalInstance_->varsBegin(); 
       it != originalInstance_->varsEnd(); ++it) {
    VariablePtr v = *it;
    VariablePtr vnew = VariablePtr(new Variable(nv, v->getLb(), v->getUb(), v->getType()));
    lb.push_back(v->getLb());
    ub.push_back(v->getUb());

    variableMapping_.insert(make_pair(vnew,v));    
    variables_.push_back(vnew);
    xvars.push_back(vnew);
    nv++;
  }

  // Add the linear constraints
  for(int i = 0; i < lcid.size(); i++) {
    const ConstraintPtr mlc = originalInstance_->getConstraint(lcid[i]);
    const LinearFunctionPtr olf = mlc->getLinearFunction();
    LinearFunctionPtr lf = LinearFunctionPtr(new LinearFunction());
    for(ConstVariableGroupIterator it = olf->varsBegin(); it != olf->varsEnd(); ++it) {
      lf->addTerm(xvars[it->first->getId()], it->second);
    }
    FunctionPtr f = (FunctionPtr) new Function(lf);
    ConstraintPtr c = (ConstraintPtr) new Constraint(f, mlc->getLb(), mlc->getUb());
    constraints_.push_back(c);    

#if defined(DEBUG_TERM_BY_TERM)
      c->display();
#endif

  }


  // The w variables
  vector<VariablePtr> wvars;
  // This holds a map between the 'w' variable added and indices of x vars in multilinear product
  map <VariablePtr, vector<int> > mlterms;

  // Go through multilinear rows.  Add constraints, and create maps
  for(int i = 0; i < mlcid.size(); i++) {

    const ConstraintPtr omlc = originalInstance_->getConstraint(mlcid[i]);
    const LinearFunctionPtr olf = omlc->getLinearFunction();
    const QuadraticFunctionPtr oqf = omlc->getQuadraticFunction();
    const NonlinearFunctionPtr onlf = omlc->getNonlinearFunction();
    //!!! Don't make this shared by boost, it will get confused in counting
    MultilinearFunction *omlf = dynamic_cast<MultilinearFunction *>(onlf.get());
    
    LinearFunctionPtr lf = LinearFunctionPtr(new LinearFunction());

    // Linear part of constraint remains the same
    for(ConstVariableGroupIterator it = olf->varsBegin(); it != olf->varsEnd(); ++it) {
      lf->addTerm(xvars[it->first->getId()], it->second);
    }

    // Quadratic part gets a new variable for every term
    for(ConstVariablePairGroupIterator it = oqf->begin(); it != oqf->end(); ++it) {

      vector<int> mlix;
      mlix.push_back(it->first.first->getId());
      mlix.push_back(it->first.second->getId());

      VariablePtr w = VariablePtr(new Variable(nv, -INFINITY,INFINITY, Continuous));
      nv++;
      variables_.push_back(w);
      wvars.push_back(w);
      //XXX Need to store term for evaluation
      mlterms.insert(make_pair(w,mlix));
      lf->addTerm(w,it->second);
    }

    // Multilinear part gets a new var for every term
//.........这里部分代码省略.........
开发者ID:devanandR,项目名称:minotaur,代码行数:101,代码来源:MultilinearFormulation.cpp

示例15: tightenVariableBound

bool OBBT::tightenVariableBound(ConstraintPtr cs, VariablePtr variable)
{
    assert(cs->hasVariable(variable));

    auto vars = cs->getVariables();

    // Store and set objective costs to zero
    std::vector<double> costs;

    for (auto var : vars)
    {
        costs.push_back(var->getCost());
    }

    // Set costs for lower bound problem
    for (auto var : vars)
    {
        if (var == variable)
            var->setCost(1); // Min. variable
        else
            var->setCost(0);
    }

    //SolverIpopt solver_min(cs);
    SolverGurobi solver_min(cs);
    SolverResult result_min = solver_min.optimize();

    // Set costs for upper bound problem
    for (auto var : vars)
    {
        if (var == variable)
            var->setCost(-1); // Max. variable
        else
            var->setCost(0);
    }

    //SolverIpopt solver_max(cs);
    SolverGurobi solver_max(cs);
    SolverResult result_max = solver_max.optimize();

    // Reset costs
    int counter = 0;
    for (auto var : vars)
    {
        var->setCost(costs.at(counter));
        counter++;
    }

    // Check for infeasibility
    if (result_min.status == SolverStatus::INFEASIBLE
        || result_max.status == SolverStatus::INFEASIBLE)
        return false;

    // Update lower bound
    if (result_min.status == SolverStatus::OPTIMAL)
    {
        if (!variable->updateLowerBound(result_min.objectiveValue))
        {
            // This should never happen!
            cout << "Min bound" << endl;
            cout << *variable << endl;
            cout << result_min << endl;
            return false;
        }
    }

    // Update upper bound
    if (result_max.status == SolverStatus::OPTIMAL)
    {
        if (!variable->updateUpperBound(-result_max.objectiveValue))
        {
            cout << std::setprecision(10) << -result_max.objectiveValue << endl;
            cout << std::setprecision(10) << variable->getLowerBound() << endl;
            cout << std::setprecision(10) << variable->getLowerBound() + result_max.objectiveValue << endl;
            // This should never happen!
            cout << "Max bound" << endl;
            cout << *variable << endl;
            cout << result_max << endl;
            return false;
        }
    }

    // No update
    return true;
}
开发者ID:bgrimstad,项目名称:censo,代码行数:85,代码来源:obbt.cpp


注:本文中的VariablePtr类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。