#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

#   =================================================================
#   File: algencanma.py
#   =================================================================
#
#   =================================================================
#   Module: Main program
#   =================================================================
#
#   Last update of any of the component of this module:
#
#   August 06, 2007.
#
#   Users are encouraged to download periodically updated versions of
#   this code at the TANGO home page:
#
#   www.ime.usp.br/~egbirgin/tango/
#
#   *****************************************************************
#   *****************************************************************
#
#   ALGENCAN solves problems of the form
#   ------------------------------------
#
#   min f(x)
#
#   subject to
#
#           c_j(x)  = 0, j in E,
#           c_j(x) <= 0, j in I,
#           l <= x <= u,
#
#   where E is the set of indices of the equality constraints, I is
#   the set of indices of the inequality constraints, and there are
#   n variables and m constraints.
#
#   ALGENCAN is an Augmented Lagrangian method that uses GENCAN to
#   solve the bound-constrained problems.
#
#   ALGENCAN is part of the TANGO Project.
#
#   Visit the TANGO home page in the web:
#
#   www.ime.usp.br/~egbirgin/tango/
#
#   *****************************************************************
#
#   TANGO LICENSE:
#   --------------
#
#   TANGO is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation. Non-free versions of TANGO are
#   available under terms different from those of the General Public
#   License. Professors J. M. Martínez (martinez@ime.unicamp.br,
#   martinezimecc@gmail.com) or E. G. Birgin (egbirgin@ime.usp.br,
#   egbirgin@gmail.com) should be contacted for more information
#   related to such a license, future developments and/or technical
#   support.
#
#   Every published work that uses ALGENCAN should cite:
#
#   R. Andreani, E. G. Birgin, J. M. Martínez and M. L. Schuverdt,
#   "On Augmented Lagrangian methods with general lower-level
#   constraints", to appear in SIAM Journal on Optimization.
#
#   and
#
#   R. Andreani, E. G. Birgin, J. M. Martínez and M. L. Schuverdt,
#   "Augmented Lagrangian methods under the Constant Positive Linear
#   Dependence constraint qualification", Mathematical
#   Programming 111, pp. 5-32, 2008.
#
#   Every published work that uses GENCAN should cite:
#
#   E. G. Birgin and J. M. Martínez, "Large-scale active-set
#   box-constrained optimization method with spectral projected
#   gradients", Computational Optimization and Applications 23, pp.
#   101-125, 2002.
#
#   (See other related works at the TANGO home page.)
#
#   *****************************************************************
#
#   HOW TO USE ALGENCAN TO SOLVE YOUR OWN PROBLEM
#   --------------------------------------------
#
#   You will find below 10 functions and that YOU SHOULD MODIFY to
#   solve your own problem. The modifications that you must do are:
#
#   1) In the FUNCTION INIP you must set the number of variables
#   of your problem (n), the initial point (x), the lower and upper
#   bounds on the variables (l and u), the number of constraints (m)
#   (set m equal to zero if your problems has no constraints), a
#   vector (equatn) that indicates, for each constraint, whether it
#   is and equality constraint or an inequality (least-or-equal),
#   a vector (linear) that indicates, for each constraint, whether it
#   is a linear constraint or not, and the initial estimation of the
#   Lagrange multipliers (lambda) (you can set it equal to zero if
#   you do not have a better estimative).
#
#   2) FUNCTIONS EVALF and EVALC MUST also be MODIFIED to evaluate
#   the objective function and the constraints, respectively.
#
#   3) FUNCTIONS EVALG and EVALJAC, to compute the gradient vector
#   of the objective function and the gradients of the constraints,
#   respectively, are NOT MANDATORY but HIGHLY RECOMMENDED. They
#   must be provided by the user if he/she set gtype = 0. If the
#   user set gtype = 1 then finite differences approximations will
#   be used. (See the description of gtype parameter in function
#   param.)
#
#   4) OPTIONALY, you can modify FUNCTIONS EVALH and EVALHC to
#   evaluate the Hessian matrices of the objective function and the
#   constraints. Both functions are just optional functions and
#   are required when hptype = 0. (See the description of hptype
#   parameter in function param.)
#
#   5) OPTIONALY, another way of using second-order information is
#   to provide, not individual functions to compute the Hessians
#   of the objective function and the constraints, but to provide
#   a unique FUNCTION EVALHLP to compute the product of an
#   arbitrary vector times the Lagrangian Hessian matrix. There is
#   not a clear advantage in coding EVALHLP instead of coding
#   EVALH and EVALHC. This alternative was incorporated to be
#   primarily used in the AMPL and CUTEr interfaces. This
#   function is optional and is required when hptype = 1. (See
#   the description of hptype parameter in function param.)
#
#   6) OPTIONALLY, in the FUNCTION PARAM you may modify some
#   arguments like feasibility and optimality tolerances and maximum
#   number of iterations and functional evaluations. See the detailed
#   description of each argument in function PARAM. If you prefer,
#   leave all these parameters with the suggested values.

#   7) Alternatively, if you are interested in modifying any of the
#   parameters described in (5) but you prefer not to modify function
#   PARAM, you can use the algencan.dat SPECIFICATION FILE. The
#   advantage of using the specification file is that you can test
#   the method with different parameters without compiling it every
#   time. This file IS NOT MANDATORY. So, if you will not modify the
#   default parameters or if you fill more comfortable modifying
#   function PARAM, you do not need to worry about algencan.dat
#   specification file.
#
#   The specification file can have any number of lines, with at most
#   80 characters per line. Blank lines are admitted and lines starting
#   with '*' or '#' are considered comment lines and will be ignored.
#   Each line must have just a "keyword" or a keyword followed by an
#   integer or a real value when required. As the interpreter is not
#   case-sensitive, you can write the keywords in lower case, upper
#   case or any combination of them. You can also insert blanks in any
#   place before or after the keywords. Moreover, just the first 10
#   characters of the keyword are relevant and the rest will be ignored.
#   Available keywords are:
#
#   ANALYTIC-GRADIENT
#   FINITE-DIFFERENCES-GRADIENT
#   HESSIANS-PROVIDED
#   LAGRHESS-PRODUCT-PROVIDED
#   INCREMENTAL-QUOTIENTS
#   BFGS-QN-APPROXIMATION
#   ADAPTIVE-HESSIAN
#   AUTO-BDSOLVER
#   GENCAN-BDSOLVER
#   BETRA-BDSOLVER
#   SPARSE-BETRA-BDSOLVER
#   UNPRECONDITIONED-CG
#   BFGS-QN-PRECONDITIONER
#   AUTO-INITIAL-PENALTY-PARAMETERS
#   MANUAL-INITIAL-PENALTY-PARAMETERS      <real-value>
#   COLLECTIVE-PENALTY-PARAMETERS-UPDATE
#   INDEPENDENT-PENALTY-PARAMETERS-UPDATE
#   DESIRED-INFEASIBILITY-FRACTION         <real-value>
#   PENALTY-PARAMETER-MULTIPLIER-INCREMENT <real-value>
#   CHECK-DERIVATIVES
#   FEASIBILITY-TOLERANCE                  <real-value>
#   OPTIMALITY-TOLERANCE                   <real-value>
#   MAX-OUTER-ITERATIONS                   <integer-value>
#   MAX-INNER-ITERATIONS                   <integer-value>
#   MAX-FUNCTION-EVALUATIONS               <integer-value>
#   OUTPUT-DETAIL                          <integer-value>
#   NCOMP-ARRAY                            <integer-value>
#
#   By default, ALGENCAN uses:
#
#   ANALYTIC-GRADIENT
#   ADAPTIVE-HESSIAN
#   BFGS-QN-PRECONDITIONER
#   AUTO-BDSOLVER
#   BFGS-QN-PRECONDITIONER
#   AUTO-INITIAL-PENALTY-PARAMETERS
#   COLLECTIVE-PENALTY-PARAMETERS-UPDATE
#   DESIRED-INFEASIBILITY-FRACTION           0.5d0
#   PENALTY-PARAMETER-MULTIPLIER-INCREMENT 1.0d+01
#   FEASIBILITY-TOLERANCE                  1.0d-04
#   OPTIMALITY-TOLERANCE                   1.0d-04
#   MAX-OUTER-ITERATIONS                        50
#   MAX-INNER-ITERATIONS                   1000000
#   MAX-FUNCTION-EVALUATIONS               5000000
#   OUTPUT-DETAIL                                1
#   NCOMP-ARRAY                                  5
#
#   and derivatives are not tested.
#
#   8) Finally, FUNCTION ENDP can be modified by the user to write,
#   save or compute any type of extra information related to the
#   solution of the problem. Function ENDP will be called just once
#   after algencan found a solution of the problem. You can modify
#   this function for, for example, save the solution in a file
#   chosed by you and in a special format to be processed later by
#   another software.
#
#   Besides the modifications that you must do to solve your own
#   problem using ALGENCAN, ALGENCAN has many other arguments to
#   control a large variety of issues of the algorithmic behaviour.
#   To see how to control these other arguments, see their detailed
#   description at the begining of function algencan (for arguments
#   related to Augmented Lagrangians) and at the begining of
#   function gencan (for arguments related to the bound-constraints
#   internal solver).
#
#   ******************************************************************
#   ******************************************************************

#   Load the problem definition file

from toyprob import *

#   Import the Python module

import algencan

#   Set some optional params

algencan.param['iprint'] = 1

#   Call the solver

algencan.solvers(evalf,evalg,evalh,evalc,evaljac,evalhc,evalhlp,inip,endp)

