Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Testfunctions/Evaluators.cs @ 13515

Last change on this file since 13515 was 13515, checked in by bwerth, 8 years ago

#1087 minor bugfixes and added unittests

File size: 11.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Encodings.RealVectorEncoding;
7
8namespace HeuristicLab.Problems.MultiObjectiveTestFunctions.Testfunctions {
9
10  /// <summary>
11  /// unused class
12  /// </summary>
13  class Evaluators {
14
15    //http://www.tik.ee.ethz.ch/sop/download/supplementary/testproblems/ [30.11.2015]
16    private double[] ZDT1(RealVector r) {  //0,1
17      double g = 0;
18      for (int i = 1; i < r.Length; i++) g += r[i];
19      g = 1.0 + 9.0 * g / (r.Length - 1);
20      double[] res = { r[0], g * (1.0 - Math.Sqrt(r[0] / g)) };
21      return res;
22    }
23    private double[] ZDT2(RealVector r) { //0,1
24      double g = 0;
25      for (int i = 1; i < r.Length; i++) g += r[i];
26      g = 1.0 + 9.0 * g / (r.Length - 1);
27      double d = r[0] / g;
28      double[] res = { r[0], g * (1.0 - d * d) };
29      return res;
30    }
31    private double[] ZDT3(RealVector r) { //0,1
32      double g = 0;
33      for (int i = 1; i < r.Length; i++) g += r[i];
34      g = 1.0 + 9.0 * g / (r.Length - 1);
35      double d = r[0] / g;
36      double[] res = { r[0], g * (1.0 - Math.Sqrt(d) - d * Math.Sin(10 * Math.PI * r[0])) };
37      return res;
38    }
39    private double[] ZDT4(RealVector r) {  //-5,5
40      double g = 0;
41      for (int i = 1; i < r.Length; i++) {
42        double v = r[i];
43        g += v * v - 10 * Math.Cos(4 * Math.PI * v);
44      }
45      g = 1.0 + 10.0 * (r.Length - 1) + g;
46      double d = r[0] / g;
47      double[] res = { r[0], g * (1.0 - d * d) };
48      return res;
49    }
50    private double[] ZDT6(RealVector r) { //0,1
51      double g = 0;
52      for (int i = 1; i < r.Length; i++) g += r[i];
53      g = g = 1.0 + 9.0 * Math.Pow(g / (r.Length - 1), 0.25);
54      double d = r[0] / g;
55      double[] res = { 1 - Math.Exp(-4 * r[0]) * Math.Pow(Math.Sin(6 * Math.PI * r[0]), 6), g * (1.0 - d * d) };
56      return res;
57    }
58
59
60    //IMPOTANT There is often confusion about the numbering and the function definitions
61    //eg.: in EALib only the first 4 functions are the same although the other 3 share resemblances
62    //even on  http://www.tik.ee.ethz.ch/ where the original paper is referenced DTLZ7 on the page is DTLZ6 in the paper
63    /*Deb, K., Thiele, L., Laumanns, M., & Zitzler, E. (2002, May).
64    Scalable multi-objective optimization test problems.
65    In Proceedings of the Congress on Evolutionary Computation (CEC-2002),(Honolulu, USA) (pp. 825-830).
66    http://repository.ias.ac.in/81671/ [30.11.15]
67      */
68    private double[] DTLZ1(RealVector r, int objectives) { //0,1
69      double[] res = new double[objectives];
70
71      //calculate g(Xm)
72      double sum = 0, length = 0;
73      for (int i = objectives; i < r.Length; i++) {
74        double d = r[i] - 0.5;
75        sum += d * d - Math.Cos(20 * Math.PI * d);
76        length += r[i] * r[i];
77      }
78      length = Math.Sqrt(length);
79      double g = 100 * (length + sum);
80
81      //calculating f0...fM-1
82      for (int i = 0; i < objectives; i++) {
83        double f = 0.5 * i == 0 ? 1 : (1 - r[objectives - i - 1]) * (1 + g);
84        for (int j = 0; j < objectives - i - 1; j++) {
85          f *= r[j];
86        }
87        res[i] = f;
88      }
89      return res;
90    }
91    private double[] DTLZ2(RealVector r, int objectives) { //0,1
92      double[] res = new double[objectives];
93
94      //calculate g(Xm)
95      double g = 0;
96      for (int i = objectives; i < r.Length; i++) {
97        double d = r[i] - 0.5;
98        g += d * d;
99      }
100
101      //calculating f0...fM-1
102      for (int i = 0; i < objectives; i++) {
103        double f = i == 0 ? 1 : (Math.Sin(r[objectives - i - 1] * Math.PI / 2)) * (1 + g);
104        for (int j = 0; j < objectives - i - 1; j++) {
105          f *= Math.Cos(r[j] * Math.PI / 2);
106        }
107        res[i] = f;
108      }
109      return res;
110    }
111    private double[] DTLZ3(RealVector r, int objectives) {  //0,1
112      double[] res = new double[objectives];
113
114      //calculate g(Xm)
115      double sum = 0, length = 0;
116      for (int i = objectives; i < r.Length; i++) {
117        double d = r[i] - 0.5;
118        sum += d * d - Math.Cos(20 * Math.PI * d);
119        length += r[i] * r[i];
120      }
121      length = Math.Sqrt(length);
122      double g = 100 * (length + sum);
123
124      //calculating f0...fM-1
125      for (int i = 0; i < objectives; i++) {
126        double f = i == 0 ? 1 : (Math.Sin(r[objectives - i - 1] * Math.PI / 2)) * (1 + g);
127        for (int j = 0; j < objectives - i - 1; j++) {
128          f *= Math.Cos(r[j] * Math.PI / 2);
129        }
130        res[i] = f;
131      }
132      return res;
133    }
134    private double[] DTLZ4(RealVector r, int objectives) { //0,1
135      double[] res = new double[objectives];
136
137      //calculate g(Xm)
138      double g = 0;
139      for (int i = objectives; i < r.Length; i++) {
140        double d = r[i] - 0.5;
141        g += d * d;
142      }
143
144      //calculating f0...fM-1
145      for (int i = 0; i < objectives; i++) {
146        double f = i == 0 ? 1 : (Math.Sin(Math.Pow(r[objectives - i - 1], 100) * Math.PI / 2)) * (1 + g);
147        for (int j = 0; j < objectives - i - 1; j++) {
148          f *= Math.Cos(Math.Pow(r[j], 100) * Math.PI / 2);
149        }
150        res[i] = f;
151      }
152      return res;
153    }
154    private double[] DTLZ5(RealVector r, int objectives) {  //0,1
155      double[] res = new double[objectives];
156
157      //calculate g(Xm)
158      double g = 0;
159      for (int i = objectives; i < r.Length; i++) {
160        g += Math.Pow(r[i], 0.1);
161      }
162
163      //phi definition
164      Func<double, double> phi;
165      phi = (double x) => { return Math.PI / (4 * 1 + g) * (1 + 2 * g * x); };
166
167      //calculating f0...fM-1
168      for (int i = 0; i < objectives; i++) {
169        double f = i == 0 ? 1 : (Math.Sin(phi(r[objectives - i - 1]) * Math.PI / 2)) * (1 + g);
170        for (int j = 0; j < objectives - i - 1; j++) {
171          f *= Math.Cos(phi(r[j]) * Math.PI / 2);
172        }
173        res[i] = f;
174      }
175      return res;
176    }
177    private double[] DTLZ6(RealVector r, int objectives) { //0,1
178      double[] res = new double[objectives];
179
180      //calculate g(Xm)
181      double g = 0, length = 0;
182      for (int i = objectives; i < r.Length; i++) {
183        g += r[i];
184        length += r[i] * r[i];
185      }
186      length = Math.Sqrt(length);
187      g = 1.0 + 9.0 / length * g;
188
189      //calculating f0...fM-2
190      for (int i = 0; i < objectives - 1; i++) {
191        res[i] = r[i];
192      }
193      //calculate fM-1
194      double h = objectives;
195      for (int i = 0; i < objectives - 1; i++) {
196        h -= res[i] / (1 + g) * (1 + Math.Sin(3 * Math.PI * res[i]));
197      }
198      res[objectives - 1] = (1 + g) * h;
199
200      return res;
201    }
202    private double[] DTLZ7(RealVector r, int objectives) { //0,1
203      double[] res = new double[objectives];
204
205      //calculate f0...fM-1;
206      double n = 10 * objectives;
207      for (int i = 0; i < objectives; i++) {
208        double d = 0;
209        for (int j = (int)Math.Floor((i - 1) * n / objectives); j < (int)Math.Floor(i * n / objectives); j++) {
210          d += r[j];
211        }
212        d *= 1 / Math.Floor(n / objectives);
213        res[i] = d;
214      }
215
216      //evaluate constraints g0...gM-2
217      for (int i = 0; i < objectives - 1; i++) {
218        if (res[objectives - 1] + 4 * res[i] - 1 < 0) return null; //TODO null is not the way to go
219      }
220      //evaluate gM-1
221      double min = Double.PositiveInfinity;
222      for (int i = 0; i < objectives - 1; i++) {
223        for (int j = 0; j < i; j++) min = Math.Min(min, res[i] + res[j]);
224      };
225      if (2 * res[objectives - 1] + min - 1 < 0) return null;  //TODO null is not the way to go
226
227      return res;
228    }
229
230    //original paper not accessible + wikipedia definition works only with fixed (3) dimensions
231    //http://darwin.di.uminho.pt/jecoli/index.php/Multiobjective_example [30.11.2015]
232    private double[] Kursawe(RealVector r) { //-5,5
233      //objective 1
234      double f0 = 0.0;
235      for (int i = 0; i < r.Length - 1; i++) {
236        f0 += -10 * Math.Exp(-0.2 * Math.Sqrt(r[i] * r[i] + r[i + 1] * r[i + 1]));
237      }
238      //objective2
239      double f1 = 0.0;
240      for (int i = 0; i < r.Length; i++) {
241        f1 += Math.Pow(Math.Abs(r[i]), 0.2) + 5 * Math.Sin(Math.Pow(r[i], 3));
242      }
243
244      double[] res = { f0, f1 };
245      return res;
246    }
247    //http://www.eng.buffalo.edu/Research/MODEL/mdo.test.orig/class2prob4/descr.html
248    //TODO this is not multi-objective
249    private double[] Golinski(RealVector r) { //bounds {{2.6,3.6}, {0.7,0.8}, {17,28}, {7.3, 8.3}, {7.3,8.3}, {2.9,3.9}, {5.0,5.5}}
250      if (r.Length != 7) return null;
251
252      double[] Cf = { 0.7854, 3.3333, 14.9334, 43.0934, 1.5079, 7.477 };
253      double f = Cf[0] * r[0] * r[1] * r[1];
254      f*= Cf[1] * r[2] * r[2] + Cf[2] * r[2] - Cf[3];
255      f -= Cf[4] * (r[5] * r[5] + r[6] * r[6]);
256      f += Cf[5] * (r[5] * r[5] * r[5] + r[6] * r[6] * r[6]);
257      f += Cf[0] * (r[3] * r[5] * r[5] + r[4] * r[6] * r[6]);
258
259      double Ca12s = 745.0*745.0;
260      //Constraints x[0],x[1],x[2],x[5],x[6]
261      List<GoldinskiConstraint> constraints = new List<GoldinskiConstraint>();
262      constraints.Add(new GoldinskiConstraint(x => 27.0/(x[0]*x[1]*x[1]*x[2])));
263      constraints.Add(new GoldinskiConstraint(x => 397.5 / (x[0] * x[1] * x[1] * x[2]*x[2])));
264      constraints.Add(new GoldinskiConstraint(x => 1.93*Math.Pow(x[3],3) / (x[1] * x[2] * Math.Pow(x[5],4))));
265      constraints.Add(new GoldinskiConstraint(x => 1.93 * Math.Pow(x[4], 3) / (x[1] * x[2] * Math.Pow(x[6], 4))));
266      constraints.Add(new GoldinskiConstraint(x => Math.Sqrt(Ca12s*x[3]*x[3]/(x[1]*x[1]*x[2]*x[2])+0.169e8)/(1100*0.1*Math.Pow(x[5],3))));
267      constraints.Add(new GoldinskiConstraint(x => Math.Sqrt(Ca12s * x[4] * x[4] / (x[1] * x[1] * x[2] * x[2]) + 0.169e8) / (859 * 0.1 * Math.Pow(x[6], 3))));
268      constraints.Add(new GoldinskiConstraint(x => x[1]*x[2]/40.0));
269      constraints.Add(new GoldinskiConstraint(x => 5.0 * x[1] / x[0]));
270      constraints.Add(new GoldinskiConstraint(x => x[0] / 12 * x[1]));
271      constraints.Add(new GoldinskiConstraint(x => 2.6/x[0])); //g10
272      constraints.Add(new GoldinskiConstraint(x => x[0]/3.6));
273      constraints.Add(new GoldinskiConstraint(x => 0.7 / x[1]));
274      constraints.Add(new GoldinskiConstraint(x => x[1] / 0.8));
275      return null;
276    }
277    private class GoldinskiConstraint {
278      private Func<RealVector, double> phi;
279      public GoldinskiConstraint(Func<RealVector, double> phi) {
280        this.phi = phi;
281      }
282      public bool eval(RealVector r) {
283        return phi(r) <= 1.0;
284      }
285    }
286
287
288    // https://en.wikipedia.org/wiki/Test_functions_for_optimization [30.11.2015]
289    private double[] Fonseca(RealVector r) {  //-4,4
290      double f0 = 0.0, aux = 1.0 / Math.Sqrt(r.Length);
291
292      //objective1
293      for (int i = 0; i < r.Length; i++) {
294        double d = r[i] - aux;
295        f0 += d * d;
296      }
297      f0 = 1 - Math.Exp(-f0);
298
299      //objective2
300      double f1 = 0.0;
301      for (int i = 0; i < r.Length; i++) {
302        double d = r[i] + aux;
303        f1 += d * d;
304      }
305      f1 = 1 - Math.Exp(-f1);
306
307      double[] res = { f0, f1 };
308      return res;
309    }
310    private double[] Schaffer1(RealVector r) { //-A,A
311      if (r.Length != 1) return null;
312      double x = r[0];
313
314      //objective1
315      double f0=x*x;
316
317      //objective0
318      double f1 = x - 2;
319      f1 *= f1;
320
321      double[] res = { f0, f1 };
322      return res;
323    }
324    private double[] Schaffer2(RealVector r) { //-5,10
325      if (r.Length != 1) return null;
326      double x = r[0];
327
328      //objective1
329      double f0;
330      if (x <= 11) f0 = -x;
331      else if (x <= 3) f0 = x - 2;
332      else if (x <= 4) f0 = 4 - x;
333      else f0 = x - 4;
334
335      //objective0
336      double f1 = x - 5;
337      f1 *= f1;
338
339      double[] res = { f0, f1 };
340      return res;
341    }
342
343  }
344}
Note: See TracBrowser for help on using the repository browser.