summaryrefslogtreecommitdiff
path: root/newstructure/thirdparty/linux/include/coin/AmplTNLP.hpp
blob: a8c823ae099c43c0402acddb2d2c03cc06db56c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright (C) 2004, 2009 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: AmplTNLP.hpp 2242 2013-04-24 19:26:30Z stefan $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPAMPLTNLP_HPP__
#define __IPAMPLTNLP_HPP__

#include "IpUtils.hpp"
#include "IpTNLP.hpp"
#include "IpJournalist.hpp"
#include "IpOptionsList.hpp"

#include <map>
#include <string>

/* non Ipopt forward declaration */
struct ASL_pfgh;
struct SufDecl;
struct SufDesc;

namespace Ipopt
{
  class AmplSuffixHandler : public ReferencedObject
  {
  public:
    AmplSuffixHandler();

    ~AmplSuffixHandler();

    enum Suffix_Type
    {
      Index_Type,
      Number_Type
    };

    enum Suffix_Source
    {
      Variable_Source,
      Constraint_Source,
      Objective_Source,
      Problem_Source
    };

    void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
    {
      suffix_ids_.push_back(suffix_string);
      suffix_types_.push_back(type);
      suffix_sources_.push_back(source);
      //      suffix_values_.push_back();
    }

    const Index* GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const;

    const Number* GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const;

    std::vector<Index> GetIntegerSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;

    std::vector<Number> GetNumberSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Default Constructor */
    //AmplSuffixHandler();

    /** Copy Constructor */
    AmplSuffixHandler(const AmplSuffixHandler&);

    /** Overloaded Equals Operator */
    void operator=(const AmplSuffixHandler&);
    //@}

    mutable ASL_pfgh* asl_;

    SufDecl* suftab_;

    std::vector<std::string> suffix_ids_;
    std::vector<Suffix_Type> suffix_types_;
    std::vector<Suffix_Source> suffix_sources_;

    /** Method called by AmplTNLP to prepare the asl for the suffixes */
    void PrepareAmplForSuffixes(ASL_pfgh* asl);

    /** Method called by AmplTNLP to retrieve the suffixes from asl */
    //    void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);

    friend class AmplTNLP;
  };

  /** Class for storing a number of AMPL options that should be
   *  registered to the AMPL Solver library interface */
  class AmplOptionsList : public ReferencedObject
  {
  public:
    enum AmplOptionType {
      String_Option,
      Number_Option,
      Integer_Option,
      WS_Option,  /* this is for AMPL's internal wantsol callback */
      HaltOnError_Option /* this is for our setting of the nerror_ member */
    };

    /** Ampl Option class, contains name, type and description for an
     *  AMPL option */
  class AmplOption : public ReferencedObject
    {
    public:
      AmplOption(const std::string ipopt_option_name,
                 AmplOptionType type,
                 const std::string description);

      ~AmplOption()
      {
        delete [] description_;
      }

      const std::string& IpoptOptionName() const
      {
        return ipopt_option_name_;
      }
      AmplOptionType Type() const
      {
        return type_;
      }
      char* Description() const
      {
        return description_;
      }
    private:
      /**@name Default Compiler Generated Methods
       * (Hidden to avoid implicit creation/calling).
       * These methods are not implemented and 
       * we do not want the compiler to implement
       * them for us, so we declare them private
       * and do not define them. This ensures that
       * they will not be implicitly created/called. */
      //@{
      /** Default Constructor */
      AmplOption();

      /** Copy Constructor */
      AmplOption(const AmplOption&);

      /** Overloaded Equals Operator */
      void operator=(const AmplOption&);
      //@}

      const std::string ipopt_option_name_;
      const AmplOptionType type_;
      char* description_;
    };

    class PrivatInfo
    {
    public:
      PrivatInfo(const std::string ipopt_name,
                 SmartPtr<OptionsList> options,
                 SmartPtr<const Journalist> jnlst,
                 void** nerror = NULL)
          :
          ipopt_name_(ipopt_name),
          options_(options),
          jnlst_(jnlst),
          nerror_(nerror)
      {}
      const std::string& IpoptName() const
      {
        return ipopt_name_;
      }
      const SmartPtr<OptionsList>& Options() const
      {
        return options_;
      }
      const SmartPtr<const Journalist>& Jnlst() const
      {
        return jnlst_;
      }
      void** NError()
      {
        return nerror_;
      }
    private:
      const std::string ipopt_name_;
      const SmartPtr<OptionsList> options_;
      const SmartPtr<const Journalist> jnlst_;
      void** nerror_;
    };

  public:
    /** Default Constructor */
    AmplOptionsList()
        :
        keywds_(NULL),
        nkeywds_(0)
    {}

    /** Destructor */
    ~AmplOptionsList();

    /** Adding a new AMPL Option */
    void AddAmplOption(const std::string ampl_option_name,
                       const std::string ipopt_option_name,
                       AmplOptionsList::AmplOptionType type,
                       const std::string description)
    {
      SmartPtr<AmplOption> new_option =
        new AmplOption(ipopt_option_name, type, description);
      ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
    }

    /** Number of AMPL Options */
    Index NumberOfAmplOptions()
    {
      return (Index)ampl_options_map_.size();
    }

    /** ASL keywords list for the stored options. */
    void* Keywords(const SmartPtr<OptionsList>& options,
                   SmartPtr<const Journalist> jnlst,
                   void** nerror);

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Default Constructor */
    //AmplOptionsList();

    /** Copy Constructor */
    AmplOptionsList(const AmplOptionsList&);

    /** Overloaded Equals Operator */
    void operator=(const AmplOptionsList&);
    //@}

    void MakeValidLatexString(std::string source, std::string& dest) const;

    void PrintLatex(SmartPtr<const Journalist> jnlst);

    /** map for storing registered AMPL options */
    std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
    // AW: I think it should be with const like in the following line
    //     but with const the AIX compiler fails
    // std::map<const std::string, SmartPtr<const AmplOption> > ampl_options_map_;

    /** pointer to the keywords */
    void* keywds_;

    /** Number of entries stored in keywds_ */
    Index nkeywds_;
  };

  /** Ampl Interface.
   *  Ampl Interface, implemented as a TNLP.
   */
  class AmplTNLP : public TNLP
  {
  public:
    /**@name Constructors/Destructors */
    //@{
    /** Constructor. */
    AmplTNLP(const SmartPtr<const Journalist>& jnlst,
             const SmartPtr<OptionsList> options,
             char**& argv, SmartPtr<AmplSuffixHandler>
             suffix_handler = NULL, bool allow_discrete = false,
             SmartPtr<AmplOptionsList> ampl_options_list = NULL,
             const char* ampl_option_string = NULL,
             const char* ampl_invokation_string = NULL,
             const char* ampl_banner_string = NULL,
             std::string* nl_file_content = NULL);

    /** Default destructor */
    virtual ~AmplTNLP();
    //@}

    /** Exceptions */
    DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);

    /**@name methods to gather information about the NLP. These
    * methods are overloaded from TNLP. See TNLP for their more
    * detailed documentation. */
    //@{
    /** returns dimensions of the nlp. Overloaded from TNLP */
    virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
                              Index& nnz_h_lag, IndexStyleEnum& index_style);

    /** returns names and other meta data for the variables and constraints
     *  Overloaded from TNLP */
    virtual bool get_var_con_metadata(Index n,
                                      StringMetaDataMapType& var_string_md,
                                      IntegerMetaDataMapType& var_integer_md,
                                      NumericMetaDataMapType& var_numeric_md,
                                      Index m,
                                      StringMetaDataMapType& con_string_md,
                                      IntegerMetaDataMapType& con_integer_md,
                                      NumericMetaDataMapType& con_numeric_md);

    /** returns bounds of the nlp. Overloaded from TNLP */
    virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
                                 Index m, Number* g_l, Number* g_u);

    /** Returns the constraint linearity.
     * array will be alocated with length n. (default implementation
     *  just return false and does not fill the array). */
    virtual bool get_constraints_linearity(Index m,
                                           LinearityType* const_types);

    /** provides a starting point for the nlp variables. Overloaded
    from TNLP */
    virtual bool get_starting_point(Index n, bool init_x, Number* x,
                                    bool init_z, Number* z_L, Number* z_U,
                                    Index m, bool init_lambda, Number* lambda);

    /** evaluates the objective value for the nlp. Overloaded from TNLP */
    virtual bool eval_f(Index n, const Number* x, bool new_x,
                        Number& obj_value);

    /** evaluates the gradient of the objective for the
    nlp. Overloaded from TNLP */
    virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
                             Number* grad_f);

    /** evaluates the constraint residuals for the nlp. Overloaded from TNLP */
    virtual bool eval_g(Index n, const Number* x, bool new_x,
                        Index m, Number* g);

    /** specifies the jacobian structure (if values is NULL) and
     *  evaluates the jacobian values (if values is not NULL) for the
     *  nlp. Overloaded from TNLP */
    virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
                            Index m, Index nele_jac, Index* iRow,
                            Index *jCol, Number* values);

    /** specifies the structure of the hessian of the lagrangian (if
     *  values is NULL) and evaluates the values (if values is not
     *  NULL). Overloaded from TNLP */
    virtual bool eval_h(Index n, const Number* x, bool new_x,
                        Number obj_factor, Index m, const Number* lambda,
                        bool new_lambda, Index nele_hess, Index* iRow,
                        Index* jCol, Number* values);

    /** retrieve the scaling parameters for the variables, objective
     *  function, and constraints. */
    virtual bool get_scaling_parameters(Number& obj_scaling,
                                        bool& use_x_scaling, Index n,
                                        Number* x_scaling,
                                        bool& use_g_scaling, Index m,
                                        Number* g_scaling);
    //@}

    /** @name Solution Methods */
    //@{
    virtual void finalize_solution(SolverReturn status,
                                   Index n, const Number* x, const Number* z_L, const Number* z_U,
                                   Index m, const Number* g, const Number* lambda,
                                   Number obj_value,
                                   const IpoptData* ip_data,
                                   IpoptCalculatedQuantities* ip_cq);
    //@}

    /** @name Method for quasi-Newton approximation information. */
    //@{
    virtual Index get_number_of_nonlinear_variables();
    virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
        Index* pos_nonlin_vars);
    //@}


    /**@name Ampl specific methods */
    //@{
    /** Return the ampl solver object (ASL*) */
    ASL_pfgh* AmplSolverObject()
    {
      return asl_;
    }

    /** Write the solution file.  This is a wrapper for AMPL's
     *  write_sol.  TODO Maybe this should be at a different place, or
     *  collect the numbers itself? */
    void write_solution_file(const std::string& message) const;

    /** ampl orders the variables like (continuous, binary, integer).
     *  This method gives the number of binary and integer variables.
     *  For details, see Tables 3 and 4 in "Hooking Your Solver to
     *  AMPL"
     */
    void get_discrete_info(Index& nlvb_,
                           Index& nlvbi_,
                           Index& nlvc_,
                           Index& nlvci_,
                           Index& nlvo_,
                           Index& nlvoi_,
                           Index& nbv_,
                           Index& niv_) const;
    //@}

    /** A method for setting the index of the objective function to be
     *  considered.  This method must be called after the constructor,
     *  and before anything else is called.  It can only be called
     *  once, and if there is more than one objective function in the
     *  AMPL model, it MUST be called. */
    void set_active_objective(Index obj_no);

    /**@name Methods to set meta data for the variables
     * and constraints. These values will be passed on
     * to the TNLP in get_var_con_meta_data
     */
    //@{
    void set_string_metadata_for_var(std::string tag, std::vector<std::string> meta_data)
    {
      var_string_md_[tag] = meta_data;
    }

    void set_integer_metadata_for_var(std::string tag, std::vector<Index> meta_data)
    {
      var_integer_md_[tag] = meta_data;
    }

    void set_numeric_metadata_for_var(std::string tag, std::vector<Number> meta_data)
    {
      var_numeric_md_[tag] = meta_data;
    }

    void set_string_metadata_for_con(std::string tag, std::vector<std::string> meta_data)
    {
      con_string_md_[tag] = meta_data;
    }

    void set_integer_metadata_for_con(std::string tag, std::vector<Index> meta_data)
    {
      con_integer_md_[tag] = meta_data;
    }

    void set_numeric_metadata_for_con(std::string tag, std::vector<Number> meta_data)
    {
      con_numeric_md_[tag] = meta_data;
    }
    //@}

    /** Method for returning the suffix handler */
    SmartPtr<AmplSuffixHandler> get_suffix_handler()
    {
      return suffix_handler_;
    }

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Default Constructor */
    AmplTNLP();

    /** Copy Constructor */
    AmplTNLP(const AmplTNLP&);

    /** Overloaded Equals Operator */
    void operator=(const AmplTNLP&);
    //@}

    /** Journlist */
    SmartPtr<const Journalist> jnlst_;

    /** pointer to the main ASL structure */
    ASL_pfgh* asl_;

    /** Sign of the objective fn (1 for min, -1 for max) */
    double obj_sign_;

    /**@name Problem Size Data*/
    //@{
    Index nz_h_full_; // number of nonzeros in the full_x hessian
    /* the rest of the problem size data is available easily through the ampl variables */
    //@}

    /**@name Internal copies of data */
    //@{
    /** Solution Vectors */
    Number* x_sol_;
    Number* z_L_sol_;
    Number* z_U_sol_;
    Number* g_sol_;
    Number* lambda_sol_;
    Number obj_sol_;
    //@}

    /**@name Flags to track internal state */
    //@{
    /** true when the objective value has been calculated with the
     *  current x, set to false in apply_new_x, and set to true in
     *  internal_objval */
    bool objval_called_with_current_x_;
    /** true when the constraint values have been calculated with the
     *  current x, set to false in apply_new_x, and set to true in
     *  internal_conval */
    bool conval_called_with_current_x_;
    /** true when we have called hesset */
    bool hesset_called_;
    /** true when set_active_objective has been called */
    bool set_active_objective_called_;
    //@}

    /** Pointer to the Oinfo structure */
    void* Oinfo_ptr_;

    /** nerror flag passed to ampl calls - set to NULL to halt on error */
    void* nerror_;

    /** Suffix Handler */
    SmartPtr<AmplSuffixHandler> suffix_handler_;

    /** Make the objective call to ampl */
    bool internal_objval(const Number* x, Number& obj_val);

    /** Make the constraint call to ampl*/
    bool internal_conval(const Number* x, Index m, Number* g=NULL);

    /** Internal function to update the internal and ampl state if the
     *  x value changes */
    bool apply_new_x(bool new_x, Index n, const Number* x);

    /** Method for obtaining the name of the NL file and the options
     *  set from AMPL.  Returns a pointer to a char* with the name of
     *  the stub */
    char* get_options(const SmartPtr<OptionsList>& options,
                      SmartPtr<AmplOptionsList>& ampl_options_list,
                      const char* ampl_option_string,
                      const char* ampl_invokation_string,
                      const char* ampl_banner_string, char**& argv);

    /** returns true if the ampl nerror code is ok */
    bool nerror_ok(void* nerror);

    /** calls hesset ASL function */
    void call_hesset();

    /** meta data to pass on to TNLP */
    StringMetaDataMapType var_string_md_;
    IntegerMetaDataMapType var_integer_md_;
    NumericMetaDataMapType var_numeric_md_;
    StringMetaDataMapType con_string_md_;
    IntegerMetaDataMapType con_integer_md_;
    NumericMetaDataMapType con_numeric_md_;
  };



} // namespace Ipopt

#endif