Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gnowzil
Created October 28, 2021 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnowzil/16bed4965da27a86133fbaec7f75658b to your computer and use it in GitHub Desktop.
Save gnowzil/16bed4965da27a86133fbaec7f75658b to your computer and use it in GitHub Desktop.
HS116 with nonlinear constraints
function [x,F,xmul,Fmul,INFO] = hs116_nonlin()
% HS116
% Problem has both linear and nonlinear constraints but
% all constraints are defiend as nonlinear in this example.
%
% Jacobian defined in coordinate form
% Linear objective defined in row 1.
%
%
options.printfile = 'hs116_nonlin.out';
options.specsfile = which('hs116.spc');
options.screen = 'on';
options.name = 'hs116';
[x,xlow,xupp,xmul,xstate, ...
Flow,Fupp,Fmul,Fstate, ...
ObjAdd,ObjRow,A.val,A.row,A.col,G.row,G.col] = hs116data;
[x,F,INFO,xmul,Fmul]= snopt(x, xlow, xupp, xmul, xstate, ...
Flow, Fupp, Fmul, Fstate, ...
@hs116userfun, ObjAdd, ObjRow, ...
A, G, options);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x,xlow,xupp,xmul,xstate, ...
Flow,Fupp,Fmul,Fstate, ...
ObjAdd,ObjRow,A,iAfun,jAvar,iGfun,jGvar] = hs116data()
% hs116data defines problem HS116.
%
% Minimize x(11) + x(12) + x(13)
% subject to many constraints
n = 13;
neF = 15;
ObjRow = 1;
%% parameters
a = 0.002;
b = 1.262626;
c = 1.231059;
d = 0.03475;
e = 0.975;
f = 0.00975;
% HS Solution
x = [ 0.80377
0.89999
0.97095
0.10000
0.19081
0.46057
574.07758
74.07758
500.01615
0.10000
20.23309
77.34768
0.00673];
%% initial x
x = [ 0.5;
0.8;
0.9;
0.1;
0.14;
0.5;
489;
80;
650;
450;
150;
150;
150 ];
%% lower bounds on x
xlow = [ 0.1;
0.1;
0.1;
1e-4;
0.1;
0.1;
0.1;
0.1;
500;
0.1;
1;
1e-4;
1e-4 ];
%% upper bounds on x
xupp = [ 1 ;
1 ;
1 ;
0.1;
0.9;
0.9;
1000;
1000;
1000;
500;
150;
150;
150 ];
xstate = zeros(n,1);
xmul = zeros(n,1);
iAfun = []; jAvar = []; A = [];
ObjAdd = 0;
%% Bounds on F
Flow = zeros(neF,1); Fupp = Inf*ones(neF,1);
Flow(ObjRow) = -Inf; Fupp(ObjRow) = Inf;
Flow(3) = -Inf; Fupp(3) = 1;
Flow(4) = 50; Fupp(4) = 250;
Flow(13) = -Inf; Fupp(13) = 1;
Flow(15) = 0.9; Fupp(15) = Inf;
Fmul = zeros(neF,1);
Fstate = zeros(neF,1);
G = [ 5, 2, -1;
5, 3, 1;
2, 1, -1;
2, 2, 1;
3, 7, a;
3, 8, -a;
4, 11, 1;
4, 12, 1;
4, 13, 1;
ObjRow, 11, 1;
ObjRow, 12, 1;
ObjRow, 13, 1;
6, 3, c*x(10);
6, 10, -b + c*x(3);
6, 13, 1;
7, 2, -d - e*x(5) + 2*f*x(2);
7, 5, 1 - e*x(2);
8, 3, -d - e*x(6) + 2*f*x(3);
8, 6, 1 - e*x(3);
9, 1, -d - e*x(4) + 2*f*x(1);
9, 4, 1 - e*x(1);
10, 2, c*x(9);
10, 9, -b + c*x(2);
10, 12, 1;
11, 1, c*x(8);
11, 8, -b + c*x(1);
11, 11, 1;
12, 1, -x(8);
12, 4, -x(7) + x(8);
12, 5, x(7);
12, 7, x(5) - x(4);
12, 8, -x(1) + x(4);
13, 1, -a*x(8);
13, 2, a*x(9);
13, 5, -1 + a*x(8);
13, 6, 1 - a*x(9);
13, 8, a*(x(5) - x(1));
13, 9, a*(x(2) - x(6));
14, 2, -500 + x(9) + x(10);
14, 3, -x(10);
14, 6, 500 - x(9);
14, 9, x(2) - x(6);
14, 10, -x(3) + x(2);
15, 2, 1 - a*x(10);
15, 3, a*x(10);
15, 10, -a*(x(2) - x(3)) ];
iGfun = G(:,1); jGvar = G(:,2);
end
function [F,G] = hs116userfun(x)
% Defines nonlinear terms of F and derivatives for HS 116.
% Parameters
a = 2e-3;
b = 1.262626;
c = 1.231059;
d = 3.475e-2;
e = 9.75e-1;
f = 9.75e-3;
ObjRow = 1;
F = [ObjRow, x(11) + x(12) + x(13);
2, x(2) - x(1);
3, a*x(7) - a*x(8);
4, x(11) + x(12) + x(13);
5, x(3) - x(2);
6, x(13) - b*x(10) + c*x(3)*x(10); %
7, x(5) - d*x(2) - e*x(2)*x(5) + f*x(2)^2; %
8, x(6) - d*x(3) - e*x(3)*x(6) + f*x(3)^2; %
9, x(4) - d*x(1) - e*x(1)*x(4) + f*x(1)^2; %
10, x(12) - b*x(9) + c*x(2)*x(9); %
11, x(11) - b*x(8) + c*x(1)*x(8); %
12, x(5)*x(7) - x(1)*x(8) - x(4)*x(7) + x(4)*x(8); %
13, x(6) + x(5) + a*(x(2)*x(9) + x(5)*x(8) - x(1)*x(8) - x(6)*x(9)); %
14, -500*(x(2) - x(6)) + x(2)*x(9) - x(3)*x(10) - x(6)*x(9) + x(2)*x(10); %
15, x(2) - a*(x(2)*x(10) - x(3)*x(10)) ]; %
F = F(:,2);
% Define the derivatives.
if nargout > 1,
G = [ 5, 2, -1;
5, 3, 1;
2, 1, -1;
2, 2, 1;
3, 7, a;
3, 8, -a;
4, 11, 1;
4, 12, 1;
4, 13, 1;
ObjRow, 11, 1;
ObjRow, 12, 1;
ObjRow, 13, 1;
6, 3, c*x(10);
6, 10, -b + c*x(3);
6, 13, 1;
7, 2, -d - e*x(5) + 2*f*x(2);
7, 5, 1 - e*x(2);
8, 3, -d - e*x(6) + 2*f*x(3);
8, 6, 1 - e*x(3);
9, 1, -d - e*x(4) + 2*f*x(1);
9, 4, 1 - e*x(1);
10, 2, c*x(9);
10, 9, -b + c*x(2);
10, 12, 1;
11, 1, c*x(8);
11, 8, -b + c*x(1);
11, 11, 1;
12, 1, -x(8);
12, 4, -x(7) + x(8);
12, 5, x(7);
12, 7, x(5) - x(4);
12, 8, -x(1) + x(4);
13, 1, -a*x(8);
13, 2, a*x(9);
13, 5, 1 + a*x(8);
13, 6, 1 - a*x(9);
13, 8, a*(x(5) - x(1));
13, 9, a*(x(2) - x(6));
14, 2, -500 + x(9) + x(10);
14, 3, -x(10);
14, 6, 500 - x(9);
14, 9, x(2) - x(6);
14, 10, -x(3) + x(2);
15, 2, 1 - a*x(10);
15, 3, a*x(10);
15, 10, -a*(x(2) - x(3)) ];
G = G(:,3);
end
end
@gnowzil
Copy link
Author

gnowzil commented Oct 28, 2021


         ==============================
         S N O P T  7.7.7    (Feb 2021)
         ==============================
1
 

 SPECS file
 ----------

      Begin HS problem
         Major iterations               100
         Minor iterations               100
 
         Derivative option                1 * First derivatives
         Derivative line search             * The default
 
         Major Print level           000001
      *                             (JFLXBT)
         Minor print level                1
 
      *  Print frequency                  1
      *  Summary frequency                1
 
         Verify level                     3
 
      *  Objective row                    0 * Would find a feasible point
         Solution                       Yes
      End HS problem
1
 
 SNSPEC EXIT 100 -- finished successfully
 SNSPEC INFO 101 -- SPECS file read
1
 
 SNMEMA EXIT 100 -- finished successfully
 SNMEMA INFO 104 -- memory requirements estimated
1
 
 Parameters
 ==========

 Files
 -----
 Solution file..........         0       Old basis file ........         0       Standard input.........         5
 Insert file............         0       New basis file ........         0       (Printer)..............        10
 Punch file.............         0       Backup basis file......         0       (Specs file)...........        11
 Load file..............         0       Dump file..............         0       Standard output........         6

 Frequencies
 -----------
 Print frequency........       100       Check frequency........        60       Save new basis map.....       100
 Summary frequency......       100       Factorization frequency        50       Expand frequency.......     10000

 QP subproblems
 --------------
 QPsolver Cholesky......
 Scale tolerance........     0.900       Minor feasibility tol..  1.00E-06       Iteration limit........     10000
 Scale option...........         0       Minor optimality  tol..  1.00E-06       Minor print level......         1
 Crash tolerance........     0.100       Pivot tolerance........  3.25E-11       New superbasics........        99
 Crash option...........         3       Elastic weight.........  1.00E+05

 Partial pricing
 ---------------
 LP Partial price.......         1       Prtl price section ( A)        13       Prtl price section (-I)        15
 QP Partial price.......         1       Prtl price section ( A)        13       Prtl price section (-I)        15

 The SQP Method
 --------------
 Minimize...............                 Cold start.............                 Proximal Point method..         1
 Nonlinear objectiv vars         3       Objective Row..........         1       Function precision.....  3.00E-13
 Unbounded step size....  1.00E+20       Superbasics limit......        13       Difference interval....  5.48E-07
 Unbounded objective....  1.00E+10       Reduced Hessian dim....        13       Central difference int.  6.70E-05
 Major step limit.......  2.00E+00       Derivative linesearch..                 Derivative option......         1
 Major iterations limit.       100       Linesearch tolerance...   0.90000       Verify level...........         3
 Minor iterations limit.       100       Penalty parameter......  0.00E+00       Major Print Level......         1
 Time limit (secs)...... 9999999.0       Major optimality tol...  2.00E-06

 Hessian Approximation
 ---------------------
 Hessian full-memory....                 Hessian updates........  99999999       Hessian frequency......  99999999
                                                                                 Hessian flush..........  99999999

 Nonlinear constraints
 ---------------------
 Nonlinear constraints..        14       Major feasibility tol..  1.00E-06       Violation limit........  1.00E+06
 Nonlinear Jacobian vars        13

 Miscellaneous
 -------------
 LU factor tolerance....      3.99       LU singularity tol.....  3.25E-11       Timing level...........         3
 LU update tolerance....      3.99       LU swap tolerance......  1.22E-04       Debug level............         0
 LU partial  pivoting...                 eps (machine precision)  2.22E-16       System information.....        No
                                                                                 Sticky parameters......        No

 Total char*8  workspace       500       Total integer workspace     22017       Total real    workspace     12463
 Total char*8  (minimum)       500       Total integer (minimum)      1230       Total real    (minimum)      1997
 
1
 

 

 Matrix statistics
 -----------------
               Total      Normal        Free       Fixed     Bounded
 Rows             15          13           1           0           1
 Columns          13           0           0           0          13

 No. of matrix elements                   43     Density      22.051
 Biggest  constant element        0.0000E+00  (excluding fixed columns,
 Smallest constant element        0.0000E+00   free rows, and RHS)

 No. of objective coefficients             0

 Nonlinear constraints      14     Linear constraints       1
 Nonlinear variables        13     Linear variables         0
 Jacobian  variables        13     Objective variables      3
 Total constraints          15     Total variables         13
1
 
 Initial basis
 -------------

 No basis file supplied

 Crash option  3

 Crash on linear E  rows:

 Crash on linear LG rows:
 Slacks     1  Free cols     0  Preferred     0
 Unit       0  Double        0  Triangle      0  Pad     0

 Itn      0: Feasible linear rows
 Itn      0: PP1.  Minimizing  Norm(x-x0)

 Itn      0: PP1.  Norm(x-x0) approximately minimized  (0.00E+00)

 
 The user has defined      46   out of      46   first  derivatives

 Verification of user-supplied problem derivatives.

 The constraint gradients seem to be OK.

 -->  The largest discrepancy was    1.96E-07  in constraint    27
 

 The objective  gradients seem to be OK.

 Gradient projected in one direction  -2.30766923085E-01
 Difference approximation             -2.30766923078E-01

 
 Column       x(j)        dx(j)    Element no.    Row        Derivative    Difference approxn

     11  1.50000000E+02 -8.27E-05         1         4    1.00000000E+00    1.00000000E+00  ok
                                          2        11    1.00000000E+00    1.00000000E+00  ok
     11  1.50000000E+02 -8.27E-05           Objective    1.00000000E+00    1.00000000E+00  ok

     12  1.50000000E+02 -8.27E-05         3         4    1.00000000E+00    1.00000000E+00  ok
                                          4        10    1.00000000E+00    1.00000000E+00  ok
     12  1.50000000E+02 -8.27E-05           Objective    1.00000000E+00    1.00000000E+00  ok

     13  1.50000000E+02 -8.27E-05         5         4    1.00000000E+00    1.00000000E+00  ok
                                          6         6    1.00000000E+00    1.00000000E+00  ok
     13  1.50000000E+02 -8.27E-05           Objective    1.00000000E+00    1.00000000E+00  ok

      1  5.00000000E-01  8.22E-07         7         2   -1.00000000E+00   -1.00000000E+00  ok
                                          8         9   -1.22500000E-01   -1.22499992E-01  ok
                                          9        11    9.84847200E+01    9.84847200E+01  ok
                                         10        12   -8.00000000E+01   -8.00000000E+01  ok
                                         11        13   -1.60000000E-01   -1.60000000E-01  ok

      2  8.00000000E-01  9.86E-07        12         5   -1.00000000E+00   -1.00000000E+00  ok
                                         13         2    1.00000000E+00    1.00000000E+00  ok
                                         14         7   -1.55650000E-01   -1.55649990E-01  ok
                                         15        10    8.00188350E+02    8.00188350E+02  ok
                                         16        13    1.30000000E+00    1.30000000E+00  ok
                                         17        14    6.00000000E+02    6.00000000E+02  ok
                                         18        15    1.00000000E-01    1.00000000E-01  ok

      3  9.00000000E-01  1.04E-06        19         5    1.00000000E+00    1.00000000E+00  ok
                                         20         6    5.53976550E+02    5.53976550E+02  ok
                                         21         8   -5.04700000E-01   -5.04699990E-01  ok
                                         22        14   -4.50000000E+02   -4.50000000E+02  ok
                                         23        15    9.00000000E-01    9.00000000E-01  ok

      4  1.00000000E-01 -6.03E-07        24         9    5.12500000E-01    5.12500000E-01  ok
                                         25        12   -4.09000000E+02   -4.09000000E+02  ok

      5  1.40000000E-01  6.24E-07        26         7    2.20000000E-01    2.20000000E-01  ok
                                         27        12    4.89000000E+02    4.89000000E+02  ok
                                         28        13    1.16000000E+00    1.16000000E+00  ok

      6  5.00000000E-01  8.22E-07        29         8    1.22500000E-01    1.22500000E-01  ok
                                         30        13   -3.00000000E-01   -3.00000000E-01  ok
                                         31        14   -1.50000000E+02   -1.50000000E+02  ok

      7  4.89000000E+02  2.68E-04        32         3    2.00000000E-03    2.00000000E-03  ok
                                         33        12    4.00000000E-02    4.00000000E-02  ok

      8  8.00000000E+01  4.44E-05        34         3   -2.00000000E-03   -2.00000000E-03  ok
                                         35        11   -6.47096500E-01   -6.47096500E-01  ok
                                         36        12   -4.00000000E-01   -4.00000000E-01  ok
                                         37        13   -7.20000000E-04   -7.20000004E-04  ok

      9  6.50000000E+02  3.57E-04        38        10   -2.77778800E-01   -2.77778800E-01  ok
                                         39        13    6.00000000E-04    6.00000000E-04  ok
                                         40        14    3.00000000E-01    3.00000000E-01  ok

     10  4.50000000E+02  2.47E-04        41         6   -1.54672900E-01   -1.54672900E-01  ok
                                         42        14   -1.00000000E-01   -1.00000000E-01  ok
                                         43        15    2.00000000E-04    2.00000000E-04  ok

     43  Jacobian elements in cols      1  thru    13  seem to be OK.

 -->  The largest relative error was    8.31E-09   in row     7,  column     2
 

      3  objective gradients out of     1  thru    13  seem to be OK.

 -->  The largest relative error was    8.33E-11   in column    11
 

 Crash on nonlinear rows:
 Slacks     8  Free cols     0  Preferred     0
 Unit       3  Double        1  Triangle      0  Pad     2
1
 
 Itn      0: Hessian set to a scaled identity matrix
 

   Itns Major Minors    Step   nCon Feasible  Optimal  MeritFunction     L+U BSwap     nS condZHZ Penalty
     16     0     16              1  3.1E-01  2.2E-02  4.5000000E+02      46            4 2.1E+00         _  r
 Itn     16: Hessian set to a scaled identity matrix
     17     1      1 1.4E-02      2  3.0E-01  2.1E-02  2.9039929E+04      39     1      4 1.9E+00 1.4E+00 _  rl
     30     2     13 1.4E-02      3  3.0E-01  7.7E-02  5.0796990E+04      56            2 1.0E+01 2.6E+00 _s  l
     41     3     11 1.0E+00      4  1.9E-02  1.9E-02  1.7132364E+02      79            3 1.5E+06 2.6E+00 _
     64     4     23 1.0E+00      5  1.6E-02  6.0E-02  1.3950071E+02      87            2 8.5E+00 2.6E+00 _
     73     5      9 1.0E+00      6  2.6E-03  4.1E-02  1.2229579E+02      44            2 9.1E+00 2.6E+00 _
     79     6      6 1.0E+00      8  4.9E-05  5.9E-02  1.1098210E+02      58            1 1.5E+05 2.6E+00 _ M
 Singular(m=n)  rank       14  n-rank       1  nsing        1
 Singular(m>n)  rank       14  n-rank       1  nsing        1
 Itn     79: BR factorize
 Singular(m=n)  rank       14  n-rank       1  nsing        1
 Itn     79: LU partial  pivoting tols       2.00      2.00
     83     7      4 1.0E+00      9  4.2E-04  2.1E-03  9.8011083E+01      47     1      1 1.9E+04 2.6E+00 _
 Itn     83: LU partial  pivoting tols       3.99      3.99
     84     8      1 1.0E+00     10  1.5E-05  1.9E-05  9.7590245E+01      42            1 1.9E+04 2.6E+00 _
     85     9      1 1.0E+00     11 (3.2E-09)(8.0E-10) 9.7591035E+01      42            1 1.9E+04 8.3E+00 _
1
 
 SNOPTA EXIT   0 -- finished successfully
 SNOPTA INFO   1 -- optimality conditions satisfied

 Problem name                 hs116
 No. of iterations                  85   Objective            9.7591031602E+01
 No. of major iterations             9   Linear    obj. term  0.0000000000E+00
 Penalty parameter           8.320E+00   Nonlinear obj. term  9.7591031602E+01
 User function calls (total)        25
 No. of superbasics                  1   No. of basic nonlinears            10
 No. of degenerate steps             2   Percentage                       2.35
 Max x                      10 5.7E+02   Max pi                      6 2.1E+03
 Max Primal infeas          24 1.8E-06   Max Dual infeas            22 1.1E+03
 Nonlinear constraint violn    1.8E-06
1
 
 Name           hs116                    Objective Value      9.7591031602E+01

 Status         Optimal Soln             Iteration     85    Superbasics     1

 Objective               (Min)
 RHS
 Ranges
 Bounds

 Section 1 - Constraints

  Number  ...Row.. State  ...Value......  Slack Value...  ..Lower Limit.  ..Upper Limit.  .Dual Variable    ..i
 
      14  r      1    BS          .               .                None            None           .           1
      15  r      2    BS         0.09623         0.09623          .                None           .           2
      16  r      3    UL         1.0              .                None          1.0           -20.94183      3
      17  r      4    BS        97.59103        47.59103        50.00000       250.00000          .           4
      18  r      5 D  BS         0.00000         0.00000          .                None           .           5
      19  r      6    LL          .               .               .                None          1.00000      6
      20  r      7    LL         0.00000         0.00000          .                None       2088.38824      7
      21  r      8    BS         0.08615         0.08615          .                None           .           8
      22  r      9    LL         0.00000         0.00000          .                None       1065.74680      9
      23  r     10    LL          .               .               .                None          1.00000     10
      24  r     11 I  LL        -0.00000        -0.00000          .                None          1.00000     11
      25  r     12 N  FR         0.00000         0.00000          .                None          0.46109     12
      26  r     13    UL         1.00000         0.00000           None          1.0          -453.35123     13
      27  r     14    LL        -0.00000        -0.00000          .                None          1.75545     14
      28  r     15    LL         0.90000        -0.00000         0.90000           None        262.19656     15
1
 
 Section 2 - Variables

  Number  .Column. State  ...Value......  .Obj Gradient.  ..Lower Limit.  ..Upper Limit.  .Dual Variable    m+j
 
       1  x      1    BS         0.80377          .              0.10000         1.0             0.00000     16
       2  x      2    BS         0.90000          .              0.10000         1.0             0.00000     17
       3  x      3    BS         0.90000          .              0.10000         1.0            -0.00000     18
       4  x      4 D  BS         0.10000          .              0.00010         0.10000         0.00000     19
       5  x      5    BS         0.19084          .              0.10000         0.90000         0.00000     20
       6  x      6   SBS         0.89407          .              0.10000         0.90000          .          21
       7  x      7    BS       574.09963          .              0.10000      1000.00000        -0.00000     22
       8  x      8    BS        74.09963          .              0.10000      1000.00000        -0.00000     23
       9  x      9    LL       500.00000          .            500.00000      1000.00000         0.14964     24
      10  x     10    LL         0.10000          .              0.10000       500.00000         0.15467     25
      11  x     11    BS        20.23911         1.0             1.0           150.00000         0.00000     26
      12  x     12    BS        77.33645         1.0             0.00010       150.00000         0.00000     27
      13  x     13    BS         0.01547         1.0             0.00010       150.00000        -0.00000     28
 
 Time for MPS input                             0.00 seconds
 Time for solving problem                       0.00 seconds
 Time for solution output                       0.00 seconds
 Time for constraint functions                  0.00 seconds
 Time for objective function                    0.00 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment