1 // -*- C++ -*-
2 /**
3 * @brief The test file of class Cobyla for standard methods
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include "openturns/OT.hxx"
22 #include "openturns/OTtestcode.hxx"
23
24 using namespace OT;
25 using namespace OT::Test;
26
printPoint(const Point & point,const UnsignedInteger digits)27 inline String printPoint(const Point & point, const UnsignedInteger digits)
28 {
29 OSS oss;
30 oss << "[";
31 Scalar eps = pow(0.1, 1.0 * digits);
32 for (UnsignedInteger i = 0; i < point.getDimension(); i++)
33 {
34 oss << std::fixed << std::setprecision(digits) << (i == 0 ? "" : ",") << Bulk<double>((std::abs(point[i]) < eps) ? std::abs(point[i]) : point[i]);
35 }
36 oss << "]";
37 return oss;
38 }
39
main(int,char * [])40 int main(int, char *[])
41 {
42 TESTPREAMBLE;
43 OStream fullprint(std::cout);
44
45 try
46 {
47
48 // Test function operator ()
49 Description input(4);
50 input[0] = "x1";
51 input[1] = "x2";
52 input[2] = "x3";
53 input[3] = "x4";
54 SymbolicFunction levelFunction(input, Description(1, "x1+2*x2-3*x3+4*x4"));
55 Point startingPoint(4, 0.0);
56 Cobyla myAlgorithm(NearestPointProblem(levelFunction, 3.0));
57 myAlgorithm.setStartingPoint(startingPoint);
58 fullprint << "myAlgorithm = " << myAlgorithm << std::endl;
59 myAlgorithm.run();
60 fullprint << "result = " << printPoint(myAlgorithm.getResult().getOptimalPoint(), 4) << std::endl;
61 fullprint << "multipliers = " << printPoint(myAlgorithm.getResult().computeLagrangeMultipliers(), 4) << std::endl;
62 }
63 catch (TestFailed & ex)
64 {
65 std::cerr << ex << std::endl;
66 return ExitCode::Error;
67 }
68
69 try
70 {
71 Description input(4);
72 input[0] = "x1";
73 input[1] = "x2";
74 input[2] = "x3";
75 input[3] = "x4";
76 SymbolicFunction levelFunction(input, Description(1, "x1*cos(x1)+2*x2*x3-3*x3+4*x3*x4"));
77 Point startingPoint(4, 0.0);
78 Cobyla myAlgorithm(NearestPointProblem(levelFunction, 3.0));
79 myAlgorithm.setStartingPoint(startingPoint);
80 myAlgorithm.setMaximumEvaluationNumber(400);
81 myAlgorithm.setMaximumAbsoluteError(1.0e-10);
82 myAlgorithm.setMaximumRelativeError(1.0e-10);
83 myAlgorithm.setMaximumResidualError(1.0e-10);
84 myAlgorithm.setMaximumConstraintError(1.0e-10);
85 fullprint << "myAlgorithm = " << myAlgorithm << std::endl;
86 myAlgorithm.run();
87 OptimizationResult result(myAlgorithm.getResult());
88 fullprint << "result = " << printPoint(result.getOptimalPoint(), 4) << std::endl;
89 fullprint << "multipliers = " << printPoint(result.computeLagrangeMultipliers(), 4) << std::endl;
90 Graph convergence(result.drawErrorHistory());
91 //FIXME:fullprint << "evaluation calls number=" << levelFunction.getEvaluationCallsNumber() << std::endl;
92 fullprint << "gradient calls number=" << levelFunction.getGradientCallsNumber() << std::endl;
93 fullprint << "hessian calls number=" << levelFunction.getHessianCallsNumber() << std::endl;
94 }
95 catch (TestFailed & ex)
96 {
97 std::cerr << ex << std::endl;
98 return ExitCode::Error;
99 }
100
101 return ExitCode::Success;
102 }
103