Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added multiple discretizer to GAssist
  • created ensembles for LCS problems and edited CrossValidation to use them
File size: 4.5 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization.Operators.LCS {
33  [Item("ILASOperator", "Description missing")]
34  [StorableClass]
35  public class ILASOperator : SingleSuccessorOperator, IStochasticOperator {
36
37    #region Parameter Properties
38    public ILookupParameter<IRandom> RandomParameter {
39      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ILookupParameter<IGAssistProblemData> ProblemDataParameter {
42      get { return (ILookupParameter<IGAssistProblemData>)Parameters["ProblemData"]; }
43    }
44    public IValueLookupParameter<IntValue> NumberOfStrataParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfStrata"]; }
46    }
47    public IValueLookupParameter<ItemList<ItemList<IntValue>>> StrataParameter {
48      get { return (IValueLookupParameter<ItemList<ItemList<IntValue>>>)Parameters["Strata"]; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    protected ILASOperator(bool deserializing) : base(deserializing) { }
54    protected ILASOperator(ILASOperator original, Cloner cloner)
55      : base(original, cloner) {
56    }
57    public ILASOperator()
58      : base() {
59      Parameters.Add(new LookupParameter<IRandom>("Random", "The random generator to use."));
60      Parameters.Add(new LookupParameter<IGAssistProblemData>("ProblemData", ""));
61      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfStrata", ""));
62      Parameters.Add(new ValueLookupParameter<ItemList<ItemList<IntValue>>>("Strata", ""));
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new ILASOperator(this, cloner);
66    }
67
68    public override IOperation Apply() {
69      if (StrataParameter.ActualValue == null) {
70        //initialize strata
71        int numberOfStrata = NumberOfStrataParameter.ActualValue.Value;
72        var strata = new ItemList<ItemList<IntValue>>(numberOfStrata);
73        for (int i = 0; i < numberOfStrata; i++) {
74          strata.Add(new ItemList<IntValue>());
75        }
76
77        //get all rows for every niche
78        var problemData = ProblemDataParameter.ActualValue;
79        IntRange evaluate = ProblemDataParameter.ActualValue.TrainingPartition;
80        Dictionary<IGAssistNiche, List<int>> nicheRows = null;
81        for (int row = evaluate.Start; row < evaluate.End; row++) {
82          if (problemData.IsTrainingSample(row)) {
83            var niche = problemData.FetchAction(row);
84            if (nicheRows == null) {
85              nicheRows = new Dictionary<IGAssistNiche, List<int>>(niche.Comparer);
86            }
87            if (!nicheRows.ContainsKey(niche)) {
88              nicheRows.Add(niche, new List<int>());
89            }
90            nicheRows[niche].Add(row);
91          }
92        }
93
94        //distribute niches to strata
95        var random = RandomParameter.ActualValue;
96        foreach (var niche in nicheRows.Keys) {
97          var rows = nicheRows[niche];
98          int count = 0;
99          while (rows.Count > 0) {
100            int pos = random.Next(rows.Count);
101            strata[count % numberOfStrata].Add(new IntValue(rows[pos]));
102            rows.RemoveAt(pos);
103            count++;
104          }
105        }
106
107        if (strata.Any(x => x.Count() == 0)) {
108          throw new ArgumentException("At least one strata is empty. Use less strata or add more data.");
109        }
110
111        StrataParameter.ActualValue = strata;
112      }
113      return base.Apply();
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.