1+ /* *****************************************************************************
2+ Copyright (c) 2017, Farbod Farshidian. All rights reserved.
3+
4+ Redistribution and use in source and binary forms, with or without
5+ modification, are permitted provided that the following conditions are met:
6+
7+ * Redistributions of source code must retain the above copyright notice, this
8+ list of conditions and the following disclaimer.
9+
10+ * Redistributions in binary form must reproduce the above copyright notice,
11+ this list of conditions and the following disclaimer in the documentation
12+ and/or other materials provided with the distribution.
13+
14+ * Neither the name of the copyright holder nor the names of its
15+ contributors may be used to endorse or promote products derived from
16+ this software without specific prior written permission.
17+
18+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+ ******************************************************************************/
29+
130#include < gtest/gtest.h>
231#include < cstdlib>
332#include < ctime>
736#include " ocs2_ipm/IpmSolver.h"
837
938#include < ocs2_core/initialization/DefaultInitializer.h>
10-
1139#include < ocs2_oc/test/EXP0.h>
1240
1341using namespace ocs2 ;
@@ -75,24 +103,26 @@ TEST(Exp0Test, Unconstrained) {
75103 static constexpr size_t INPUT_DIM = 1 ;
76104
77105 // Solver settings
78- ipm::Settings settings;
79- settings.dt = 0.01 ;
80- settings.ipmIteration = 20 ;
81- settings.projectStateInputEqualityConstraints = true ;
82- settings.useFeedbackPolicy = true ;
83- settings.printSolverStatistics = true ;
84- settings.printSolverStatus = true ;
85- settings.printLinesearch = true ;
86- settings.printSolverStatistics = true ;
87- settings.printSolverStatus = true ;
88- settings.printLinesearch = true ;
89- settings.nThreads = 1 ;
90-
91- settings.initialBarrierParameter = 1.0e-02 ;
92- settings.targetBarrierParameter = 1.0e-04 ;
93- settings.barrierLinearDecreaseFactor = 0.2 ;
94- settings.barrierSuperlinearDecreasePower = 1.5 ;
95- settings.fractionToBoundaryMargin = 0.995 ;
106+ const auto settings = []() {
107+ ipm::Settings s;
108+ s.dt = 0.01 ;
109+ s.ipmIteration = 20 ;
110+ s.projectStateInputEqualityConstraints = true ;
111+ s.useFeedbackPolicy = true ;
112+ s.printSolverStatistics = true ;
113+ s.printSolverStatus = true ;
114+ s.printLinesearch = true ;
115+ s.printSolverStatistics = true ;
116+ s.printSolverStatus = true ;
117+ s.printLinesearch = true ;
118+ s.nThreads = 1 ;
119+ s.initialBarrierParameter = 1.0e-02 ;
120+ s.targetBarrierParameter = 1.0e-04 ;
121+ s.barrierLinearDecreaseFactor = 0.2 ;
122+ s.barrierSuperlinearDecreasePower = 1.5 ;
123+ s.fractionToBoundaryMargin = 0.995 ;
124+ return s;
125+ }();
96126
97127 const scalar_array_t initEventTimes{0.1897 };
98128 const size_array_t modeSequence{0 , 1 };
@@ -109,41 +139,33 @@ TEST(Exp0Test, Unconstrained) {
109139 IpmSolver solver (settings, problem, zeroInitializer);
110140 solver.setReferenceManager (referenceManagerPtr);
111141 solver.run (startTime, initState, finalTime);
112- solver.run (startTime, initState, finalTime);
113- solver.run (startTime, initState, finalTime);
114-
115- const auto primalSolution = solver.primalSolution (finalTime);
116- std::cout << " Optimal unconstrained trajectory" << std::endl;
117- for (int i = 0 ; i < primalSolution.timeTrajectory_ .size (); i++) {
118- std::cout << " time: " << std::setprecision (4 ) << primalSolution.timeTrajectory_ [i]
119- << " \t state: " << primalSolution.stateTrajectory_ [i].transpose ()
120- << " \t input: " << primalSolution.inputTrajectory_ [i].transpose () << std::endl;
121- }
122142}
123143
124144TEST (Exp0Test, Constrained) {
125145 static constexpr size_t STATE_DIM = 2 ;
126146 static constexpr size_t INPUT_DIM = 1 ;
127147
128148 // Solver settings
129- ipm::Settings settings;
130- settings.dt = 0.01 ;
131- settings.ipmIteration = 20 ;
132- settings.projectStateInputEqualityConstraints = true ;
133- settings.useFeedbackPolicy = true ;
134- settings.printSolverStatistics = true ;
135- settings.printSolverStatus = true ;
136- settings.printLinesearch = true ;
137- settings.printSolverStatistics = true ;
138- settings.printSolverStatus = true ;
139- settings.printLinesearch = true ;
140- settings.nThreads = 1 ;
141-
142- settings.initialBarrierParameter = 1.0e-02 ;
143- settings.targetBarrierParameter = 1.0e-04 ;
144- settings.barrierLinearDecreaseFactor = 0.2 ;
145- settings.barrierSuperlinearDecreasePower = 1.5 ;
146- settings.fractionToBoundaryMargin = 0.995 ;
149+ const auto settings = []() {
150+ ipm::Settings s;
151+ s.dt = 0.01 ;
152+ s.ipmIteration = 20 ;
153+ s.projectStateInputEqualityConstraints = true ;
154+ s.useFeedbackPolicy = true ;
155+ s.printSolverStatistics = true ;
156+ s.printSolverStatus = true ;
157+ s.printLinesearch = true ;
158+ s.printSolverStatistics = true ;
159+ s.printSolverStatus = true ;
160+ s.printLinesearch = true ;
161+ s.nThreads = 1 ;
162+ s.initialBarrierParameter = 1.0e-02 ;
163+ s.targetBarrierParameter = 1.0e-04 ;
164+ s.barrierLinearDecreaseFactor = 0.2 ;
165+ s.barrierSuperlinearDecreasePower = 1.5 ;
166+ s.fractionToBoundaryMargin = 0.995 ;
167+ return s;
168+ }();
147169
148170 const scalar_array_t initEventTimes{0.1897 };
149171 const size_array_t modeSequence{0 , 1 };
@@ -153,12 +175,12 @@ TEST(Exp0Test, Constrained) {
153175 // add inequality constraints
154176 const scalar_t umin = -7.5 ;
155177 const scalar_t umax = 7.5 ;
156- std::unique_ptr<StateInputConstraint> stateInputIneqConstraint ( new EXP0_StateInputIneqConstraints ( umin, umax) );
178+ auto stateInputIneqConstraint = std::make_unique<EXP0_StateInputIneqConstraints>( umin, umax);
157179 problem.inequalityConstraintPtr ->add (" ubound" , std::move (stateInputIneqConstraint));
158180 const vector_t xmin = (vector_t (2 ) << -7.5 , -7.5 ).finished ();
159181 const vector_t xmax = (vector_t (2 ) << 7.5 , 7.5 ).finished ();
160- std::unique_ptr<StateConstraint> stateIneqConstraint ( new EXP0_StateIneqConstraints ( xmin, xmax) );
161- std::unique_ptr<StateConstraint> finalStateIneqConstraint ( new EXP0_StateIneqConstraints ( xmin, xmax) );
182+ auto stateIneqConstraint = std::make_unique<EXP0_StateIneqConstraints>( xmin, xmax);
183+ auto finalStateIneqConstraint = std::make_unique<EXP0_StateIneqConstraints>( xmin, xmax);
162184 problem.stateInequalityConstraintPtr ->add (" xbound" , std::move (stateIneqConstraint));
163185 problem.finalInequalityConstraintPtr ->add (" xbound" , std::move (finalStateIneqConstraint));
164186
@@ -172,29 +194,22 @@ TEST(Exp0Test, Constrained) {
172194 IpmSolver solver (settings, problem, zeroInitializer);
173195 solver.setReferenceManager (referenceManagerPtr);
174196 solver.run (startTime, initState, finalTime);
175- // std::cout << solver.getBenchmarkingInformation() << std::endl;
176197
177198 const auto primalSolution = solver.primalSolution (finalTime);
178- std::cout << " Optimal unconstrained trajectory" << std::endl;
179- for (int i = 0 ; i < primalSolution.timeTrajectory_ .size (); i++) {
180- std::cout << " time: " << std::setprecision (4 ) << primalSolution.timeTrajectory_ [i]
181- << " \t state: " << primalSolution.stateTrajectory_ [i].transpose ()
182- << " \t input: " << primalSolution.inputTrajectory_ [i].transpose () << std::endl;
183- }
184199
185200 // check constraint satisfaction
186201 for (const auto & e : primalSolution.stateTrajectory_ ) {
187202 if (e.size () > 0 ) {
188- EXPECT_TRUE (e.coeff (0 ) >= xmin.coeff (0 ));
189- EXPECT_TRUE (e.coeff (1 ) >= xmin.coeff (1 ));
190- EXPECT_TRUE (e.coeff (0 ) <= xmax.coeff (0 ));
191- EXPECT_TRUE (e.coeff (1 ) <= xmax.coeff (1 ));
203+ ASSERT_TRUE (e.coeff (0 ) >= xmin.coeff (0 ));
204+ ASSERT_TRUE (e.coeff (1 ) >= xmin.coeff (1 ));
205+ ASSERT_TRUE (e.coeff (0 ) <= xmax.coeff (0 ));
206+ ASSERT_TRUE (e.coeff (1 ) <= xmax.coeff (1 ));
192207 }
193208 }
194209 for (const auto & e : primalSolution.inputTrajectory_ ) {
195210 if (e.size () > 0 ) {
196- EXPECT_TRUE (e.coeff (0 ) >= umin);
197- EXPECT_TRUE (e.coeff (0 ) <= umax);
211+ ASSERT_TRUE (e.coeff (0 ) >= umin);
212+ ASSERT_TRUE (e.coeff (0 ) <= umax);
198213 }
199214 }
200215}
0 commit comments