Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80630748
micpsolver_base.inl
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Sep 1, 18:46
Size
6 KB
Mime Type
text/x-c++
Expires
Tue, Sep 3, 18:46 (2 d)
Engine
blob
Format
Raw Data
Handle
20315629
Attached To
rSPECMICP SpecMiCP / ReactMiCP
micpsolver_base.inl
View Options
/*-------------------------------------------------------
- Module : micpsolver
- File : micpsolver_base.inl
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget <fabieng@princeton.edu>, Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Princeton University nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------*/
#include "micpsolver_base.hpp" // syntaxic coloration
namespace specmicp {
namespace micpsolver {
// ref : Munson et al. (2001)
template <class program_t>
void MiCPSolverBaseProgram<program_t>::scaling_jacobian(
const Matrix& jacobian,
const Vector& residuals,
Vector& rscaler,
Vector& cscaler)
{
for (int i=0; i<jacobian.cols(); ++i)
{
const double sumhsq = jacobian.row(i).array().square().sum();
double s = std::sqrt(residuals(i)*residuals(i) + sumhsq);
rscaler(i) = 1.0/std::max(s, 1e-10);
}
for (int i=0; i<jacobian.cols(); ++i)
{
const double sumhsq = (rscaler.asDiagonal()*jacobian).col(i).array().square().sum();
double s = std::sqrt(sumhsq);
cscaler(i) = 1.0/std::max(s, 1e-10);
}
}
template <class program_t>
MiCPSolverReturnCode MiCPSolverBaseProgram<program_t>::check_convergence(
int nb_iterations,
const Vector& update,
const Vector& solution,
const Vector& residuals,
bool may_have_converged
)
{
MiCPSolverReturnCode termcode = MiCPSolverReturnCode::NotConvergedYet;
const scalar_t norm_residuals = residuals.lpNorm<Eigen::Infinity>();
if (norm_residuals < get_options().fvectol)
{
if (may_have_converged == true)
termcode = MiCPSolverReturnCode::ResidualMinimized;
}
else if (nb_iterations >0 and norm_update<Eigen::Infinity>(update, solution) < get_options().steptol)
{
if (norm_residuals > get_options().threshold_stationary_point)
{
ERROR << "Stationary point detected !";
termcode = MiCPSolverReturnCode::StationaryPoint;
}
if (may_have_converged == true)
{
WARNING << "MiCP solver : Error is minimized - may indicate a stationnary point";
termcode = MiCPSolverReturnCode::ErrorMinimized;
}
}
else if (nb_iterations > get_options().max_iter)
{
ERROR << "Maximum number of iteration reached (" << get_options().max_iter << ")";
termcode = MiCPSolverReturnCode::MaxIterations;
}
else if (get_perfs().max_taken)
{
++get_perfs().nb_consecutive_max_taken;
++get_perfs().nb_max_taken;
if (get_perfs().nb_consecutive_max_taken == get_options().maxiter_maxstep) {
ERROR << "Divergence detected - Maximum step length taken two many times";
termcode = MiCPSolverReturnCode::MaxStepTakenTooManyTimes;
}
}
else
{
get_perfs().nb_consecutive_max_taken = 0;
}
return termcode;
}
template <class program_t>
void MiCPSolverBaseProgram<program_t>::reformulate_jacobian_cck(
const Vector& x,
const Vector& r,
Matrix& jacobian
)
{
// set the z vector : contains 1 for degenerate points
Eigen::VectorXd z(Eigen::VectorXd::Zero(get_neq()));
for (index_t i=get_neq_free(); i<get_neq(); ++i)
{
if (x(i) == 0 and r(i) == 0)
z(i) = 1.0;
}
// modify the jacobian
const scalar_t lambda = get_options().penalization_factor;
for (index_t i=get_neq_free(); i<get_neq(); ++i)
{
Vector grad_fi = jacobian.block(i, 0, 1, get_neq()).transpose();
if (z(i) != 0)
{
const scalar_t gpdotz = grad_fi.dot(z);
scalar_t s = std::abs(z(i)) + std::abs(gpdotz);
s = s * std::sqrt(z(i)*z(i)/(s*s) + gpdotz*gpdotz/(s*s));
const scalar_t c = lambda*(z(i)/s - 1);
const scalar_t d = lambda*(gpdotz/s -1);
grad_fi = d*grad_fi;
grad_fi(i) += c;
}
else
{
scalar_t s = std::abs(x(i)) + std::abs(r(i));
s = s * std::sqrt(x(i)*x(i)/(s*s) + r(i)*r(i)/(s*s));
scalar_t c = lambda*(x(i)/s - 1);
scalar_t d = lambda*(r(i)/s - 1);
if ((lambda <1) and (r(i) > 0) and (x(i) >0))
{
c -= (1-lambda)*r(i);
d -= (1-lambda)*x(i);
}
grad_fi = d*grad_fi;
grad_fi(i) += c;
}
jacobian.block(i, 0, 1, get_neq()) = grad_fi.transpose();
}
}
// Projection of the variables onto the feasible set
template <class program_t>
void MiCPSolverBaseProgram<program_t>::projection(Vector& x)
{
for (index_t i=0; i<get_program()->nb_complementarity_variables(); ++i)
{
if (x(i+get_program()->nb_free_variables()) < get_options().projection_min_variable)
{
x(i+get_program()->nb_free_variables()) = 0;
}
}
}
template <class program_t>
scalar_t MiCPSolverBaseProgram<program_t>::is_step_too_long(Vector& update)
{
scalar_t steplength = update.norm();
if (steplength > get_options().maxstep)
{
get_perfs().max_taken = true;
update = get_options().maxstep / steplength * update;
steplength = get_options().maxstep;
}
return steplength;
}
} // end namespace micpsolver
} // end namespace specmicp
Event Timeline
Log In to Comment