C#による 大域的最適化

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

Keyword: 大域的最適化

概要

本サンプルは大域的最適化を行うC#によるサンプルプログラムです。 本サンプルは以下に示される2次元におけるpeaks関数の大域的最小値を求めて出力します。

大域的最適化のデータ 

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

入力データ

(本関数の詳細はe05jb のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

このデータをダウンロード
e05jc Example Program Data
  3                                                     : SDLIST
  0                                                     : IBOUND
  -3.0   -3.0                                           : Lower bounds BL
  3.0   3.0                                             : Upper bounds BU
  3                                                     : IINIT
  3   3   3                                             : NUMPTS
  -3.0   -1.0   3.0   -3.0   0.0   3.0                  : Matrix LIST
  2   2   2                                             : INITPT
  F                                                     : LPLOT
Begin example options file
* Comment lines like this begin with an asterisk
* Set the maximum number of function evaluations
Function Evaluations Limit = 100000
* Set the local search termination tolerance
Local Searches Tolerance = 1.0D-10
* Set the maximum number of times a given box may be split
Splits Limit = 20
End

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に初期化リストに従って分割が行われる座標の点の数の最大値(sdlist)を指定しています。
  • 3行目には境界値を処理するための機能が使用されるかを示すパラメータ(ibound)指定しています。"0"は下限と上限をそれぞれ与えることを意味します。
  • 4行目には下限(bl)を指定しています。
  • 5行目には上限(bu)を指定しています。
  • 6行目にはどの初期化の手法が使用されるかを示すパラメータ(IINIT)指定しています。"3"はユーザが初期化リストを提供することを意味しています。
  • 7行目には分割が行われる座標の点の数(numpts)を指定しています。
  • 8行目には初期化リスト(list)を指定しています。
  • 9行目には関数が初期の座標点と見なすようなリスト上の点(initpt)を指定しています。
  • 10行目に最新の検索ボックス(領域)の情報を表示するかどうかを示すパラメータ(lplot)を指定しています。"F"は表示しないことを意味します。
  • 11~19行目には以下に示すオプション・パラメータを設定するオプションファイルを定義しています。
    Function Evaluations Limit 関数の呼び出し回数の近似限界。
    Local Searches Tolerance 近似勾配が小さい場合に停止する基準として局所探索の際に使用される乗数。
    Splits Limit ボックス(領域)の分割回数の限界。

出力結果

(本関数の詳細はe05jb のマニュアルページを参照)
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

この出力例をダウンロード
e05jb Example Program Results


  (OBJFUN was just called for the first time)

  *** Begin monitoring information ***

  Total sub-boxes =  180
  Total function evaluations =  186
  Total function evaluations used in local search =  103
  Total points used in local search =    9
  Total sweeps through levels =    9
  Total splits by init. list =    5
  Lowest level with nonsplit boxes =    6
  Number of candidate minima in the "shopping basket" =    2
  Shopping basket:
xbaskt(  1,:) =    0.22828  -1.34740
xbaskt(  2,:) =   -1.62553   0.20452

  *** End monitoring information ***


  On exit from e05jb, ifail =    0
  Final objective value =   -6.55113
Global optimum x =    0.22828  -1.62553

  • 8~18行目に以下に示すモニタリング情報が出力されています。
    • サブボックス(下位領域)の数
    • 関数objfunの呼び出しの累積数
    • 局所検索での関数objfunの呼び出しの累積数
    • 局所検索の開始点として使用される座標点の数
    • 分割のレベルを通じたスイープ(sweep)の累積数
    • 初期化リストによる分割の累積数
    • 分割していないボックス(領域)を含む、最も低い分割のレベル
    • ショッピングバスケット(最小値の候補の格納場所)の最小値の候補の座標点の数
    • 最小値の候補が格納されているショッピングバスケットの内容
  • 23行目にエラーを検知せずに本関数 e05jb を終了したことを示しています。
  • 24行目に最終的な関数値が出力されています。
  • 25行目に大域的最適解xの値が出力されています。

ソースコード

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

※本サンプルソースコードは .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

このソースコードをダウンロード
//      e05jb Example Program Text
// 
//      C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
  public class E05JBE
  {
    const int n=2;
    static bool defaultdata = true;
    static string datafile = "";
    static bool lplot;
    static void Main(String[] args)
    {
      if (args.Length == 1)
      {
        defaultdata = false;
        datafile = args[0];
      }
      StartExample();
    }
    public static void StartExample()
    {
      try
      {
        DataReader sr = null;
        if (defaultdata)
        {
          sr = new DataReader("exampledata/e05jbe.d");
        }
        else
        {
          sr = new DataReader(datafile);
        }
        //      values of optional parameters associated with e05jb
        // 
        double infbnd,   obj; int i,  ibound,  iinit,  j,  sdlist,  stclim;
        string lcsrch="";
        E05.E05JB_OBJFUN objfunE05JB = new E05.E05JB_OBJFUN(objfun);
        E05.E05JB_MONIT monitE05JB = new E05.E05JB_MONIT(monit);
        // 
        int ifail=0;
        Console.WriteLine("e05jb Example Program Results");
        // 
        //      Skip heading in data file
        // 
        sr.Reset();
        // 
        //      Read sdlist from data file
        // 
        sr.Reset();
        sdlist = int.Parse(sr.Next());
        double[] bl = new double[n];
        double[] bu = new double[n];
        double[,] list = new double[n, sdlist];
        double[] ruser = new double[1];
        double[] x = new double[n];
        int[] initpt = new int[n];
        int[] numpts = new int[n];
        // 
        // 
        //         Read ibound, bl, and bu from data file
        // 
        sr.Reset();
        ibound = int.Parse(sr.Next());
        // 
        if (ibound == 0)
        {
          // 
          //            Read in the whole of each bound
          // 
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            bl[i - 1] = double.Parse(sr.Next());
          }
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            bu[i - 1] = double.Parse(sr.Next());
          }
          // 
        }
        else if (ibound == 3)
        {
          // 
          //            Bounds are uniform: read in only the first entry of each
          // 
          sr.Reset();
          bl[0] = double.Parse(sr.Next());
          sr.Reset();
          bu[0] = double.Parse(sr.Next());
          // 
        }
        // 
        //         Read in iinit (and list, numpts and initpt if necessary)
        //         from data file
        // 
        sr.Reset();
        iinit = int.Parse(sr.Next());
        // 
        if (iinit == 3)
        {
          // 
          //            User is specifying the initialization list
          // 
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            numpts[i - 1] = int.Parse(sr.Next());
          }
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            for (j = 1 ; j <= numpts[i - 1] ; j++)
            {
              list[i - 1 , j - 1] = double.Parse(sr.Next());
            }
          }
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            initpt[i - 1] = int.Parse(sr.Next());
          }
          // 
        }
        // 
        //         Read lplot. Its value determines whether monit displays
        //         information on the current search box
        // 
        sr.Reset();
        lplot = bool.Parse(sr.Next());
        // 
        //         Initialize e05jb
        // 
        E05.e05jbOptions options = new E05.e05jbOptions();
        // 
        if (ifail >= 0)
        {
          // 
          //            Use e05jb to read some options from the end of the data
          //            file
          // 
          options.Set(sr);
          Console.WriteLine("");
          // 
          //            Set 'Static Limit'
          // 
          stclim = 4 * n;
          options.Set("Static Limit", stclim);
          // 
          //            Set 'Infinite Bound Size' tenfold
          // 
          infbnd = 1.00e100;
          options.Set("Infinite Bound Size", infbnd);
          // 
          //            Set the option 'Local Searches Limit = 40'
          // 
          options.Set("Local Searches Limit = 40");
          // 
          //            Set the option 'Local Searches' to 'On'
          // 
          lcsrch = "On";
          options.Set("Local Searches", lcsrch);
          // 
          //            Solve the problem.
          // 
          E05.e05jb(n, objfunE05JB, ibound, iinit, bl, bu, list, numpts, initpt, monitE05JB, x,
                    out obj,
          options, out ifail);
          // 
          Console.WriteLine("");
          if (ifail >= 0)
          {
            Console.WriteLine("  On exit from e05jb, ifail ={0,5}",ifail);
            // 
            if (ifail == 0)
            {
              Console.WriteLine("  Final objective value ={0,11:f5}",obj);
              Console.Write("Global optimum x = ");
              for (i = 1 ; i <= n ; i++)
              {
                Console.Write(" {0, 9:f5}", x[i - 1]);
              }
              Console.WriteLine();
            }
          }
          else
          {
            Console.WriteLine(" ** e05jb returned with ifail = {0, 3}", ifail);
          }
          // 
        }
        // 
        // 
      }
      catch (Exception e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine("Exception Raised");
      }
    }
    // 
    public static void objfun (int n, double[] x, out double f, int nstate,
    out int inform)
    {
      // 
      //      Routine to evaluate objective function.
      // 
      double x1,   x2; 
      // 
      inform = 0;
      f = 0.0;
      if ((inform) >= 0)
      {
        // 
        //         If inform >= 0 then we're prepared to evaluate objfun
        //         at the current x
        // 
        if (nstate == 1)
        {
          // 
          //            This is the first call to objfun
          // 
          Console.WriteLine("");
          Console.WriteLine("  (OBJFUN was just called for the first time)");
        }
        // 
        x1 = x[0];
        x2 = x[1];
        // 
        f = 3.0e0*(1.0e0-x1)*(1.0e0-x1)*Math.Exp(-(x1*x1)-(x2+1)*(x2+1)) -
        1.0e1*(x1/5.0e0-Math.Pow(x1, 3)-Math.Pow(x2,5))*Math.Exp(-x1*x1-x2*x2) -
        1.0e0/3.0e0*Math.Exp(-(x1+1.0e0)*(x1+1.0e0)-x2*x2);
      }
      // 
      // 
    }
    // 
    public static void monit (int n, int ncall, double[] xbest, int[] icount,
    double[,] list, int[] numpts, int[] initpt, int nbaskt, double[,] xbaskt,
    double[] boxl, double[] boxu, int nstate, out int inform)
    {
      // 
      //      Monitoring method.
      // 
      int i,  j; //  inform;
      // 
      inform = 0;
      if ((inform) >= 0)
      {
        // 
        //         We are going to allow the iterations to continue.
        //         Extract plot from the integer communication-array
        // 
        // 
        if ((nstate == 0) || (nstate == 1))
        {
          // 
          //            When nstate == 1, monit is called for the first time. When
          //            nstate == 0, monit is called for the first AND last time.
          //            Display a welcome message
          // 
          Console.WriteLine("");
          Console.WriteLine("  *** Begin monitoring information ***");
          Console.WriteLine("");
          // 
          if (lplot && n == 2)
          {
            Console.WriteLine("  <Begin displaying search boxes>");
            Console.WriteLine("");
          }
          // 
        }
        // 
        if (lplot && n == 2)
        {
          // 
          //            Display the coordinates of the edges of the current search
          //            box
          // 
          outbox(ref n, boxl, boxu);
          // 
        }
        // 
        if (nstate <= 0)
        {
          // 
          //            monit is called for the last time
          // 
          if (lplot && n == 2)
          {
            Console.WriteLine("  <End displaying search boxes>");
            Console.WriteLine("");
          }
          // 
          Console.WriteLine("  Total sub-boxes ={0,5}",icount[0]);
          Console.WriteLine("  Total function evaluations ={0,5}",ncall);
          Console.WriteLine("  Total function evaluations used in local search ={0,5}",icount[1]);
          Console.WriteLine("  Total points used in local search ={0,5}",icount[2]);
          Console.WriteLine("  Total sweeps through levels ={0,5}",icount[3]);
          Console.WriteLine("  Total splits by init. list ={0,5}",icount[4]);
          Console.WriteLine("  Lowest level with nonsplit boxes ={0,5}",icount[5]);
          Console.WriteLine("  Number of candidate minima in the \"shopping basket\" ={0,5}",nbaskt);
          Console.WriteLine("  Shopping basket:");
          // 
          for (i = 1 ; i <= n ; i++)
          {
            Console.Write("xbaskt({0,3},:) = ",  i);
            for (j = 1 ; j <= nbaskt ; j++)
            {
              Console.Write(" {0, 9:f5}", xbaskt[i - 1 , j - 1]);
            }
            Console.WriteLine();
          }
          // 
          Console.WriteLine("");
          Console.WriteLine("  *** End monitoring information ***");
          Console.WriteLine("");
        }
        // 
      }
      // 
      // 
    }
    public static void outbox (ref int n, double[] boxl, double[] boxu)
    {
      // 
      //      Displays edges of box with bounds boxl and boxu in format suitable
      //      for plotting.
      // 
      // 
      // 
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxl[0],boxl[1]);
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxl[0],boxu[1]);
      Console.WriteLine("");
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxl[0],boxl[1]);
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxu[0],boxl[1]);
      Console.WriteLine("");
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxl[0],boxu[1]);
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxu[0],boxu[1]);
      Console.WriteLine("");
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxu[0],boxl[1]);
      Console.WriteLine("{0,20:f15}  {1,20:f15}",boxu[0],boxu[1]);
      Console.WriteLine("");
      // 
      // 
    }
  }
}


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