Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Algorithms.MemPR/3.3/Util/Auxiliary.cs @ 15781

Last change on this file since 15781 was 14666, checked in by abeham, 8 years ago

#2457: copied MemPR algorithm from its branch to this branch

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Algorithms.MemPR.Util {
27  public static class Auxiliary {
28    public static double[] PrepareProportional(IEnumerable<double> weights, bool windowing, bool inverseProportional) {
29      double maxValue = double.MinValue, minValue = double.MaxValue;
30      double[] valueArray = weights.ToArray();
31
32      for (int i = 0; i < valueArray.Length; i++) {
33        if (valueArray[i] > maxValue) maxValue = valueArray[i];
34        if (valueArray[i] < minValue) minValue = valueArray[i];
35      }
36      if (minValue == maxValue) {  // all values are equal
37        for (int i = 0; i < valueArray.Length; i++) {
38          valueArray[i] = 1.0;
39        }
40      } else {
41        if (windowing) {
42          if (inverseProportional) InverseProportionalScale(valueArray, maxValue);
43          else ProportionalScale(valueArray, minValue);
44        } else {
45          if (minValue < 0.0) throw new InvalidOperationException("Proportional selection without windowing does not work with values < 0.");
46          if (inverseProportional) InverseProportionalScale(valueArray, 2 * maxValue);
47        }
48      }
49      return valueArray;
50    }
51    private static void ProportionalScale(double[] values, double minValue) {
52      for (int i = 0; i < values.Length; i++) {
53        values[i] = values[i] - minValue;
54      }
55    }
56    private static void InverseProportionalScale(double[] values, double maxValue) {
57      for (int i = 0; i < values.Length; i++) {
58        values[i] = maxValue - values[i];
59      }
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.