Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/Selectors/MatchSelector.cs @ 15689

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

#1980:
A general implementation of most main components of LCS is done with an own encoding.
At the moment you can just watch the LCS generating a number of solutions and selecting the match and action set in the debug engine.

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