Nonlinear 1

Model

import sasoptpy as so


def test(cas_conn):

    m = so.Model(name='nlpse01', session=cas_conn)
    x = m.add_variables(range(1, 9), lb=0.1, ub=10, name='x')

    f = so.Expression(0.4 * (x[1]/x[7]) ** 0.67 + 0.4 * (x[2]/x[8]) ** 0.67 + 10 - x[1] - x[2], name='f')
    m.set_objective(f, sense=so.MIN, name='f1')

    m.add_constraint(1 - 0.0588*x[5]*x[7] - 0.1*x[1] >= 0, name='c1')
    m.add_constraint(1 - 0.0588*x[6]*x[8] - 0.1*x[1] - 0.1*x[2] >= 0, name='c2')
    m.add_constraint(1 - 4*x[3]/x[5] - 2/(x[3]**0.71 * x[5]) - 0.0588*(x[7]/x[3]**1.3) >= 0, name='c3')
    m.add_constraint(1 - 4*x[4]/x[6] - 2/(x[4]**0.71 * x[6]) - 0.0588*(x[8]/x[4]**1.3) >= 0, name='c4')
    m.add_constraint(f == [0.1, 4.2], name='frange')

    x[1].set_init(6)
    x[2].set_init(3)
    x[3].set_init(0.4)
    x[4].set_init(0.2)
    x[5].set_init(6)
    x[6].set_init(6)
    x[7].set_init(1)
    x[8].set_init(0.5)

    m.solve(verbose=True, options={'with': 'nlp', 'algorithm': 'activeset'})

    print(m.get_problem_summary())
    print(m.get_solution_summary())
    if m.get_session_type() == 'CAS':
        print(m.get_solution()[['var', 'value']])

    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_1 import test

In [8]: test(cas_conn)
NOTE: Initialized model nlpse01.
NOTE: Added action set 'optimization'.
NOTE: Converting model nlpse01 to OPTMODEL.
   var x {{1,2,3,4,5,6,7,8}} >= 0.1 <= 10;
   x[1] = 6;
   x[2] = 3;
   x[3] = 0.4;
   x[4] = 0.2;
   x[5] = 6;
   x[6] = 6;
   x[7] = 1;
   x[8] = 0.5;
   min f1 = 0.4 * (((x[1]) / (x[7])) ^ (0.67)) + 0.4 * (((x[2]) / (x[8])) ^ (0.67)) - x[1] - x[2] + 10.0;
   con c1 : - 0.0588 * x[5] * x[7] - 0.1 * x[1] >= -1.0;
   con c2 : - 0.0588 * x[6] * x[8] - 0.1 * x[1] - 0.1 * x[2] >= -1.0;
   con c3 : - ((4 * x[3]) / (x[5])) - ((2) / ((x[3]) ^ (0.71) * x[5])) - 0.0588 * ((x[7]) / ((x[3]) ^ (1.3))) >= -1.0;
   con c4 : - ((4 * x[4]) / (x[6])) - ((2) / ((x[4]) ^ (0.71) * x[6])) - 0.0588 * ((x[8]) / ((x[4]) ^ (1.3))) >= -1.0;
   con frange : -9.9 <= 0.4 * (((x[1]) / (x[7])) ^ (0.67)) + 0.4 * (((x[2]) / (x[8])) ^ (0.67)) - x[1] - x[2] <= -5.8;
   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;

NOTE: Submitting OPTMODEL code to CAS server.
NOTE: Problem generation will use 8 threads.
NOTE: The problem has 8 variables (0 free, 0 fixed).
NOTE: The problem has 0 linear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The problem has 5 nonlinear constraints (0 LE, 0 EQ, 4 GE, 1 range).
NOTE: The OPTMODEL presolver removed 0 variables, 0 linear constraints, and 0 nonlinear constraints.
NOTE: Using analytic derivatives for objective.
NOTE: Using analytic derivatives for nonlinear constraints.
NOTE: The NLP solver is called.
NOTE: The Active Set algorithm is used.
                           Objective                          Optimality
              Iter             Value     Infeasibility             Error
                 0        3.65736570        0.41664483        0.24247905
                 1        3.65736570        0.41664483        0.24247905
                 2        3.40486061        0.10284726        0.18904638
                 3        3.51178229        0.07506389        0.18860455
                 4        4.23595983        0.03595983        0.60088809
                 5        4.16334906                 0        0.47130008
                 6        4.03168584        0.00791810        0.13742971
                 7        3.88912660        0.11248991        0.06129662
                 8        3.89579714        0.09534670        0.05994916
                 9        3.95046640        0.02649207        0.06776850
                10        3.92833580        0.03517161        0.06442935
                11        3.95179326        0.00494247        0.05837915
                12        3.94741555        0.00651989        0.05477333
                13        3.95209064        0.00058609        0.05265725
                14        3.95058104        0.00122758        0.04772557
                15        3.95055959        0.00099113        0.04613473
                16        3.95141460        0.00000381        0.04497006
                17        3.95132211   0.0000005999371        0.07584723
                18        3.95114031        0.00000941        0.04093117
                19        3.95027690        0.00011307        0.00020755
                20        3.95115797   0.0000007730235        0.00018707
                21        3.95116558                 0        0.00001366
                22        3.95116364   0.0000000153799        0.00000814
                23        3.95116355   0.0000000228326        0.00000595
                24        3.95116352   0.0000000257138        0.00000337
                25        3.95116349   0.0000000200547        0.00000132
                26        3.95116349   0.0000000192412   0.0000002015918
NOTE: Optimal.
NOTE: Objective = 3.9511634887.
NOTE: Objective of the best feasible solution found = 3.9511579677.
NOTE: The best feasible solution found is returned.
NOTE: The output table 'SOLUTION' in caslib 'CASUSER(casuser)' has 8 rows and 6 columns.
NOTE: The output table 'DUAL' in caslib 'CASUSER(casuser)' has 5 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 20 rows and 4 columns.
Selected Rows from Table PROBLEMSUMMARY

                                Value
Label                                
Objective Sense          Minimization
Objective Function                 f1
Objective Type              Nonlinear
                                     
Number of Variables                 8
Bounded Above                       0
Bounded Below                       0
Bounded Below and Above             8
Free                                0
Fixed                               0
                                     
Number of Constraints               5
Linear LE (<=)                      0
Linear EQ (=)                       0
Linear GE (>=)                      0
Linear Range                        0
Nonlinear LE (<=)                   0
Nonlinear EQ (=)                    0
Nonlinear GE (>=)                   4
Nonlinear Range                     1
Selected Rows from Table SOLUTIONSUMMARY

                            Value
Label                            
Solver                        NLP
Algorithm              Active Set
Objective Function             f1
Solution Status     Best Feasible
Objective Value      3.9511579677
                                 
Optimality Error     0.0001050714
Infeasibility        7.7302351E-7
                                 
Iterations                     26
Presolve Time                0.00
Solution Time                0.03
Selected Rows from Table SOLUTION

    var     value
0  x[1]  6.463315
1  x[2]  2.234530
2  x[3]  0.667455
3  x[4]  0.595820
4  x[5]  5.932980
5  x[6]  5.527231
6  x[7]  1.013787
7  x[8]  0.400664
Out[8]: 3.951157967716