51 #ifndef ILQGAMES_CONSTRAINT_CONSTRAINT_H 52 #define ILQGAMES_CONSTRAINT_CONSTRAINT_H 54 #include <ilqgames/cost/cost.h> 55 #include <ilqgames/utils/types.h> 57 #include <glog/logging.h> 69 bool IsSatisfied(Time t,
const VectorXf& input,
float* level)
const {
70 const float value = Evaluate(t, input);
71 if (level) *level = value;
73 return IsSatisfied(value);
75 bool IsSatisfied(
float level)
const {
76 return (is_equality_) ? std::abs(level) <= constants::kSmallNumber
77 : level <= constants::kSmallNumber;
82 virtual float Evaluate(Time t,
const VectorXf& input)
const = 0;
83 float EvaluateAugmentedLagrangian(Time t,
const VectorXf& input)
const {
84 const float g = Evaluate(t, input);
85 const float lambda = lambdas_[TimeIndex(t)];
86 return lambda * g + 0.5 * Mu(lambda, g) * g * g;
91 virtual void Quadraticize(Time t,
const VectorXf& input, MatrixXf* hess,
92 VectorXf* grad)
const = 0;
95 bool IsEquality()
const {
return is_equality_; }
96 float& Lambda(Time t) {
return lambdas_[TimeIndex(t)]; }
97 float Lambda(Time t)
const {
return lambdas_[TimeIndex(t)]; }
98 void IncrementLambda(Time t,
float value) {
99 const size_t kk = TimeIndex(t);
100 const float new_lambda = lambdas_[kk] + mu_ * value;
101 lambdas_[kk] = (is_equality_) ? new_lambda : std::max(0.0f, new_lambda);
103 void ScaleLambdas(
float scale) {
104 for (
auto& lambda : lambdas_) lambda *= scale;
106 static float& GlobalMu() {
return mu_; }
107 static void ScaleMu(
float scale) { mu_ *= scale; }
108 float Mu(Time t,
const VectorXf& input)
const {
109 const float g = Evaluate(t, input);
110 return Mu(Lambda(t), g);
112 float Mu(
float lambda,
float g)
const {
113 if (!is_equality_ && g <= constants::kSmallNumber &&
114 std::abs(lambda) <= constants::kSmallNumber)
120 explicit Constraint(
bool is_equality,
const std::string& name)
122 is_equality_(is_equality),
123 lambdas_(time::kNumTimeSteps, constants::kDefaultLambda) {}
129 void ModifyDerivatives(Time t,
float g,
float* dx,
float* ddx,
130 float* dy =
nullptr,
float* ddy =
nullptr,
131 float* dxdy =
nullptr)
const;
138 std::vector<float> lambdas_;