Skip to content

Instantly share code, notes, and snippets.

@gnowzil
Created October 28, 2021 21:19
Show Gist options
  • Save gnowzil/5857ad03aae12d57cbba6db7c4fe14d2 to your computer and use it in GitHub Desktop.
Save gnowzil/5857ad03aae12d57cbba6db7c4fe14d2 to your computer and use it in GitHub Desktop.
HS116 matrix structure, nonlinear objective
function [x,F,info]=hs116_matrix()
% HS Problem 116 with explicit linear constraints.
% The Jacobian structure and linear elements are defined as a matrix.
% Linear objective row is in the first row and defined as a "nonlinear" function.
options.name = 'hs116';
options.printfile = 'hs116_mtx.out';
options.specsfile = which('hs116.spc');
options.system_information ='yes';
[x, xlow, xupp, xmul, xstate, ...
Flow, Fupp, Fmul, Fstate, ...
ObjAdd, ObjRow, ...
A, G] = hs116data;
% A.val, A.row, A.col, ...
% G.row, G.col] = hs116data;
[x,F,info]= snopt(x, xlow, xupp, xmul, xstate, ...
Flow, Fupp, Fmul, Fstate, ...
@(x)hs116userfun_1(x), ObjAdd, ObjRow, ...
A, G, options);
end
function [x,xlow,xupp,xmul,xstate, ...
Flow,Fupp,Fmul,Fstate, ...
ObjAdd,ObjRow,A,G] = hs116data()
% hs116data defines problem HS116.
%
% Minimize x(11) + x(12) + x(13)
% subject to linear and nonlinear 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);
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);
%% Jacobian structure as matrix
A = zeros(neF, n);
%A(ObjRow,11) = 1;
%A(ObjRow,12) = 1;
%A(ObjRow,13) = 1;
A(2,1) = -1;
A(2,2) = 1;
A(3,7) = a;
A(3,8) = -a;
A(4,11) = 1;
A(4,12) = 1;
A(4,13) = 1;
A(5,2) = -1;
A(5,3) = 1;
A(6,13) = 1;
A(10,12) = 1;
A(11,11) = 1;
G = zeros(neF,n);
G(ObjRow,13) = 1;
G(ObjRow,12) = 1;
G(ObjRow,11) = 1;
G(6,3) = c*x(10);
G(6,10) = -b + c*x(3);
G(7,2) = -d - e*x(5) + 2*f*x(2);
G(7,5) = 1 - e*x(2);
G(8,3) = -d - e*x(6) + 2*f*x(3);
G(8,6) = 1 - e*x(3);
G(9,1) = -d - e*x(4) + 2*f*x(1);
G(9,4) = 1 - e*x(1);
G(10,2) = c*x(9);
G(10,9) = -b + c*x(2);
G(11,1) = c*x(8);
G(11,8) =-b + c*x(1);
G(12,1) =-x(8);
G(12,4) =-x(7) + x(8);
G(12,5) = x(7);
G(12,7) = x(5) - x(4);
G(12,8) =-x(1) + x(4);
G(13,1) =-a*x(8);
G(13,2) = a*x(9);
G(13,5) = 1 + a*x(8);
G(13,6) = 1 - a*x(9);
G(13,8) = a*(x(5) - x(1));
G(13,9) = a*(x(2) - x(6));
G(14,2) =-500 + x(9) + x(10);
G(14,3) =-x(10);
G(14,6) = 500 - x(9);
G(14,9) = x(2) - x(6);
G(14,10) =-x(3) + x(2);
G(15,2) = 1 - a*x(10);
G(15,3) = a*x(10);
G(15,10) = -a*(x(2) - x(3));
end
function [F,G] = hs116userfun_1(x)
% Defines nonlinear terms of F and derivatives for HS 116.
% Linear constraints are explicit.
% Parameters
a = 2e-3;
b = 1.262626;
c = 1.231059;
d = 3.475e-2;
e = 9.75e-1;
f = 9.75e-3;
F = [ %1, 0;
1, x(11) + x(12) + x(13);
2, 0;
3, 0;
4, 0;
5, 0;
6, -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, -b*x(9) + c*x(2)*x(9); %
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);
if nargout > 1,
% Define the derivatives.
n = 13;
neF = 15;
G = zeros(neF,n);
G(1,13) = 1;
G(1,11) = 1;
G(1,12) = 1;
G(8,3) = -d - e*x(6) + 2*f*x(3);
G(8,6) = 1 - e*x(3);
G(9,1) = -d - e*x(4) + 2*f*x(1);
G(9,4) = 1 - e*x(1);
G(6,3) = c*x(10);
G(6,10) = -b + c*x(3);
G(7,2) = -d - e*x(5) + 2*f*x(2);
G(7,5) = 1 - e*x(2);
G(10,2) = c*x(9);
G(10,9) = -b + c*x(2);
G(11,1) = c*x(8);
G(11,8) = -b + c*x(1);
G(12,1) = -x(8);
G(12,4) = -x(7) + x(8);
G(12,5) = x(7);
G(12,7) = x(5) - x(4);
G(12,8) = -x(1) + x(4);
G(13,1) = -a*x(8);
G(13,2) = a*x(9);
G(13,5) = 1 + a*x(8);
G(13,6) = 1 - a*x(9);
G(13,8) = a*(x(5) - x(1));
G(13,9) = a*(x(2) - x(6));
G(14,2) = -500 + x(9) + x(10);
G(14,3) = -x(10);
G(14,6) = 500 - x(9);
G(14,9) = x(2) - x(6);
G(14,10) = -x(3) + x(2);
G(15,2) = 1 - a*x(10);
G(15,3) = a*x(10);
G(15,10) = -a*(x(2) - x(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
      system information yes
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        13       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..        10       Major feasibility tol..  1.00E-06       Violation limit........  1.00E+06
 Nonlinear Jacobian vars        10

 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.....       Yes
                                                                                 Sticky parameters......        No

 Total char*8  workspace       500       Total integer workspace     21993       Total real    workspace     12451
 Total char*8  (minimum)       500       Total integer (minimum)      1188       Total real    (minimum)      1947
 
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        1.0000E+00  (excluding fixed columns,
 Smallest constant element        2.0000E-03   free rows, and RHS)

 No. of objective coefficients             0

 Nonlinear constraints      10     Linear constraints       5
 Nonlinear variables        13     Linear variables         0
 Jacobian  variables        10     Objective variables     13
 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     4  Free cols     0  Preferred     0
 Unit       1  Double        0  Triangle      0  Pad     0

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

 Itn      1: PP1.  Norm(x-x0) approximately minimized  (0.00E+00)
 Itn      1: PP1.  Making nonlinear variables feasible
 Itn      2: PP1.       1 nonlinear variables made feasible

 
 The user has defined      34   out of      34   first  derivatives

 Verification of user-supplied problem derivatives.

 The constraint gradients seem to be OK.

 -->  The largest discrepancy was    3.90E-07  in constraint    25
 

 The objective  gradients seem to be OK.

 Gradient projected in one direction   7.69161541077E-02
 Difference approximation              7.69161540846E-02

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

      1  5.00000000E-01  8.22E-07         1         9   -1.22500000E-01   -1.22499992E-01  ok
                                          2        11    9.84847200E+01    9.84847200E+01  ok
                                          3        12   -8.00000000E+01   -8.00000000E+01  ok
                                          4        13   -1.60000000E-01   -1.60000000E-01  ok

      2  8.00000000E-01  9.86E-07         5         7   -1.55650000E-01   -1.55649990E-01  ok
                                          6        10    8.00188350E+02    8.00188350E+02  ok
                                          7        13    1.30000000E+00    1.30000000E+00  ok
                                          8        14    6.00000000E+02    6.00000000E+02  ok
                                          9        15    1.00000000E-01    1.00000000E-01  ok

      3  9.00000000E-01  1.04E-06        10         6    5.53976550E+02    5.53976550E+02  ok
                                         11         8   -5.04700000E-01   -5.04699990E-01  ok
                                         12        14   -4.50000000E+02   -4.50000000E+02  ok
                                         13        15    9.00000000E-01    9.00000000E-01  ok

      4  1.00000000E-01 -6.03E-07        14         9    5.12500000E-01    5.12500000E-01  ok
                                         15        12   -4.09000000E+02   -4.09000000E+02  ok

      5  1.40000000E-01  6.24E-07        16         7    2.20000000E-01    2.20000000E-01  ok
                                         17        12    4.89000000E+02    4.89000000E+02  ok
                                         18        13    1.16000000E+00    1.16000000E+00  ok

      6  5.00000000E-01  8.22E-07        19         8    1.22500000E-01    1.22500000E-01  ok
                                         20        13   -3.00000000E-01   -3.00000000E-01  ok
                                         21        14   -1.50000000E+02   -1.50000000E+02  ok

      7  4.89000000E+02  2.68E-04        22        12    4.00000000E-02    4.00000000E-02  ok

      8  8.00000000E+01  4.44E-05        23        11   -6.47096500E-01   -6.47096500E-01  ok
                                         24        12   -4.00000000E-01   -4.00000000E-01  ok
                                         25        13   -7.20000000E-04   -7.20000004E-04  ok

      9  6.50000000E+02  3.57E-04        26        10   -2.77778800E-01   -2.77778800E-01  ok
                                         27        13    6.00000000E-04    6.00000000E-04  ok
                                         28        14    3.00000000E-01    3.00000000E-01  ok

     10  4.50000000E+02  2.47E-04        29         6   -1.54672900E-01   -1.54672900E-01  ok
                                         30        14   -1.00000000E-01   -1.00000000E-01  ok
                                         31        15    2.00000000E-04    2.00000000E-04  ok
     11  9.99999000E+01  5.53E-05           Objective    1.00000000E+00    1.00000000E+00  ok
     12  1.00000000E-04  5.48E-07           Objective    1.00000000E+00    1.00000002E+00  ok
     13  1.50000000E+02 -8.27E-05           Objective    1.00000000E+00    1.00000000E+00  ok

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

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

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

 -->  The largest relative error was    9.94E-09   in column    12
 

 Crash on nonlinear rows:
 Slacks     5  Free cols     0  Preferred     0
 Unit       2  Double        1  Triangle      0  Pad     2
1
 
 Itn      2: Hessian set to a scaled identity matrix
 Itn     15: Large multipliers. Elastic mode started with weight =  1.8E+05
 

   Itns Major Minors    Step   nCon Feasible  Optimal  MeritFunction     L+U BSwap     nS condZHZ Penalty
     17     0     15              1  2.8E-01  1.0E+00  2.5000000E+02      57            5 6.0E+06         _  r i
 Itn     17: Hessian set to a scaled identity matrix
     24     1      7 2.3E-02      2  2.7E-01  1.0E-01  1.4276312E+04      40     2      5 6.1E+06 8.7E-01 _  rli
     41     2     17 5.8E-02      3  2.6E-01  7.2E+00  2.4728621E+05      52     3      5 7.2E+05 1.6E+01 _s  l
     54     3     13 1.0E+00      4  1.2E-03  4.7E+04  3.2116935E+02      45     2      7 3.2E+07 4.0E+00 _
     62     4      8 1.0E+00      6 (1.5E-09) 6.9E-03  2.4670054E+02      52                      4.0E+00 _sM
     66     5      4 1.0E+00      7  5.5E-03  9.2E+01  1.0605467E+02      42                      4.0E+00 _
     67     6      1 1.0E+00      8  5.5E-04  2.8E+01  9.7596267E+01      42                      4.0E+00 _
     67     7      0 1.0E+00      9 (2.8E-08) 1.4E-03  9.7591035E+01      38                      4.0E+00 _
 Itn     67: Elastic weight increased to   1.832E+06
 Itn     67: Elastic weight increased to   1.832E+08
 Itn     67: Elastic weight increased to   1.832E+10
     67     8      0 1.0E+00     10 (2.0E-16)(2.3E-15) 9.7591035E+01      38                      4.0E+00 _
1
 
 SNOPTA EXIT   0 -- finished successfully
 SNOPTA INFO   1 -- optimality conditions satisfied

 Problem name                 hs116
 No. of iterations                  67   Objective            9.7591034664E+01
 No. of major iterations             8   Linear    obj. term  0.0000000000E+00
 Penalty parameter           4.006E+00   Nonlinear obj. term  9.7591034664E+01
 User function calls (total)        24
 No. of degenerate steps             1   Percentage                       1.49
 Max x                       7 5.7E+02   Max pi                      5 2.1E+03
 Max Primal infeas          26 1.1E-13   Max Dual infeas            24 1.0E+00
 Nonlinear constraint violn    1.1E-13
1
 
 Name           hs116                    Objective Value      9.7591034664E+01

 Status         Optimal Soln             Iteration     67    Superbasics     0

 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 D  BS         1.00000        -0.00000           None          1.0              .           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         0.00000         0.00000          .                None          1.00000      6
      20  r      7    LL        -0.00000        -0.00000          .                None       2088.38822      7
      21  r      8    BS         0.08687         0.08687          .                None           .           8
      22  r      9    LL        -0.00000        -0.00000          .                None       1065.74685      9
      23  r     10    LL        -0.00000        -0.00000          .                None          1.00000     10
      24  r     11    LL         0.00000         0.00000          .                None          1.00000     11
      25  r     12 A  LL         0.00000         0.00000          .                None           .          12
      26  r     13    UL         1.00000         0.00000           None          1.0          -222.80763     13
      27  r     14    LL        -0.00000        -0.00000          .                None          1.29436     14
      28  r     15    LL         0.90000          .              0.90000           None         31.65296     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    UL         0.10000          .              0.00010         0.10000      -230.54361     19
       5  x      5    BS         0.19084          .              0.10000         0.90000        -0.00000     20
       6  x      6 A  UL         0.90000          .              0.10000         0.90000          .          21
       7  x      7    BS       574.09964          .              0.10000      1000.00000          .          22
       8  x      8    BS        74.09964          .              0.10000      1000.00000        -0.00000     23
       9  x      9    LL       500.00000          .            500.00000      1000.00000         0.15467     24
      10  x     10    LL         0.10000          .              0.10000       500.00000         0.15467     25
      11  x     11    BS        20.23912         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