Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/GAssist/ILAS/ILASOperator.cs @ 9392

Last change on this file since 9392 was 9392, checked in by sforsten, 11 years ago

#1980:

  • several small bug fixes
  • added windowing technique ILAS to GAssist
  • GAssist and XCS work now with real-valued features
  • severely improved the performance of XCS
File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Optimization.Operators.LCS {
31  [Item("ILASOperator", "Description missing")]
32  [StorableClass]
33  public class ILASOperator : SingleSuccessorOperator, IStochasticOperator {
34
35    #region Parameter Properties
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public ILookupParameter<IGAssistNichesProblemData> ProblemDataParameter {
40      get { return (ILookupParameter<IGAssistNichesProblemData>)Parameters["ProblemData"]; }
41    }
42    public IValueLookupParameter<IntValue> NumberOfStrataParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfStrata"]; }
44    }
45    public IValueLookupParameter<ItemList<ItemList<IntValue>>> StrataParameter {
46      get { return (IValueLookupParameter<ItemList<ItemList<IntValue>>>)Parameters["Strata"]; }
47    }
48    #endregion
49
50    [StorableConstructor]
51    protected ILASOperator(bool deserializing) : base(deserializing) { }
52    protected ILASOperator(ILASOperator original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public ILASOperator()
56      : base() {
57      Parameters.Add(new LookupParameter<IRandom>("Random", "The random generator to use."));
58      Parameters.Add(new LookupParameter<IGAssistNichesProblemData>("ProblemData", ""));
59      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfStrata", ""));
60      Parameters.Add(new ValueLookupParameter<ItemList<ItemList<IntValue>>>("Strata", ""));
61    }
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ILASOperator(this, cloner);
64    }
65
66    public override IOperation Apply() {
67      if (StrataParameter.ActualValue == null) {
68        //initialize strata
69        int numberOfStrata = NumberOfStrataParameter.ActualValue.Value;
70        var strata = new ItemList<ItemList<IntValue>>(numberOfStrata);
71        for (int i = 0; i < numberOfStrata; i++) {
72          strata.Add(new ItemList<IntValue>());
73        }
74
75        //get all rows for every niche
76        var problemData = ProblemDataParameter.ActualValue;
77        IntRange evaluate = ProblemDataParameter.ActualValue.TrainingPartition;
78        Dictionary<IGAssistNiche, List<int>> nicheRows = null;
79        for (int row = evaluate.Start; row < evaluate.End; row++) {
80          if (problemData.IsTrainingSample(row)) {
81            var niche = problemData.FetchAction(row);
82            if (nicheRows == null) {
83              nicheRows = new Dictionary<IGAssistNiche, List<int>>(niche.Comparer);
84            }
85            if (!nicheRows.ContainsKey(niche)) {
86              nicheRows.Add(niche, new List<int>());
87            }
88            nicheRows[niche].Add(row);
89          }
90        }
91
92        //distribute niches to strata
93        var random = RandomParameter.ActualValue;
94        foreach (var niche in nicheRows.Keys) {
95          var rows = nicheRows[niche];
96          int count = 0;
97          while (rows.Count > 0) {
98            int pos = random.Next(rows.Count);
99            strata[count % numberOfStrata].Add(new IntValue(rows[pos]));
100            rows.RemoveAt(pos);
101            count++;
102          }
103        }
104
105        StrataParameter.ActualValue = strata;
106      }
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.