1 // Copyright (C) 2004, 2006 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Common Public License.
4 //
5 // $Id: IpOrigIterationOutput.cpp 759 2006-07-07 03:07:08Z andreasw $
6 //
7 // Authors:  Carl Laird, Andreas Waechter              IBM    2004-09-23
8 
9 #include "IpOrigIterationOutput.hpp"
10 
11 #ifdef HAVE_CMATH
12 # include <cmath>
13 #else
14 # ifdef HAVE_MATH_H
15 #  include <math.h>
16 # else
17 #  error "don't have header file for math"
18 # endif
19 #endif
20 
21 #include <cstdio>
22 
23 // Keeps MS VC++ 8 quiet about sprintf, strcpy, etc.
24 #ifdef _MSC_VER
25 #pragma warning(disable:4996)
26 #endif
27 
28 
29 
30 
31 namespace SimTKIpopt
32 {
OrigIterationOutput()33   OrigIterationOutput::OrigIterationOutput()
34   {}
35 
~OrigIterationOutput()36   OrigIterationOutput::~OrigIterationOutput()
37   {}
38 
39   void
RegisterOptions(SmartPtr<RegisteredOptions> roptions)40   OrigIterationOutput::RegisterOptions(SmartPtr<RegisteredOptions> roptions)
41   {
42     std::string prev_cat = roptions->RegisteringCategory();
43     roptions->SetRegisteringCategory("Output");
44     roptions->AddStringOption2(
45       "print_info_string",
46       "Enables printing of additional info string at end of iteration output.",
47       "no",
48       "no", "don't print string",
49       "yes", "print string at end of each iteration output",
50       "This string contains some insider information about the current iteration.");
51     roptions->SetRegisteringCategory(prev_cat);
52   }
53 
InitializeImpl(const OptionsList & options,const std::string & prefix)54   bool OrigIterationOutput::InitializeImpl(const OptionsList& options,
55       const std::string& prefix)
56   {
57     options.GetBoolValue("print_info_string", print_info_string_, prefix);
58 
59     return true;
60   }
61 
WriteOutput()62   void OrigIterationOutput::WriteOutput()
63   {
64     //////////////////////////////////////////////////////////////////////
65     //         First print the summary line for the iteration           //
66     //////////////////////////////////////////////////////////////////////
67 
68     Index iter = IpData().iter_count();
69     std::string header =
70       "iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls\n";
71     Jnlst().Printf(J_DETAILED, J_MAIN,
72                    "\n\n**************************************************\n");
73     Jnlst().Printf(J_DETAILED, J_MAIN,
74                    "*** Summary of Iteration: %d:", IpData().iter_count());
75     Jnlst().Printf(J_DETAILED, J_MAIN,
76                    "\n**************************************************\n\n");
77     if (iter%10 == 0 && !IpData().info_skip_output()) {
78       // output the header
79       Jnlst().Printf(J_ITERSUMMARY, J_MAIN, header.c_str());
80     }
81     else {
82       Jnlst().Printf(J_DETAILED, J_MAIN, header.c_str());
83     }
84     Number inf_pr = IpCq().curr_primal_infeasibility(NORM_MAX);
85     Number inf_du = IpCq().curr_dual_infeasibility(NORM_MAX);
86     Number mu = IpData().curr_mu();
87     Number dnrm;
88     if (IsValid(IpData().delta()) && IsValid(IpData().delta()->x()) && IsValid(IpData().delta()->s())) {
89       dnrm = Max(IpData().delta()->x()->Amax(), IpData().delta()->s()->Amax());
90     }
91     else {
92       // This is the first iteration - no search direction has been
93       // computed yet.
94       dnrm = 0.;
95     }
96     Number unscaled_f = IpCq().unscaled_curr_f();
97 
98     // Retrieve some information set in the different parts of the algorithm
99     char info_iter=' ';
100     Number alpha_primal = IpData().info_alpha_primal();
101     char alpha_primal_char = IpData().info_alpha_primal_char();
102     Number alpha_dual = IpData().info_alpha_dual();
103     Number regu_x = IpData().info_regu_x();
104     char regu_x_buf[8];
105     char dashes[]="   - ";
106     char *regu_x_ptr;
107     if (regu_x==.0) {
108       regu_x_ptr = dashes;
109     }
110     else {
111       sprintf(regu_x_buf, "%5.1f", log10(regu_x));
112       regu_x_ptr = regu_x_buf;
113     }
114     Index ls_count = IpData().info_ls_count();
115     const std::string info_string = IpData().info_string();
116 
117     if (!IpData().info_skip_output()) {
118       Jnlst().Printf(J_ITERSUMMARY, J_MAIN,
119                      "%4d%c%14.7e %7.2e %7.2e %5.1f %7.2e %5s %7.2e %7.2e%c%3d",
120                      iter, info_iter, unscaled_f, inf_pr, inf_du, log10(mu), dnrm, regu_x_ptr,
121                      alpha_dual, alpha_primal, alpha_primal_char,
122                      ls_count);
123       if (print_info_string_) {
124         Jnlst().Printf(J_ITERSUMMARY, J_MAIN, " %s", info_string.c_str());
125       }
126       else {
127 // TODO UNCOMMENT WHEN DONE TESTING
128         Jnlst().Printf(J_DETAILED, J_MAIN, " %s", info_string.c_str());
129 //printf(" %s",info_string.c_str());
130       }
131       Jnlst().Printf(J_ITERSUMMARY, J_MAIN, "\n");
132     }
133 
134 
135     //////////////////////////////////////////////////////////////////////
136     //           Now if desired more detail on the iterates             //
137     //////////////////////////////////////////////////////////////////////
138 
139     if (Jnlst().ProduceOutput(J_DETAILED, J_MAIN)) {
140       Jnlst().Printf(J_DETAILED, J_MAIN,
141                      "\n**************************************************\n");
142       Jnlst().Printf(J_DETAILED, J_MAIN,
143                      "*** Beginning Iteration %d from the following point:",
144                      IpData().iter_count());
145       Jnlst().Printf(J_DETAILED, J_MAIN,
146                      "\n**************************************************\n\n");
147 
148       Jnlst().Printf(J_DETAILED, J_MAIN,
149                      "Current barrier parameter mu = %21.16e\n", IpData().curr_mu());
150       Jnlst().Printf(J_DETAILED, J_MAIN,
151                      "Current fraction-to-the-boundary parameter tau = %21.16e\n\n",
152                      IpData().curr_tau());
153       Jnlst().Printf(J_DETAILED, J_MAIN,
154                      "||curr_x||_inf   = %.16e\n", IpData().curr()->x()->Amax());
155       Jnlst().Printf(J_DETAILED, J_MAIN,
156                      "||curr_s||_inf   = %.16e\n", IpData().curr()->s()->Amax());
157       Jnlst().Printf(J_DETAILED, J_MAIN,
158                      "||curr_y_c||_inf = %.16e\n", IpData().curr()->y_c()->Amax());
159       Jnlst().Printf(J_DETAILED, J_MAIN,
160                      "||curr_y_d||_inf = %.16e\n", IpData().curr()->y_d()->Amax());
161       Jnlst().Printf(J_DETAILED, J_MAIN,
162                      "||curr_z_L||_inf = %.16e\n", IpData().curr()->z_L()->Amax());
163       Jnlst().Printf(J_DETAILED, J_MAIN,
164                      "||curr_z_U||_inf = %.16e\n", IpData().curr()->z_U()->Amax());
165       Jnlst().Printf(J_DETAILED, J_MAIN,
166                      "||curr_v_L||_inf = %.16e\n", IpData().curr()->v_L()->Amax());
167       Jnlst().Printf(J_DETAILED, J_MAIN,
168                      "||curr_v_U||_inf = %.16e\n", IpData().curr()->v_U()->Amax());
169     }
170     if (Jnlst().ProduceOutput(J_MOREDETAILED, J_MAIN)) {
171       if (IsValid(IpData().delta())) {
172         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
173                        "\n||delta_x||_inf   = %.16e\n", IpData().delta()->x()->Amax());
174         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
175                        "||delta_s||_inf   = %.16e\n", IpData().delta()->s()->Amax());
176         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
177                        "||delta_y_c||_inf = %.16e\n", IpData().delta()->y_c()->Amax());
178         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
179                        "||delta_y_d||_inf = %.16e\n", IpData().delta()->y_d()->Amax());
180         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
181                        "||delta_z_L||_inf = %.16e\n", IpData().delta()->z_L()->Amax());
182         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
183                        "||delta_z_U||_inf = %.16e\n", IpData().delta()->z_U()->Amax());
184         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
185                        "||delta_v_L||_inf = %.16e\n", IpData().delta()->v_L()->Amax());
186         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
187                        "||delta_v_U||_inf = %.16e\n", IpData().delta()->v_U()->Amax());
188       }
189       else {
190         Jnlst().Printf(J_MOREDETAILED, J_MAIN,
191                        "\nNo search direction has been computed yet.\n");
192       }
193     }
194     if (Jnlst().ProduceOutput(J_VECTOR, J_MAIN)) {
195       IpData().curr()->x()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_x");
196       IpData().curr()->s()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_s");
197 
198       IpData().curr()->y_c()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_y_c");
199       IpData().curr()->y_d()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_y_d");
200 
201       IpCq().curr_slack_x_L()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_slack_x_L");
202       IpCq().curr_slack_x_U()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_slack_x_U");
203       IpData().curr()->z_L()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_z_L");
204       IpData().curr()->z_U()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_z_U");
205 
206       IpCq().curr_slack_s_L()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_slack_s_L");
207       IpCq().curr_slack_s_U()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_slack_s_U");
208       IpData().curr()->v_L()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_v_L");
209       IpData().curr()->v_U()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_v_U");
210     }
211     if (Jnlst().ProduceOutput(J_MOREVECTOR, J_MAIN)) {
212       IpCq().curr_grad_lag_x()->Print(Jnlst(), J_MOREVECTOR, J_MAIN, "curr_grad_lag_x");
213       IpCq().curr_grad_lag_s()->Print(Jnlst(), J_MOREVECTOR, J_MAIN, "curr_grad_lag_s");
214       if (IsValid(IpData().delta())) {
215         IpData().delta()->Print(Jnlst(), J_MOREVECTOR, J_MAIN, "delta");
216       }
217     }
218 
219     if (Jnlst().ProduceOutput(J_DETAILED, J_MAIN)) {
220       Jnlst().Printf(J_DETAILED, J_MAIN,
221                      "\n\n***Current NLP Values for Iteration %d:\n",
222                      IpData().iter_count());
223       Jnlst().Printf(J_DETAILED, J_MAIN, "\n                                   (scaled)                 (unscaled)\n");
224       Jnlst().Printf(J_DETAILED, J_MAIN, "Objective...............: %24.16e  %24.16e\n", IpCq().curr_f(), IpCq().unscaled_curr_f());
225       Jnlst().Printf(J_DETAILED, J_MAIN, "Dual infeasibility......: %24.16e  %24.16e\n", IpCq().curr_dual_infeasibility(NORM_MAX), IpCq().unscaled_curr_dual_infeasibility(NORM_MAX));
226       Jnlst().Printf(J_DETAILED, J_MAIN, "Constraint violation....: %24.16e  %24.16e\n", IpCq().curr_nlp_constraint_violation(NORM_MAX), IpCq().unscaled_curr_nlp_constraint_violation(NORM_MAX));
227       Jnlst().Printf(J_DETAILED, J_MAIN, "Complementarity.........: %24.16e  %24.16e\n", IpCq().curr_complementarity(0., NORM_MAX), IpCq().unscaled_curr_complementarity(0., NORM_MAX));
228       Jnlst().Printf(J_DETAILED, J_MAIN, "Overall NLP error.......: %24.16e  %24.16e\n\n", IpCq().curr_nlp_error(), IpCq().unscaled_curr_nlp_error());
229     }
230     if (Jnlst().ProduceOutput(J_VECTOR, J_MAIN)) {
231       IpCq().curr_grad_f()->Print(Jnlst(), J_VECTOR, J_MAIN, "grad_f");
232       IpCq().curr_c()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_c");
233       IpCq().curr_d()->Print(Jnlst(), J_VECTOR, J_MAIN, "curr_d");
234       IpCq().curr_d_minus_s()->Print(Jnlst(), J_VECTOR, J_MAIN,
235                                      "curr_d - curr_s");
236     }
237 
238     if (Jnlst().ProduceOutput(J_MATRIX, J_MAIN)) {
239       IpCq().curr_jac_c()->Print(Jnlst(), J_MATRIX, J_MAIN, "jac_c");
240       IpCq().curr_jac_d()->Print(Jnlst(), J_MATRIX, J_MAIN, "jac_d");
241       IpData().W()->Print(Jnlst(), J_MATRIX, J_MAIN, "W");
242     }
243 
244     Jnlst().Printf(J_DETAILED, J_MAIN, "\n\n");
245   }
246 
247 } // namespace Ipopt
248