source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/Covering/CoveringOperator.cs @ 9105

Last change on this file since 9105 was 9105, checked in by sforsten, 6 years ago

#1980:

  • included an adapted version of GA correctly
  • added action set subsumption
  • added deletion after GA and before covering
File size: 6.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;
23using System.Linq;
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("CoveringOperator", "")]
33  [StorableClass]
34  public class CoveringOperator : SingleSuccessorOperator, ICovering {
35
36    #region Parameter Properties
37    public ILookupParameter<IItemSet<IClassifier>> ActionsInMatchSetParameter {
38      get { return (ILookupParameter<IItemSet<IClassifier>>)Parameters["ActionsInMatchSet"]; }
39    }
40    public ILookupParameter<IItemSet<IClassifier>> PossibleActionsParameter {
41      get { return (ILookupParameter<IItemSet<IClassifier>>)Parameters["PossibleActions"]; }
42    }
43    public ILookupParameter<IntValue> MinimalNumberOfUniqueActionsParameter {
44      get { return (ILookupParameter<IntValue>)Parameters["MinimalNumberOfUniqueActions"]; }
45    }
46    public IValueLookupParameter<IOperator> EvaluatorParameter {
47      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
48    }
49    public IValueLookupParameter<ICoveringSolutionCreator> SolutionCreatorParameter {
50      get { return (IValueLookupParameter<ICoveringSolutionCreator>)Parameters["SolutionCreator"]; }
51    }
52    public ILookupParameter<IRandom> RandomParameter {
53      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
54    }
55    public ILookupParameter<IntValue> CurrentPopulationSizeParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["CurrentPopulationSize"]; }
57    }
58    public IValueLookupParameter<BoolValue> ParallelParameter {
59      get { return (IValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
60    }
61    private ScopeParameter CurrentScopeParameter {
62      get { return (ScopeParameter)Parameters["CurrentScope"]; }
63    }
64    public IScope CurrentScope {
65      get { return CurrentScopeParameter.ActualValue; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    protected CoveringOperator(bool deserializing) : base(deserializing) { }
71    protected CoveringOperator(CoveringOperator original, Cloner cloner)
72      : base(original, cloner) {
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new CoveringOperator(this, cloner);
76    }
77    public CoveringOperator()
78      : base() {
79      Parameters.Add(new LookupParameter<IClassifier>("CoverClassifier"));
80      Parameters.Add(new LookupParameter<IItemSet<IClassifier>>("ActionsInMatchSet"));
81      Parameters.Add(new LookupParameter<IItemSet<IClassifier>>("PossibleActions"));
82      Parameters.Add(new LookupParameter<IntValue>("MinimalNumberOfUniqueActions"));
83      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator"));
84      Parameters.Add(new ValueLookupParameter<ICoveringSolutionCreator>("SolutionCreator"));
85      Parameters.Add(new LookupParameter<IRandom>("Random"));
86      Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
87      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
88      Parameters.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
89    }
90
91    public override IOperation Apply() {
92      int count = MinimalNumberOfUniqueActionsParameter.ActualValue.Value - ActionsInMatchSetParameter.ActualValue.Count;
93      ICoveringSolutionCreator creator = SolutionCreatorParameter.ActualValue;
94      IOperator evaluator = EvaluatorParameter.ActualValue;
95      bool parallel = ParallelParameter.ActualValue.Value;
96
97      IItemSet<IClassifier> clone = (IItemSet<IClassifier>)PossibleActionsParameter.ActualValue.Clone();
98      clone.ExceptWith(ActionsInMatchSetParameter.ActualValue);
99
100      if (count > clone.Count) {
101        throw new ArgumentException("More classifiers with unique actions shall be created than unique actions are available.");
102      }
103
104      CurrentPopulationSizeParameter.ActualValue.Value += count;
105
106      int current = CurrentScope.SubScopes.Count;
107      for (int i = 0; i < count; i++) {
108        CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
109      }
110
111      creator.ActionParameter.ActualName = "Action";
112      OperationCollection variableCreation = new OperationCollection() { Parallel = parallel };
113      OperationCollection creation = new OperationCollection();
114      OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
115      for (int i = 0; i < count; i++) {
116        VariableCreator variableCreator = new VariableCreator();
117        int pos = RandomParameter.ActualValue.Next(clone.Count);
118        IClassifier action = clone.ElementAt(pos);
119        clone.Remove(action);
120        variableCreator.CollectedValues.Add(new ValueParameter<IClassifier>("Action", action));
121        variableCreation.Add(ExecutionContext.CreateOperation(variableCreator, CurrentScope.SubScopes[current + i]));
122        creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
123        evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
124      }
125      OperationCollection next = new OperationCollection();
126      next.Add(variableCreation);
127      next.Add(creation);
128      next.Add(evaluation);
129      next.Add(base.Apply());
130      return next;
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.