26 namespace pagmo {
namespace algorithm {
29 void no_screen_output (
int,
const char []) {}
30 void default_output(
int mode,
const char *message)
32 if (mode & WORHP_PRINT_MESSAGE) {
33 printf(
" %s\n",message);
35 if (mode & WORHP_PRINT_WARNING) {
36 printf(
" %s\n",message);
38 if (mode & WORHP_PRINT_ERROR) {
39 fprintf(stderr,
" %s\n",message);
47 worhp::worhp(
const int iter,
const double feas,
const double opt,
const bool screen_output)
53 set_screen_output(screen_output);
56 setenv(
"WORHP_DISABLE_KEYBOARD_HANDLER",
"1", 0);
60 if (m_screen_output) {
61 SetWorhpPrint(default_output);
63 SetWorhpPrint(no_screen_output);
69 ReadParams(&status, const_cast<char*>(
"param.xml"), &m_params);
70 m_params.MatrixCC =
false;
73 set_param(
"TolFeas", feas);
74 set_param(
"TolOpti", opt);
75 set_param(
"MaxIter", iter);
85 if (pop.size() == 0) {
89 const auto& prob = pop.problem();
91 if (prob.get_i_dimension() != 0) {
92 pagmo_throw(value_error,
93 "The problem has an integer part and WORHP is not suitable to solve it.");
96 if (prob.get_f_dimension() != 1) {
97 pagmo_throw(value_error,
98 "The problem is not single objective and WORHP is not suitable to solve it");
102 if (m_screen_output) {
103 SetWorhpPrint(default_output);
105 SetWorhpPrint(no_screen_output);
112 WorhpPreInit(&opt, &workspace, ¶ms, &control);
114 opt.n = prob.get_dimension();
115 opt.m = prob.get_c_dimension();
116 auto n_eq = prob.get_c_dimension() - prob.get_ic_dimension();
119 workspace.DF.nnz = opt.n;
120 workspace.DG.nnz = opt.n * opt.m;
121 workspace.HM.nnz = opt.n;
123 WorhpInit(&opt, &workspace, ¶ms, &control);
124 assert(control.status == FirstCall);
128 params.UserDF =
false;
129 params.UserDG =
false;
130 params.UserHM =
false;
131 params.UserHMstructure =
false;
134 const auto best_idx = pop.get_best_idx();
137 for (
int i = 0; i < opt.n; ++i) {
140 opt.XL[i] = prob.get_lb()[i];
141 opt.XU[i] = prob.get_ub()[i];
145 for (
auto i = 0u; i < n_eq; ++i) {
152 for (
auto i = n_eq; i < unsigned(opt.m); ++i) {
154 opt.GL[i] = -params.Infty;
158 while (control.status < TerminateSuccess && control.status > TerminateError) {
159 if (GetUserAction(&control, callWorhp)) {
160 Worhp(&opt, &workspace, ¶ms, &control);
163 if (GetUserAction(&control, iterOutput))
165 IterationOutput(&opt, &workspace, ¶ms, &control);
166 DoneUserAction(&control, iterOutput);
169 if (GetUserAction(&control, evalF)) {
170 for (
int i = 0; i < opt.n; ++i) {
173 auto f = prob.objfun(x);
174 opt.F = workspace.ScaleObj * f[0];
175 DoneUserAction(&control, evalF);
178 if (GetUserAction(&control, evalG)) {
179 for (
int i = 0; i < opt.n; ++i) {
182 auto g = prob.compute_constraints(x);
183 for (
int i = 0; i < opt.m; ++i) {
186 DoneUserAction(&control, evalG);
189 if (GetUserAction(&control, fidif)) {
190 WorhpFidif(&opt, &workspace, ¶ms, &control);
194 for (
int i = 0; i < opt.n; ++i) {
197 pop.set_x(best_idx, x);
199 StatusMsg(&opt, &workspace, ¶ms, &control);
200 WorhpFree(&opt, &workspace, ¶ms, &control);
210 std::string worhp::get_name()
const
219 std::string worhp::human_readable_extra()
const
221 std::ostringstream s;
222 s <<
"MaxIter:" << get_param(
"MaxIter") <<
" TolFeas:"<<get_param(
"TolFeas")<<
" TolOpti:"<< get_param(
"TolOpti") << std::endl;
228 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::algorithm::worhp)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
decision_vector cur_x
Current decision vector.