Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/Selectors/MatchSelector.cs @ 9089

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

#1980:

  • added ConditionActionClassificationProblem
  • added ConditionActionEncoding
  • added Manipulators, Crossovers and an LCSAdaptedGeneticAlgorithm
  • changed LearningClassifierSystemMainLoop
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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Encodings.ConditionActionEncoding {
31  /// <summary>
32  /// A class for selection operators which match a target to the scopes.
33  /// </summary>
34  [Item("MatchSelector", "A class for selection operators which match a target to the scopes.")]
35  [StorableClass]
36  public class MatchSelector : Selector, IMatchSelector {
37    protected IValueParameter<BoolValue> CopySelectedParameter {
38      get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
39    }
40    protected IValueParameter<BoolValue> MatchConditionParameter {
41      get { return (IValueParameter<BoolValue>)Parameters["MatchCondition"]; }
42    }
43    public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
45    }
46    public ILookupParameter<IClassifier> TargetMatchParameter {
47      get { return (ILookupParameter<IClassifier>)Parameters["TargetMatchParameter"]; }
48    }
49    public ILookupParameter<ItemArray<IClassifier>> MatchParameter {
50      get { return (ILookupParameter<ItemArray<IClassifier>>)Parameters["MatchParameter"]; }
51    }
52    public BoolValue CopySelected {
53      get { return CopySelectedParameter.Value; }
54      set { CopySelectedParameter.Value = value; }
55    }
56
57    public BoolValue MatchCondition {
58      get { return MatchConditionParameter.Value; }
59      set { MatchConditionParameter.Value = value; }
60    }
61
62    [StorableConstructor]
63    protected MatchSelector(bool deserializing) : base(deserializing) { }
64    protected MatchSelector(MatchSelector original, Cloner cloner) : base(original, cloner) { }
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new MatchSelector(this, cloner);
67    }
68
69    public MatchSelector()
70      : base() {
71      Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "True if the selected sub-scopes should be copied, otherwise false.", new BoolValue(true)));
72      Parameters.Add(new ValueParameter<BoolValue>("MatchCondition", "True if the condition of the match paramteres have to match, otherwise the action has to match.", new BoolValue(true)));
73      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of sub-scopes which should be selected."));
74      Parameters.Add(new ValueLookupParameter<IClassifier>("TargetMatchParameter", "Target that has to be matched by the match parameter."));
75      Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("MatchParameter", "The matching encoding contained in each sub-scope which is used for selection."));
76    }
77
78    protected override IScope[] Select(List<IScope> scopes) {
79      IClassifier target = TargetMatchParameter.ActualValue;
80      List<IScope> matched = new List<IScope>();
81      for (int i = 0; i < MatchParameter.ActualValue.Length; i++) {
82        if (MatchingMethod(MatchParameter.ActualValue[i], target)) {
83          if (CopySelected.Value) {
84            matched.Add((IScope)scopes[i].Clone());
85          } else {
86            matched.Add(scopes[i]);
87          }
88        }
89      }
90      NumberOfSelectedSubScopesParameter.ActualValue = new IntValue(matched.Count);
91      return matched.ToArray();
92    }
93
94    private bool MatchingMethod(IClassifier iMatching, IClassifier target) {
95      if (MatchCondition.Value) {
96        return iMatching.MatchCondition(target);
97      } else {
98        return iMatching.MatchAction(target);
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.