Nonlinear 2

Model

import sasoptpy as so
import sasoptpy.abstract.math as sm


def test(cas_conn):

    m = so.Model(name='nlpse02', session=cas_conn)
    N = m.add_parameter(name='N', init=1000)
    x = m.add_variables(so.exp_range(1, N), name='x', init=1)
    m.set_objective(
        so.expr_sum(-4*x[i]+3 for i in so.exp_range(1, N-1)) +
        so.expr_sum((x[i]**2 + x[N]**2)**2 for i in so.exp_range(1, N-1)),
        name='f', sense=so.MIN)

    m.add_statement('print x;', after_solve=True)
    m.solve(options={'with': 'nlp'}, verbose=True)
    print(m.get_solution_summary())
    if m.get_session_type() == 'CAS':
        print(m.response['Print1.PrintTable'].head())

    # Model 2
    so.reset()
    m = so.Model(name='nlpse02_2', session=cas_conn)
    N = m.add_parameter(name='N', init=1000)
    x = m.add_variables(so.exp_range(1, N), name='x', lb=1, ub=2)
    m.set_objective(
        so.expr_sum(sm.cos(-0.5*x[i+1] - x[i]**2) for i in so.exp_range(
            1, N-1)), name='f2', sense=so.MIN)
    m.add_statement('print x;', after_solve=True)
    m.solve(verbose=True, options={'with': 'nlp', 'algorithm': 'activeset'})
    print(m.get_solution_summary())

    return m.get_objective_value()

Output

In [1]: import os

In [2]: hostname = os.getenv('CASHOST')

In [3]: port = os.getenv('CASPORT')

In [4]: from swat import CAS

In [5]: cas_conn = CAS(hostname, port)

In [6]: import sasoptpy
In [7]: from examples.client_side.nonlinear_2 import test

In [8]: test(cas_conn)
NOTE: Initialized model nlpse02.
NOTE: Added action set 'optimization'.
NOTE: Converting model nlpse02 to OPTMODEL.
   num N init 1000;
   var x {{1..N}} init 1;
   min f = sum {i in 1..N-1} (- 4 * x[i] + 3) + sum {i in 1..N-1} (((x[i]) ^ (2) + (x[N]) ^ (2)) ^ (2));
   solve with nlp / ;
   create data solution from [i]= {1.._NVAR_} var=_VAR_.name value=_VAR_ lb=_VAR_.lb ub=_VAR_.ub rc=_VAR_.rc;
   create data dual from [j] = {1.._NCON_} con=_CON_.name value=_CON_.body dual=_CON_.dual;
   print x;

NOTE: Submitting OPTMODEL code to CAS server.
NOTE: Problem generation will use 8 threads.
NOTE: The problem has 1000 variables (1000 free, 0 fixed).
NOTE: The problem has 0 linear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The problem has 0 nonlinear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The OPTMODEL presolver removed 0 variables, 0 linear constraints, and 0 nonlinear constraints.
NOTE: Using analytic derivatives for objective.
NOTE: Using 2 threads for nonlinear evaluation.
NOTE: The NLP solver is called.
NOTE: The Interior Point Direct algorithm is used.
                           Objective                          Optimality
              Iter             Value     Infeasibility             Error
                 0     2997.00000000                 0        2.66666667
                 1      561.93750000                 0        4.44444444
                 2       41.17478400                 0       12.76397516
                 3        0.41230550                 0       43.37933609
                 4        0.00005471                 0        0.47005128
                 5  2.2737367544E-13                 0        0.00006316
                 6                 0                 0  1.1527477269E-12
NOTE: Optimal.
NOTE: Objective = 0.
NOTE: The output table 'SOLUTION' in caslib 'CASUSER(casuser)' has 1000 rows and 6 columns.
NOTE: The output table 'DUAL' in caslib 'CASUSER(casuser)' has 0 rows and 4 columns.
NOTE: The CAS table 'solutionSummary' in caslib 'CASUSER(casuser)' has 12 rows and 4 columns.
NOTE: The CAS table 'problemSummary' in caslib 'CASUSER(casuser)' has 12 rows and 4 columns.
Selected Rows from Table SOLUTIONSUMMARY

                                    Value
Label                                    
Solver                                NLP
Algorithm           Interior Point Direct
Objective Function                      f
Solution Status                   Optimal
Objective Value                         0
                                         
Optimality Error             1.152748E-12
Infeasibility                           0
                                         
Iterations                              6
Presolve Time                        0.00
Solution Time                        0.02
   COL1    x
0   1.0  1.0
1   2.0  1.0
2   3.0  1.0
3   4.0  1.0
4   5.0  1.0
NOTE: Initialized model nlpse02_2.
NOTE: Added action set 'optimization'.
NOTE: Converting model nlpse02_2 to OPTMODEL.
   num N init 1000;
   var x {{1..N}} >= 1 <= 2;
   min f2 = sum {i in 1..N-1} (cos(- 0.5 * (x[i + 1]) - ((x[i]) ^ (2))));
   solve with nlp / algorithm=activeset;
   create data solution from [i]= {1.._NVAR_} var=_VAR_.name value=_VAR_ lb=_VAR_.lb ub=_VAR_.ub rc=_VAR_.rc;
   create data dual from [j] = {1.._NCON_} con=_CON_.name value=_CON_.body dual=_CON_.dual;
   print x;

NOTE: Submitting OPTMODEL code to CAS server.
NOTE: Problem generation will use 8 threads.
NOTE: The problem has 1000 variables (0 free, 0 fixed).
NOTE: The problem has 0 linear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The problem has 0 nonlinear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The OPTMODEL presolver removed 0 variables, 0 linear constraints, and 0 nonlinear constraints.
NOTE: Using analytic derivatives for objective.
NOTE: Using 3 threads for nonlinear evaluation.
NOTE: The NLP solver is called.
NOTE: The Active Set algorithm is used.
NOTE: Initial point was changed to be feasible to bounds.
                           Objective                          Optimality
              Iter             Value     Infeasibility             Error
                 0       70.66646447                 0        1.24686873
                 1       70.66646439                 0        1.24686873
                 2     -996.26893548                 0        0.23815533
                 3     -998.99328004                 0        0.10718277
                 4     -998.99999439                 0        0.00379400
                 5     -999.00000000                 0        0.00000393
                 6     -999.00000000                 0  1.7018480129E-12
NOTE: Optimal.
NOTE: Objective = -999.
NOTE: The output table 'SOLUTION' in caslib 'CASUSER(casuser)' has 1000 rows and 6 columns.
NOTE: The output table 'DUAL' in caslib 'CASUSER(casuser)' has 0 rows and 4 columns.
NOTE: The CAS table 'solutionSummary' in caslib 'CASUSER(casuser)' has 12 rows and 4 columns.
NOTE: The CAS table 'problemSummary' in caslib 'CASUSER(casuser)' has 12 rows and 4 columns.
Selected Rows from Table SOLUTIONSUMMARY

                           Value
Label                           
Solver                       NLP
Algorithm             Active Set
Objective Function            f2
Solution Status          Optimal
Objective Value             -999
                                
Optimality Error    1.701848E-12
Infeasibility                  0
                                
Iterations                     6
Presolve Time               0.00
Solution Time               0.04
Out[8]: -999.0