Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/GA/InsertInPopulationOperator.cs @ 9844

Last change on this file since 9844 was 9194, checked in by sforsten, 12 years ago

#1980:

  • added necessary interface ICondition, IAction, IInput
  • removed not used class MatchSelector and interface IMatchSelector
  • added constructors to ItemDictionary
  • added new encoding
File size: 4.1 KB
RevLine 
[9110]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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.ConditionActionEncoding {
32  [Item("InsertInPopulationOperator", "Description missing")]
33  [StorableClass]
34  public class InsertInPopulationOperator : SingleSuccessorOperator {
35
36    #region Parameter Properties
37    public ILookupParameter<ItemArray<BoolValue>> InsertInPopulationParameter {
38      get { return (ILookupParameter<ItemArray<BoolValue>>)Parameters["InsertInPopulation"]; }
39    }
40    public ILookupParameter<ItemArray<IClassifier>> ClassifiersParameter {
41      get { return (ILookupParameter<ItemArray<IClassifier>>)Parameters["Classifiers"]; }
42    }
43    public ILookupParameter<ItemArray<IntValue>> NumerositiesParameter {
44      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Numerosity"]; }
45    }
46    public ILookupParameter<ItemArray<BoolValue>> HasToBeDeletedParameter {
47      get { return (ILookupParameter<ItemArray<BoolValue>>)Parameters["HasToBeDeleted"]; }
48    }
49    #endregion
50
51    [StorableConstructor]
52    protected InsertInPopulationOperator(bool deserializing) : base(deserializing) { }
53    protected InsertInPopulationOperator(InsertInPopulationOperator original, Cloner cloner)
54      : base(original, cloner) {
55    }
56    public InsertInPopulationOperator()
57      : base() {
58      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("InsertInPopulation"));
59      Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("Classifiers"));
60      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosity"));
61      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("HasToBeDeleted"));
62    }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new InsertInPopulationOperator(this, cloner);
65    }
66
67    public override IOperation Apply() {
68      var numerosities = NumerositiesParameter.ActualValue;
69      var classifiers = ClassifiersParameter.ActualValue;
70      var hasToBeDeleted = HasToBeDeletedParameter.ActualValue;
71      var insertInPopulation = InsertInPopulationParameter.ActualValue;
72
73      if (insertInPopulation.Length != numerosities.Length || insertInPopulation.Length != classifiers.Length
74        || insertInPopulation.Length != hasToBeDeleted.Length) {
75        throw new ArgumentException("Different number of classifiers, numerosity, hasToBeDeleted and insertInPopulation values.");
76      }
77
78      IList<int> insertIndices = new List<int>();
79      for (int i = 0; i < insertInPopulation.Length; i++) {
80        if (insertInPopulation[i].Value) {
81          insertIndices.Add(i);
82        }
83      }
84
85      foreach (var indices in insertIndices) {
86        IClassifier cl = classifiers[indices];
87        for (int i = 0; i < classifiers.Length; i++) {
[9194]88          if (!hasToBeDeleted[i].Value && !insertInPopulation[i].Value && cl.Identical(classifiers[i])) {
[9110]89            numerosities[i].Value += numerosities[indices].Value;
90            hasToBeDeleted[indices].Value = true;
91            break;
92          }
93        }
94        insertInPopulation[indices].Value = false;
95      }
96
97      return base.Apply();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.