本文整理汇总了C++中SmartPtr::Scal方法的典型用法代码示例。如果您正苦于以下问题:C++ SmartPtr::Scal方法的具体用法?C++ SmartPtr::Scal怎么用?C++ SmartPtr::Scal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SmartPtr
的用法示例。
在下文中一共展示了SmartPtr::Scal方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Jnlst
bool
RestoRestorationPhase::PerformRestoration()
{
DBG_START_METH("RestoRestorationPhase::PerformRestoration",
dbg_verbosity);
Jnlst().Printf(J_DETAILED, J_MAIN,
"Performing second level restoration phase for current constriant violation %8.2e\n", IpCq().curr_constraint_violation());
DBG_ASSERT(IpCq().curr_constraint_violation()>0.);
// Get a grip on the restoration phase NLP and obtain the pointers
// to the original NLP data
SmartPtr<RestoIpoptNLP> resto_ip_nlp =
static_cast<RestoIpoptNLP*> (&IpNLP());
DBG_ASSERT(dynamic_cast<RestoIpoptNLP*> (&IpNLP()));
SmartPtr<IpoptNLP> orig_ip_nlp =
static_cast<IpoptNLP*> (&resto_ip_nlp->OrigIpNLP());
DBG_ASSERT(dynamic_cast<IpoptNLP*> (&resto_ip_nlp->OrigIpNLP()));
// Get the current point and create a new vector for the result
SmartPtr<const CompoundVector> Ccurr_x =
static_cast<const CompoundVector*> (GetRawPtr(IpData().curr()->x()));
SmartPtr<Vector> new_x = IpData().curr()->x()->MakeNew();
SmartPtr<CompoundVector> Cnew_x =
static_cast<CompoundVector*> (GetRawPtr(new_x));
// The x values remain unchanged
SmartPtr<Vector> x = Cnew_x->GetCompNonConst(0);
x->Copy(*Ccurr_x->GetComp(0));
// ToDo in free mu mode - what to do here?
Number mu = IpData().curr_mu();
// Compute the initial values for the n and p variables for the
// equality constraints
Number rho = resto_ip_nlp->Rho();
SmartPtr<Vector> nc = Cnew_x->GetCompNonConst(1);
SmartPtr<Vector> pc = Cnew_x->GetCompNonConst(2);
SmartPtr<const Vector> cvec = orig_ip_nlp->c(*Ccurr_x->GetComp(0));
SmartPtr<Vector> a = nc->MakeNew();
SmartPtr<Vector> b = nc->MakeNew();
a->Set(mu/(2.*rho));
a->Axpy(-0.5, *cvec);
b->Copy(*cvec);
b->Scal(mu/(2.*rho));
solve_quadratic(*a, *b, *nc);
pc->Copy(*cvec);
pc->Axpy(1., *nc);
DBG_PRINT_VECTOR(2, "nc", *nc);
DBG_PRINT_VECTOR(2, "pc", *pc);
// initial values for the n and p variables for the inequality
// constraints
SmartPtr<Vector> nd = Cnew_x->GetCompNonConst(3);
SmartPtr<Vector> pd = Cnew_x->GetCompNonConst(4);
SmartPtr<Vector> dvec = pd->MakeNew();
dvec->Copy(*orig_ip_nlp->d(*Ccurr_x->GetComp(0)));
dvec->Axpy(-1., *IpData().curr()->s());
a = nd->MakeNew();
b = nd->MakeNew();
a->Set(mu/(2.*rho));
a->Axpy(-0.5, *dvec);
b->Copy(*dvec);
b->Scal(mu/(2.*rho));
solve_quadratic(*a, *b, *nd);
pd->Copy(*dvec);
pd->Axpy(1., *nd);
DBG_PRINT_VECTOR(2, "nd", *nd);
DBG_PRINT_VECTOR(2, "pd", *pd);
// Now set the trial point to the solution of the restoration phase
// s and all multipliers remain unchanged
SmartPtr<IteratesVector> new_trial = IpData().curr()->MakeNewContainer();
new_trial->Set_x(*new_x);
IpData().set_trial(new_trial);
IpData().Append_info_string("R");
return true;
}
示例2: SetInitialIterates
bool RestoIterateInitializer::SetInitialIterates()
{
DBG_START_METH("RestoIterateInitializer::SetInitialIterates",
dbg_verbosity);
// Get a grip on the restoration phase NLP and obtain the pointers
// to the original NLP data
SmartPtr<RestoIpoptNLP> resto_ip_nlp =
static_cast<RestoIpoptNLP*> (&IpNLP());
SmartPtr<IpoptNLP> orig_ip_nlp =
static_cast<IpoptNLP*> (&resto_ip_nlp->OrigIpNLP());
SmartPtr<IpoptData> orig_ip_data =
static_cast<IpoptData*> (&resto_ip_nlp->OrigIpData());
SmartPtr<IpoptCalculatedQuantities> orig_ip_cq =
static_cast<IpoptCalculatedQuantities*> (&resto_ip_nlp->OrigIpCq());
// Set the value of the barrier parameter
Number resto_mu;
resto_mu = Max(orig_ip_data->curr_mu(),
orig_ip_cq->curr_c()->Amax(),
orig_ip_cq->curr_d_minus_s()->Amax());
IpData().Set_mu(resto_mu);
Jnlst().Printf(J_DETAILED, J_INITIALIZATION,
"Initial barrier parameter resto_mu = %e\n", resto_mu);
/////////////////////////////////////////////////////////////////////
// Initialize primal varialbes //
/////////////////////////////////////////////////////////////////////
// initialize the data structures in the restoration phase NLP
IpData().InitializeDataStructures(IpNLP(), false, false, false,
false, false);
SmartPtr<Vector> new_x = IpData().curr()->x()->MakeNew();
SmartPtr<CompoundVector> Cnew_x =
static_cast<CompoundVector*> (GetRawPtr(new_x));
// Set the trial x variables from the original NLP
Cnew_x->GetCompNonConst(0)->Copy(*orig_ip_data->curr()->x());
// Compute the initial values for the n and p variables for the
// equality constraints
Number rho = resto_ip_nlp->Rho();
DBG_PRINT((1,"rho = %e\n", rho));
SmartPtr<Vector> nc = Cnew_x->GetCompNonConst(1);
SmartPtr<Vector> pc = Cnew_x->GetCompNonConst(2);
SmartPtr<const Vector> cvec = orig_ip_cq->curr_c();
DBG_PRINT_VECTOR(2, "cvec", *cvec);
SmartPtr<Vector> a = nc->MakeNew();
SmartPtr<Vector> b = nc->MakeNew();
a->Set(resto_mu/(2.*rho));
a->Axpy(-0.5, *cvec);
b->Copy(*cvec);
b->Scal(resto_mu/(2.*rho));
DBG_PRINT_VECTOR(2, "a", *a);
DBG_PRINT_VECTOR(2, "b", *b);
solve_quadratic(*a, *b, *nc);
pc->Copy(*cvec);
pc->Axpy(1., *nc);
DBG_PRINT_VECTOR(2, "nc", *nc);
DBG_PRINT_VECTOR(2, "pc", *pc);
// initial values for the n and p variables for the inequality
// constraints
SmartPtr<Vector> nd = Cnew_x->GetCompNonConst(3);
SmartPtr<Vector> pd = Cnew_x->GetCompNonConst(4);
cvec = orig_ip_cq->curr_d_minus_s();
a = nd->MakeNew();
b = nd->MakeNew();
a->Set(resto_mu/(2.*rho));
a->Axpy(-0.5, *cvec);
b->Copy(*cvec);
b->Scal(resto_mu/(2.*rho));
solve_quadratic(*a, *b, *nd);
pd->Copy(*cvec);
pd->Axpy(1., *nd);
DBG_PRINT_VECTOR(2, "nd", *nd);
DBG_PRINT_VECTOR(2, "pd", *pd);
// Leave the slacks unchanged
SmartPtr<const Vector> new_s = orig_ip_data->curr()->s();
// Now set the primal trial variables
DBG_PRINT_VECTOR(2,"new_s",*new_s);
DBG_PRINT_VECTOR(2,"new_x",*new_x);
SmartPtr<IteratesVector> trial = IpData().curr()->MakeNewContainer();
trial->Set_primal(*new_x, *new_s);
IpData().set_trial(trial);
DBG_PRINT_VECTOR(2, "resto_c", *IpCq().trial_c());
DBG_PRINT_VECTOR(2, "resto_d_minus_s", *IpCq().trial_d_minus_s());
/////////////////////////////////////////////////////////////////////
// Initialize bound multipliers //
/////////////////////////////////////////////////////////////////////
SmartPtr<Vector> new_z_L = IpData().curr()->z_L()->MakeNew();
SmartPtr<CompoundVector> Cnew_z_L =
static_cast<CompoundVector*> (GetRawPtr(new_z_L));
DBG_ASSERT(IsValid(Cnew_z_L));
//.........这里部分代码省略.........
示例3: DetermineScalingParametersImpl
void GradientScaling::DetermineScalingParametersImpl(
const SmartPtr<const VectorSpace> x_space,
const SmartPtr<const VectorSpace> p_space,
const SmartPtr<const VectorSpace> c_space,
const SmartPtr<const VectorSpace> d_space,
const SmartPtr<const MatrixSpace> jac_c_space,
const SmartPtr<const MatrixSpace> jac_d_space,
const SmartPtr<const SymMatrixSpace> h_space,
const Matrix& Px_L, const Vector& x_L,
const Matrix& Px_U, const Vector& x_U,
Number& df,
SmartPtr<Vector>& dx,
SmartPtr<Vector>& dc,
SmartPtr<Vector>& dd)
{
DBG_ASSERT(IsValid(nlp_));
SmartPtr<Vector> x = x_space->MakeNew();
SmartPtr<Vector> p = p_space->MakeNew();
if (!nlp_->GetStartingPoint(GetRawPtr(x), true,
GetRawPtr(p), true,
NULL, false,
NULL, false,
NULL, false,
NULL, false)) {
THROW_EXCEPTION(FAILED_INITIALIZATION,
"Error getting initial point from NLP in GradientScaling.\n");
}
//
// Calculate grad_f scaling
//
SmartPtr<Vector> grad_f = x_space->MakeNew();
if (nlp_->Eval_grad_f(*x, *p, *grad_f)) {
double max_grad_f = grad_f->Amax();
df = 1.;
if (scaling_obj_target_gradient_ == 0.) {
if (max_grad_f > scaling_max_gradient_) {
df = scaling_max_gradient_ / max_grad_f;
}
}
else {
if (max_grad_f == 0.) {
Jnlst().Printf(J_WARNING, J_INITIALIZATION,
"Gradient of objective function is zero at starting point. Cannot determine scaling factor based on scaling_obj_target_gradient option.\n");
}
else {
df = scaling_obj_target_gradient_ / max_grad_f;
}
}
df = Max(df, scaling_min_value_);
Jnlst().Printf(J_DETAILED, J_INITIALIZATION,
"Scaling parameter for objective function = %e\n", df);
}
else {
Jnlst().Printf(J_WARNING, J_INITIALIZATION,
"Error evaluating objective gradient at user provided starting point.\n No scaling factor for objective function computed!\n");
df = 1.;
}
//
// No x scaling
//
dx = NULL;
dc = NULL;
if (c_space->Dim()>0) {
//
// Calculate c scaling
//
SmartPtr<Matrix> jac_c = jac_c_space->MakeNew();
if (nlp_->Eval_jac_c(*x, *p, *jac_c)) {
dc = c_space->MakeNew();
const double dbl_min = std::numeric_limits<double>::min();
dc->Set(dbl_min);
jac_c->ComputeRowAMax(*dc, false);
Number arow_max = dc->Amax();
if (scaling_constr_target_gradient_<=0.) {
if (arow_max > scaling_max_gradient_) {
dc->ElementWiseReciprocal();
dc->Scal(scaling_max_gradient_);
SmartPtr<Vector> dummy = dc->MakeNew();
dummy->Set(1.);
dc->ElementWiseMin(*dummy);
}
else {
dc = NULL;
}
}
else {
dc->Set(scaling_constr_target_gradient_/arow_max);
}
if (IsValid(dc) && scaling_min_value_ > 0.) {
SmartPtr<Vector> tmp = dc->MakeNew();
tmp->Set(scaling_min_value_);
dc->ElementWiseMax(*tmp);
}
}
else {
Jnlst().Printf(J_WARNING, J_INITIALIZATION,
"Error evaluating Jacobian of equality constraints at user provided starting point.\n No scaling factors for equality constraints computed!\n");
//.........这里部分代码省略.........
示例4: UnScaleIteratesVector
void SensAlgorithm::UnScaleIteratesVector(SmartPtr<IteratesVector> *V) {
// unscale the iterates vector
// pretty much a copy from IpOrigIpopt::finalize_solution
SmartPtr<const Vector> unscaled_x;
unscaled_x = IpNLP().NLP_scaling()->unapply_vector_scaling_x((*V)->x());
DBG_ASSERT(IsValid(unscaled_x));
(*V)->Set_x(*unscaled_x);
unscaled_x = NULL ;
SmartPtr<const Matrix> Px_L = IpNLP().Px_L();
SmartPtr<const Matrix> Px_U = IpNLP().Px_U();
SmartPtr<const VectorSpace> x_space = IpNLP().x_space();
SmartPtr<const Vector> y_c = (*V)->y_c();
SmartPtr<const Vector> y_d = (*V)->y_d();
SmartPtr<const Vector> z_L = (*V)->z_L();
SmartPtr<const Vector> z_U = (*V)->z_U();
// unscale y_c
SmartPtr<const Vector> unscaled_yc;
SmartPtr<const Vector> unscaled_yd;
SmartPtr<const Vector> unscaled_z_L;
SmartPtr<const Vector> unscaled_z_U;
Number obj_unscale_factor = IpNLP().NLP_scaling()->unapply_obj_scaling(1.);
if (obj_unscale_factor!=1.) {
SmartPtr<Vector> tmp = IpNLP().NLP_scaling()->apply_vector_scaling_x_LU_NonConst(*Px_L, z_L, *x_space);
tmp->Scal(obj_unscale_factor);
unscaled_z_L = ConstPtr(tmp);
tmp = IpNLP().NLP_scaling()->apply_vector_scaling_x_LU_NonConst(*Px_U, z_U, *x_space);
tmp->Scal(obj_unscale_factor);
unscaled_z_U = ConstPtr(tmp);
tmp = IpNLP().NLP_scaling()->apply_vector_scaling_c_NonConst(y_c);
tmp->Scal(obj_unscale_factor);
unscaled_yc = ConstPtr(tmp);
tmp = IpNLP().NLP_scaling()->apply_vector_scaling_d_NonConst(y_d);
tmp->Scal(obj_unscale_factor);
unscaled_yd = ConstPtr(tmp);
}
else {
unscaled_z_L = IpNLP().NLP_scaling()->apply_vector_scaling_x_LU(*Px_L, z_L, *x_space);
unscaled_z_U = IpNLP().NLP_scaling()->apply_vector_scaling_x_LU(*Px_U, z_U, *x_space);
unscaled_yc = IpNLP().NLP_scaling()->apply_vector_scaling_c(y_c);
unscaled_yd = IpNLP().NLP_scaling()->apply_vector_scaling_d(y_d);
}
(*V)->Set_z_U(*unscaled_z_U);
(*V)->Set_z_L(*unscaled_z_L);
(*V)->Set_y_c(*unscaled_yc);
(*V)->Set_y_d(*unscaled_yd);
}
示例5: InitializeStructures
//.........这里部分代码省略.........
// d_L
d_L_ = d_l_space_->MakeNewCompoundVector();
d_L_->SetComp(0, *orig_ip_nlp_->d_L());
// d_U
d_U_ = d_u_space_->MakeNewCompoundVector();
d_U_->SetComp(0, *orig_ip_nlp_->d_U());
// Px_L
Px_L_ = px_l_space_->MakeNewCompoundMatrix();
Px_L_->SetComp(0, 0, *orig_ip_nlp_->Px_L());
// Identities are auto-created (true flag passed into SetCompSpace)
// Px_U
Px_U_ = px_u_space_->MakeNewCompoundMatrix();
Px_U_->SetComp(0, 0, *orig_ip_nlp_->Px_U());
// Remaining matrices will be zero'ed out
// Pd_L
//Pd_L_ = orig_ip_nlp_->Pd_L();
Pd_L_ = pd_l_space_->MakeNewCompoundMatrix();
Pd_L_->SetComp(0, 0, *orig_ip_nlp_->Pd_L());
// Pd_U
//Pd_U_ = orig_ip_nlp_->Pd_U();
Pd_U_ = pd_u_space_->MakeNewCompoundMatrix();
Pd_U_->SetComp(0, 0, *orig_ip_nlp_->Pd_U());
// Getting the NLP scaling
SmartPtr<const MatrixSpace> scaled_jac_c_space;
SmartPtr<const MatrixSpace> scaled_jac_d_space;
SmartPtr<const SymMatrixSpace> scaled_h_space;
NLP_scaling()->DetermineScaling(GetRawPtr(x_space_), c_space_, d_space_, GetRawPtr(jac_c_space_),
GetRawPtr(jac_d_space_), GetRawPtr(h_space_), scaled_jac_c_space, scaled_jac_d_space, scaled_h_space, *Px_L_,
*x_L_, *Px_U_, *x_U_);
// For now we assume that no scaling is done inside the NLP_Scaling
DBG_ASSERT(scaled_jac_c_space == jac_c_space_); DBG_ASSERT(scaled_jac_d_space == jac_d_space_); DBG_ASSERT(scaled_h_space == h_space_);
/////////////////////////////////////////////////////////////////////////
// Create and initialize the vectors for the restoration phase problem //
/////////////////////////////////////////////////////////////////////////
// Vector x
SmartPtr<CompoundVector> comp_x = x_space_->MakeNewCompoundVector();
if( init_x )
{
comp_x->GetCompNonConst(0)->Copy(*orig_ip_data_->curr()->x());
comp_x->GetCompNonConst(1)->Set(1.0);
comp_x->GetCompNonConst(2)->Set(1.0);
comp_x->GetCompNonConst(3)->Set(1.0);
comp_x->GetCompNonConst(4)->Set(1.0);
}
x = GetRawPtr(comp_x);
// Vector y_c
y_c = c_space_->MakeNew();
if( init_y_c )
{
y_c->Set(0.0); // ToDo
}
// Vector y_d
y_d = d_space_->MakeNew();
if( init_y_d )
{