C#による 非線形計画問題(スパース)

C#によるサンプルソースコード : 使用関数名:e04ug

Keyword: 非線形計画問題, スパース

概要

本サンプルは非線形計画問題(スパース)を解くC#によるサンプルプログラムです。 本サンプルは以下に示される非線形関数を最小化する解を求めて出力します。

非線形計画問題(スパース)のデータ 

※本サンプルはnAG Library for .NETに含まれる関数 e04ug() のExampleコードです。本サンプル及び関数の詳細情報は e04ug のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はe04ug のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

このデータをダウンロード
e04ug Example Program Data
 4   6              :Values of N and M
 3   4   2          :Values of NCNLN, NONLN and NJNLN
14   6  'C'  10     :Values of NNZ, IOBJ, START and NNAME
'Varble 1'  'Varble 2'  'Varble 3'  'Varble 4'  'NlnCon 1'
'NlnCon 2'  'NlnCon 3'  'LinCon 1'  'LinCon 2'  'Free Row' :End of NAMES
 1.0E+25   1   1
 1.0E+25   2   1
 1.0E+25   3   1
     1.0   5   1
    -1.0   4   1
 1.0E+25   1   2
 1.0E+25   2   2
 1.0E+25   3   2
    -1.0   5   2
     1.0   4   2
     3.0   6   3
    -1.0   1   3
    -1.0   2   4
     2.0   6   4    :End of matrix A
-0.55     -0.55      0.0       0.0     -894.8   -894.8   -1294.8  -0.55
-0.55     -1.0E+25  :End of BL
 0.55      0.55      1200.0    1200.0  -894.8   -894.8   -1294.8   1.0E+25
 1.0E+25   1.0E+25  :End of BU
 0    0    0    0   :End of ISTATE 
 0.0  0.0  0.0  0.0 :End of XS
 0.0  0.0  0.0      :End of CLAMDA 

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に変数の数(n)、一般制約の数(m)を指定しています。
  • 3行目に非線形制約の数(ncnln)、非線形目的変数の数(nonln)、非線形ヤコビ変数の数(njnln)を指定しています。
  • 4行目の1番目のパラメータ(nnz)はヤコビ行列Aの非ゼロ要素の数、2番目のパラメータ(iobj)は目的関数の線形部分の非ゼロ要素を含む行を指定しています。3番目のパラメータ(start)はどのようにプログラム開始時の基底行列が得られるかを指定しています。""C"は初期の基底行列を選択するためにCrash処理が使用されることを意味します。4番目のパラメータ(nname)は列と行の名前の数を指定しています。
  • 5~6行目に列と行の名前(names)を指定しています。
  • 7~20行目にヤコビ行列の非ゼロ要素の値(a)、行のインデックス(ha)、列のインデックス(icol)を指定しています。
  • 21~22行目に変数の下限と制約の下限(bl)を指定しています。
  • 23~24行目に変数の上限と制約の上限(bu)を指定しています。
  • 25行目に変数xの初期の状態(istate)を指定しています。"0"は基底に適していることを意味します。
  • 26行目に変数xの初期値(xs)を指定しています。
  • 27行目にラグランジュ乗数の推定値(clamda)を指定しています。

出力結果

(本関数の詳細はe04ug のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

この出力例をダウンロード
e04ug Example Program Results
Computed values of the size of z and iz are 758, 628 respectively.


 Calls to E04UJF
 ---------------

      List
      Print level = 10

 *** E04UGF

 Parameters
 ----------

 Frequencies.
 Check frequency.........        60       Expand frequency.......     10000
 Factorization frequency.        50

 QP subproblems.
 Scale tolerance.........  9.00E-01       Minor feasibility tol..  1.05E-08
 Scale option............         1       Minor optimality tol...  1.05E-08
 Partial price...........         1       Crash tolerance........  1.00E-01
 Pivot tolerance.........  2.04E-11       Minor print level......         0
 Crash option............         0       Elastic weight.........  1.00E+02

 The SQP method.
 Minimize................
 Nonlinear objective vars         4       Major optimality tol...  1.05E-08
 Function precision......  1.72E-13       Unbounded step size....  1.00E+20
 Superbasics limit.......         4       Forward difference int.  4.15E-07
 Unbounded objective.....  1.00E+15       Central difference int.  5.56E-05
 Major step limit........  2.00E+00       Derivative linesearch..
 Derivative level........         3       Major iteration limit..      1000
 Linesearch tolerance....  9.00E-01       Verify level...........         0
 Minor iteration limit...       500       Major print level......        10
 Infinite bound size.....  1.00E+20       Iteration limit........     10000

 Hessian approximation.
 Hessian full memory.....                 Hessian updates........  99999999
 Hessian frequency.......  99999999

 Nonlinear constraints.
 Nonlinear constraints...         3       Major feasibility tol..  1.05E-08
 Nonlinear Jacobian vars.         2       Violation limit........  1.00E+01

 Miscellaneous.
 Variables...............         4       Linear constraints.....         3
 Nonlinear variables.....         4       Linear variables.......         0
 LU factor tolerance.....  5.00E+00       LU singularity tol.....  2.04E-11
 LU update tolerance.....  5.00E+00       LU density tolerance...  6.00E-01
 eps (machine precision).  1.11E-16       Monitoring file........        -1
 COLD start..............                 Infeasible exit........

 Workspace provided is                 IZ(    1000),  Z(    1000).
 To start solving the problem we need  IZ(     628),  Z(     758).

 Itn      0 -- Scale option reduced from                1 to                0.

 Itn      0 -- Feasible linear rows.

 Itn      0 -- Norm(x-x0) minimized. Sum of infeasibilities = 0.00E+00.

 confun  sets       6   out of       6   constraint gradients.
 objfun  sets       4   out of       4   objective  gradients.

 Cheap test on confun...

 The Jacobian seems to be OK.

 The largest discrepancy was    4.41E-08  in constraint     2.

 Cheap test on objfun...

 The objective gradients seem to be OK.
 Gradient projected in two directions   0.00000000000E+00   0.00000000000E+00
 Difference approximations              1.74111992322E-19   4.48742248252E-21

 Itn      0 -- All-slack basis B = I selected.

 Itn      7 -- Large multipliers.
               Elastic mode started with weight =  2.0E+02.


  Maj  Mnr    Step Merit Function Feasibl Optimal Cond Hz PD
    0   12 0.0E+00   3.199952E+05 1.7E+00 8.0E-01 2.1E+06 FF  R   i
    1    2 1.0E+00   2.463016E+05 1.2E+00 3.2E+03 4.5E+00 FF s
    2    1 1.0E+00   1.001802E+04 3.3E-02 9.2E+01 4.5E+00 FF
    3    1 1.0E+00   5.253418E+03 6.6E-04 2.5E+01 4.8E+00 FF
    4    1 1.0E+00   5.239444E+03 2.0E-06 2.8E+01 1.0E+02 FF
    5    1 1.0E+00   5.126208E+03 6.0E-04 5.9E-01 1.1E+02 FF
    6    1 1.0E+00   5.126498E+03 4.7E-07 2.9E-02 1.0E+02 FF
    7    1 1.0E+00   5.126498E+03 5.9E-10 1.5E-03 1.1E+02 TF
    8    1 1.0E+00   5.126498E+03 1.2E-12 7.6E-09 1.1E+02 TT

 Exit from NP problem after     8 major iterations,
                               21 minor iterations.

 Variable State     Value       Lower Bound  Upper Bound   Lagr Mult  Residual

 Varble 1    BS   0.118876     -0.55000      0.55000     -1.2529E-07  0.4311
 Varble 2    BS  -0.396234     -0.55000      0.55000      1.9246E-08  0.1538
 Varble 3    BS    679.945           .        1200.0      1.7001E-10   520.1
 Varble 4   SBS    1026.07           .        1200.0     -2.1918E-10   173.9

 Constrnt State     Value       Lower Bound  Upper Bound   Lagr Mult  Residual

 NlnCon 1    EQ   -894.800      -894.80      -894.80      -4.387      3.3644E-09
 NlnCon 2    EQ   -894.800      -894.80      -894.80      -4.106      6.0061E-10
 NlnCon 3    EQ   -1294.80      -1294.8      -1294.8      -5.463      3.3554E-09
 LinCon 1    BS  -0.515110     -0.55000          None          .      3.4890E-02
 LinCon 2    BS   0.515110     -0.55000          None          .       1.065
 Free Row    BS    4091.97          None         None     -1.000       4092.

 Exit E04UGF - Optimal solution found.

 Final objective value =     5126.498

 ** e04ug returned with ifail =   0

  Variable    Istate          Value                  Lagr Mult

  Varble   1    3              0.118876      -1.253e-07
  Varble   2    3             -0.396234       1.925e-08
  Varble   3    3               679.945         1.7e-10
  Varble   4    2               1026.07      -2.192e-10


  Constrnt    Istate          Value                  Lagr Mult

  NlnCon   1    0                -894.8          -4.387
  NlnCon   2    0                -894.8          -4.106
  NlnCon   3    0               -1294.8          -5.463
  LinCon   1    3              -0.51511               0
  LinCon   2    3               0.51511               0
  Free Row      3               4091.97              -1


  Final objective value =        5126.498

  • 2行目に作業域 z と iz のサイズがそれぞれ 758 と 628 であることが示されています。
  • 9行目にPrint levl に"10"が設定されていることが示されています。
  • 16~53行目に以下に示すプログラムのオプション引数が出力されています。
    Check frequency 因数分解後にこの回数の反復ごとに現在の解が一般線形制約を満たしているかを確認するための数値テストが行われます。
    Expand frequency この反復数を超えると実行可能許容値が増加します。
    Factorization frequency 最大でこの回数の基底変換が基底行列の因数分解で生じます。
    Scale tolerance スケーリングパスの数を制御します。
    Minor feasibiity tol 全ての変数の下限・上限がこの許容値の範囲内かどうか確認されます。
    Scale option 変数や制約のスケーリング。"1"は全ての線形制約と変数がスケーリングされることを意味します。
    Minor optimality tol QP子問題の最適化の判定に使用されます。
    Partial price プライシングに必要な処理を軽減します。"1"は制約行列の全てのカラムが探索されることを意味します。
    Crash tolerance Crash処理で三角基底の検索時に行列Aの非ゼロ要素を無視できるようにします。
    Pivot tolerance カラムが基底行列に入ることで基底行列が特異行列になるのを防ぐために使用されます。
    Minor print level 小さな反復の出力結果を制御します。"0"は何も出力されないことを意味します。
    Crash option 行列Aのどの行や列が基底に値するかを決定し、Crash処理が何回呼び出されるかを示します。 "0"は初期基底がB=Iとなるスラック変数のみを含むことを意味します。
    Elastic weight 初期の重み。
    Minimize 最適化の方向が最小化であることを意味します。
    Nonlinear Objective vars 非線形目的変数。
    Major optimality tol 双対変数の最終的な精度。
    Function precision 非線形関数が計算できる相対的精度の基準となる相対関数。
    Unbound step size f(x + αp)のαがこの値に達したら反復が終了します。
    Superbasics limit superbasic変数に割り当てられた記憶域の上限。
    Forward difference int 導関数の推定に使用される区間。
    Unbounded objective 目的関数fの絶対値がこの値に達したら反復が終了します。
    Central difference int 勾配のより正確な推定値を得るために最適解の近くで使用されます。
    Major step limit 直線探索(linesearch)の際のxの変量の上限。
    Derivative linesearch 直線探索(linesearch)が三次補間を使用することを意味します。
    Derivative level どの非線形関数勾配が与えられるかを示しています。"3"は目的関数の勾配と制約関数のヤコビ行列の全ての要素が与えられることを意味します。
    Major iteration limit 大きな反復の最大数。
    Linesearch tolerance 各反復の検索の際の精度を制御します。
    Verify level 勾配要素の有限差分のチェックのレベル。"0"は簡易なチェックのみ行われることを意味します。
    Minor iteration limit 非線形制約の一連の制約化で可能な反復の最大数。
    Major print level 大きな反復によって生成される出力を制御します。"10"は最終解と各反復のサマリーが出力されることを意味します。
    Infinite bound size 問題制約の定義での無限境界。
    Iteration limit 小さな反復の最大数。
    Hessian full memory ラグランジュ関数のヘッセ行列の準ニュートン近似の格納及び更新の手法を指定しており、近似ヘッセ行列が密行列として処理されることを意味します。
    Hessian updates ヘッセ行列更新ベクトルのペアの最大数。
    Hessian frequency この回数のBFGS(Broyden-Fletcher-Goldfarb-Shanno)更新から形成される近似ヘッセ行列をリセットします。
    Nonlinear constraints 非線形制約。
    Major feasibility tol 非線形制約の精度。
    Nonlinear Jacobian vars 非線形ヤコビ変数。
    Violation limit 直線探索(linesearch)後の最大制約違反の大きさの絶対限度。
    Variables 変数。
    Linear constraints 線形制約。
    Nonlinear variables 非線形変数。
    Linear variables 線形変数。
    LU factor tolerance 再因数分解時の基底因数分解の安定性及びスパース性を制御します。
    LU singularity tol 悪条件の基底行列を防ぐために使用される特異点許容値。
    LU update tolerance 更新時の基底因数分解の安定性及びスパース性を制御します。
    LU density tolerance 基底行列のLU分解時に使用される密度許容値。
    eps (machine precision) マシン精度。
    Monitoring file モニタリングファイル。"-1"はモニタリング情報が出力されないことを意味します。
    COLD start プログラムがCOLD startすることを示しています。
    Infeasible exit 非線形制約について実行可能点を見つけるための追加の反復が実行されないことを意味します。
  • 58~60行目にはスケールオプションが1から0に減少したことと、実行可能な線形の行数が出力されています。
  • 64行目には関数confunが6つの制約勾配から6つを設定したことが出力されています。
  • 65行目には関数objfunが4つの目的関数の勾配から4つを設定したことが出力されています。
  • 69行目にはヤコビ行列に問題がないことが出力されています。
  • 71行目には制約の最大の相違が出力されています。
  • 75行目には目的関数の勾配に問題がないことが出力されています。
  • 76行目には推定される勾配が出力されています。
  • 77行目には差分近似が出力されています。
  • 82行目には弾性モード開始時の重みが出力されています。
  • 85~94行目には以下が出力されています。
    Maj 大きな反復の回数。
    Mnr 実行可能解生成フェーズや最適化フェーズで必要とされる小さな反復の回数。
    Step 探索方向に進んだステップ幅。
    Merit Function ラグランジュメリット関数の値。
    Feasibl スケーリングされた非線形制約残差ベクトルの最大要素の値。
    Optimal 最大の補間ギャップベクトルの最大要素の値。
    Cond Hz ラグランジュの縮小ヘッセ行列の条件数の推定値。
    PD 実行可能性と最適性についての収束判定の状況。 "T"は満たしていることを意味し、"F"は満たしていないことを意味します。さらに右側に表示されている"R"は近似ヘッセ行列がリセットされたことを意味し、"i"はQP子問題が実行不可能であることを意味し、"s"は自己スケーリング BFGS(Broyden-Fletcher-Goldfarb-Shanno)更新が実行されたことを意味します。
  • 96~97行目にはNP問題を解くのに8回の大きな反復と21回の小さな反復が行われたことが示されています。
  • 99~104行目には以下が出力されています。
    Variable 変数の名前。
    State 変数の状態("BS"は基底、"SBS"はsuperbasicであることを意味します)。
    Value 最後の反復の変数の値。
    Lower Bound 下限。
    Upper Bound 上限。
    Lagr Mult 関連する境界制約のラグランジュ乗数。
    Residual 残差(変数の値と上限/下限の近いほうとの差)。
  • 106~113行目には以下が出力されています。
    Constrnt 制約の名前。
    State 制約の状態("EQ"は非基底で固定、"BS"は基底であることを意味します)。
    Value 最後の反復の制約の値。
    Lower Bound 下限。
    Upper Bound 上限。
    Lagr Mult 関連する境界制約のラグランジュ乗数。
    Residual 残差(制約の値と上限/下限の近いほうとの差)。
  • 115行目に最適解が見つかったことが示されています。
  • 117行目に最終的な目的関数値が出力されています。
  • 121~126行目に変数の名前、変数の状態("3"は基底、"2"はsuperbasic)、値、ラグランジュ乗数が出力されています。
  • 129~136行目に制約の名前、制約の状態("0"は非基底、"3"は基底)、値、ラグランジュ乗数が出力されています。
  • 139行目に最終的な目的関数値が出力されています。

ソースコード

(本関数の詳細はe04ug のマニュアルページを参照)

※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

このソースコードをダウンロード
//      e04ug Example Program Text
//      C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
  public class E04UGE
  {
    static bool defaultdata = true;
    static string datafile = "";
    // as a command line argument. It defaults to the named file specified below otherwise.
    //
    static void Main(String[] args)
    {
      if (args.Length == 1)
      {
        defaultdata = false;
        datafile = args[0];
      }
      StartExample();
    }
    public static void StartExample()
    {
      E04.E04UG_CONFUN confunE04UG = new E04.E04UG_CONFUN(confun);
      E04.E04UG_OBJFUN objfunE04UG = new E04.E04UG_OBJFUN(objfun);
      try 
      {
        DataReader sr = null;
        if(defaultdata)
        { sr = new DataReader("exampledata/e04uge.d");
      }
      else
      {
        sr = new DataReader(datafile);
      }
      double obj,   sinf; int i,  icol=0,  iobj,  j,  jcol,  m,  miniz,  minz,  n,  ncnln,  ninf,  njnln,  nname,  nnz,  nonln,  ns=0;
      string start="";
      int idummy = -11111;
      string[] names = new string[200];
      int ifail;
      Console.WriteLine("e04ug Example Program Results");
      //      Skip heading in data file.
      sr.Reset();
      sr.Reset();
      n = int.Parse(sr.Next());
      m = int.Parse(sr.Next());
      //
      //         Read ncnln, nonln and njnln from data file.
      //
      sr.Reset();
      ncnln = int.Parse(sr.Next());
      nonln = int.Parse(sr.Next());
      njnln = int.Parse(sr.Next());
      //
      //         Read nnz, iobj, start, nname and names from data file.
      //
      sr.Reset();
      nnz = int.Parse(sr.Next());
      iobj = int.Parse(sr.Next());
      start = sr.Next();
      nname = int.Parse(sr.Next());
      double[] a = new double[nnz];
      double[] bl = new double[n+m];
      double[] bu = new double[n+m];
      double[] clamda = new double[n+m];
      double[] user = new double[1];
      double[] xs = new double[n+m];
      int[] ha = new int[300];
      int[] istate = new int[n+m];
      int[] iuser = new int[1];
      int[] ka = new int[n+1];
      if (nname == n + m)
      {
        sr.Reset();
        for (i = 1 ; i <= n + m ; i++)
        {
          names[i - 1] = sr.Next();
        }
      }
      //
      //         Initialize ka.
      //
      for (i = 1 ; i <= n + 1 ; i++)
      {
        ka[i - 1] = idummy;
      }
      //
      //         Read the matrix a from data file. Set up ka.
      //
      jcol = 1;
      ka[jcol - 1] = 1;
      for (i = 1 ; i <= nnz ; i++)
      {
        //
        //            Element ( ha( i-1 ), icol-1 ) is stored in a[ i-1].
        //
        sr.Reset();
        a[i - 1] = double.Parse(sr.Next());
        ha[i - 1] = int.Parse(sr.Next());
        icol = int.Parse(sr.Next());
        //
        if (icol < jcol)
        {
          //
          //               Elements not ordered by increasing column index.
          //
          Console.WriteLine("\n  {0}{1,5}{2}{3,5}{4}{5}","Element in column",icol," found after element in column",jcol,". Problem"," abandoned.");
          goto L160;
        }
        else if (icol == jcol + 1)
        {
          //
          //               Index in a of the start of the icol-th column equals i.
          //
          ka[icol - 1] = i;
          jcol = (int)icol;
        }
        else if (icol > jcol + 1)
        {
          //
          //               Index in a of the start of the icol-th column equals i,
          //               but columns jcol+1,jcol+2,...,icol-1 are empty. Set the
          //               corresponding elements of ka to i.
          //
          for (j = jcol + 1 ; j <= icol - 1 ; j++)
          {
            ka[j - 1] = i;
          }
          ka[icol - 1] = i;
          jcol = (int)icol;
        }
      }
      //
      ka[n + 1 - 1] = nnz + 1;
      //
      if (n > icol)
      {
        //
        //            Columns n,n-1,...,icol+1 are empty. Set the corresponding
        //            elements of ka accordingly.
        //
        for (i = n ; i <= icol + 1 ; i +=  -1)
        {
          if (ka[i - 1] == idummy)
          {
            ka[i - 1] = ka[i + 1 - 1];
          }
        }
      }
      //
      //         Read bl, bu, istate, xs and clamda from data file.
      //
      sr.Reset();
      for (i = 1 ; i <= n + m ; i++)
      {
        bl[i - 1] = double.Parse(sr.Next());
      }
      sr.Reset();
      for (i = 1 ; i <= n + m ; i++)
      {
        bu[i - 1] = double.Parse(sr.Next());
      }
      if (start == "C")
      {
        sr.Reset();
        for (i = 1 ; i <= n ; i++)
        {
          istate[i - 1] = int.Parse(sr.Next());
        }
      }
      else if (start == "W")
      {
        sr.Reset();
        for (i = 1 ; i <= n + m ; i++)
        {
          istate[i - 1] = int.Parse(sr.Next());
        }
      }
      sr.Reset();
      for (i = 1 ; i <= n ; i++)
      {
        xs[i - 1] = double.Parse(sr.Next());
      }
      if (ncnln > 0)
      {
        sr.Reset();
        for (i = n + 1 ; i <= n + ncnln ; i++)
        {
          clamda[i - 1] = double.Parse(sr.Next());
        }
      }
      // Switch off all output.
      PrintManager.Warning = new PrintManager.MessageLogger(discardmessage);
      PrintManager.Message = new PrintManager.MessageLogger(discardmessage);
      //
      //         Initialise E04.e04ug 
      //
      E04.e04ugOptions options = new E04.e04ugOptions();
      //
      //            Solve the problem.
      //
      start="C";
      // First get appropriate minimum lengths of arrays, z and iz
      int leniz = 500;
      int lenz = 500;
      double[] z = new double[lenz];
      int[] iz = new int[leniz];
      E04.e04ug(confunE04UG, objfunE04UG, n, m, ncnln, nonln, njnln, iobj, nnz, a, ha, ka, bl,
      bu, start, nname, names, ref ns, xs, istate, clamda, out miniz, out minz, out ninf, out sinf,
      out obj, iz, z, options, out ifail);
      Console.WriteLine("Computed values of the size of z and iz are {0}, {1} respectively.", minz, miniz);
      //         Initialise E04.e04ug again and check for error exits
      PrintManager.Warning = new PrintManager.MessageLogger(printmessage);
      PrintManager.Message = new PrintManager.MessageLogger(printmessage);
      options = new E04.e04ugOptions();
      options.Set("List");
      options.Set("Print level = 10");
      //
      //            Solve the problem.
      //
      start="C";
      // The minimum values of lenz and leniz found above are only a preliminary estimate.
      // In practice, larger values need to be chosen frequently, we have therefore specified 
      // each of leniz and lenz as 1000.
      leniz = 1000;
      lenz = 1000;
      z = new double[lenz];
      iz = new int[leniz];
      E04.e04ug(confunE04UG, objfunE04UG, n, m, ncnln, nonln, njnln, iobj, nnz, a, ha, ka, bl,
      bu, start, nname, names, ref ns, xs, istate, clamda, out miniz, out minz, out ninf, out sinf,
      out obj, iz, z, options, out ifail);
      //
      //            Check for error exits
      //
      Console.WriteLine("");
      if (ifail >= (7))
      {
        Console.WriteLine(" ** e04ug returned with ifail = {0, 3}", ifail);
      }
      else if (ifail >= 0)
      {
        Console.WriteLine(" ** e04ug returned with ifail = {0, 3}", ifail);
        Console.WriteLine("");
        Console.WriteLine("  Variable    Istate          Value                  Lagr Mult");
        Console.WriteLine("");
        for (i = 1 ; i <= n ; i++)
        {
          Console.WriteLine("  Varble  {0,2}  {1,3}        {2,14:g6}    {3,12:g4}",i,istate[i - 1],xs[i - 1],clamda[i - 1]);
        }
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("  Constrnt    Istate          Value                  Lagr Mult");
        Console.WriteLine("");
        if (ncnln > 0)
        {
          for (i = n + 1 ; i <= n + ncnln ; i++)
          {
            j = i - n;
            Console.WriteLine("  NlnCon  {0,2}  {1,3}        {2,14:g6}    {3,12:g4}",j,istate[i - 1],xs[i - 1],clamda[i - 1]);
          }
        }
        if ((((ncnln == 0)) && ((m == 1))) && ((a[0] == 0.00e0)))
        {
          Console.WriteLine("  DummyRow    {0,3}        {1,14:g6}    {2,12:g4}",istate[n + 1 - 1],xs[n + 1 - 1],clamda[n + 1 - 1]);
        }
        else if (m > ncnln)
        {
          for (i = n + ncnln + 1 ; i <= n + m ; i++)
          {
            j = i - n - ncnln;
            if (i - n == iobj)
            {
              Console.WriteLine("  Free Row    {0,3}        {1,14:g6}    {2,12:g4}",istate[i - 1],xs[i - 1],clamda[i - 1]);
            }
            else
            {
              Console.WriteLine("  LinCon  {0,2}  {1,3}        {2,14:g6}    {3,12:g4}",j,istate[i - 1],xs[i - 1],clamda[i - 1]);
            }
          }
        }
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("  Final objective value = {0,15:g7}",obj);
      }
      else
      {
        Console.WriteLine(" ** e04ug returned with ifail = {0, 3}", ifail);
      }
      //
      L160: ;
      //
      }catch (Exception e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine( "Exception Raised");
      }
    }
    //
    public static void confun (ref int mode, int ncnln, int njnln, int nnzjac,
    double[] x, double[] f, double[] fjac, int nstate)
    {
      //      Computes the nonlinear constraint functions and their Jacobian.
      if ((mode == 0) || (mode == 2))
      {
        f[0] = 1000.00e+0 * Math.Sin( -x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin( -x[1] - 0.250e+0);
        f[1] = 1000.00e+0 * Math.Sin(x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[0] - x[1] - 0.250e+0);
        f[2] = 1000.00e+0 * Math.Sin(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[1] - 0.250e+0);
      }
      //
      if ((mode == 1) || (mode == 2))
      {
        //
        //         Nonlinear Jacobian elements for column 1.
        //
        fjac[0] =  -1000.00e+0 * Math.Cos( -x[0] - 0.250e+0);
        fjac[1] = 1000.00e+0 * Math.Cos(x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0);
        fjac[2] =  -1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0);
        //
        //         Nonlinear Jacobian elements for column 2.
        //
        fjac[3] =  -1000.00e+0 * Math.Cos( -x[1] - 0.250e+0);
        fjac[4] =  -1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0);
        fjac[5] = 1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[1] - 0.250e+0);
      }
      //
    }
    //
    public static void objfun (ref int mode, int nonln, double[] x, ref double objf,
    double[] objgrd, int nstate)
    {
      //      Computes the nonlinear part of the objective function and its
      //      gradient
      if ((mode == 0) || (mode == 2))
      {
        objf = 1.00e-6 * Math.Pow(x[2] ,3) + 2.00e-6 * Math.Pow(x[3] ,3) / 3.00e+0;
      }
      //
      if ((mode == 1) || (mode == 2))
      {
        objgrd[0] = 0.00e+0;
        objgrd[1] = 0.00e+0;
        objgrd[2] = 3.00e-6 * (x[2])*(x[2]);
        objgrd[3] = 2.00e-6 * (x[3])*(x[3]);
      }
      //
    }
    static void discardmessage(String message)
    {
    }
    static void printmessage(String message)
    {
      Console.WriteLine(message);
    }
  }
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks