Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17189 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: 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<IAction>> ActionsInMatchSetParameter {
38      get { return (ILookupParameter<IItemSet<IAction>>)Parameters["ActionsInMatchSet"]; }
39    }
40    public ILookupParameter<IItemSet<IAction>> PossibleActionsParameter {
41      get { return (ILookupParameter<IItemSet<IAction>>)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<IItemSet<IAction>>("ActionsInMatchSet"));
80      Parameters.Add(new LookupParameter<IItemSet<IAction>>("PossibleActions"));
81      Parameters.Add(new LookupParameter<IntValue>("MinimalNumberOfUniqueActions"));
82      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator"));
83      Parameters.Add(new ValueLookupParameter<ICoveringSolutionCreator>("SolutionCreator"));
84      Parameters.Add(new LookupParameter<IRandom>("Random"));
85      Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
86      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
87      Parameters.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
88    }
89
90    public override IOperation Apply() {
91      int count = MinimalNumberOfUniqueActionsParameter.ActualValue.Value - ActionsInMatchSetParameter.ActualValue.Count;
92      ICoveringSolutionCreator creator = SolutionCreatorParameter.ActualValue;
93      IOperator evaluator = EvaluatorParameter.ActualValue;
94      bool parallel = ParallelParameter.ActualValue.Value;
95
96      IItemSet<IAction> clone = (IItemSet<IAction>)PossibleActionsParameter.ActualValue.Clone();
97      clone.ExceptWith(ActionsInMatchSetParameter.ActualValue);
98
99      if (count > clone.Count) {
100        throw new ArgumentException("More classifiers with unique actions shall be created than unique actions are available.");
101      }
102      if (count > 0) {
103        CurrentPopulationSizeParameter.ActualValue.Value += count;
104
105        int current = CurrentScope.SubScopes.Count;
106        for (int i = 0; i < count; i++) {
107          CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
108        }
109
110        creator.ActionParameter.ActualName = "Action";
111        OperationCollection variableCreation = new OperationCollection() { Parallel = parallel };
112        OperationCollection creation = new OperationCollection();
113        OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
114        for (int i = 0; i < count; i++) {
115          VariableCreator variableCreator = new VariableCreator();
116          int pos = RandomParameter.ActualValue.Next(clone.Count);
117          IAction action = clone.ElementAt(pos);
118          clone.Remove(action);
119          variableCreator.CollectedValues.Add(new ValueParameter<IAction>("Action", action));
120          variableCreation.Add(ExecutionContext.CreateOperation(variableCreator, CurrentScope.SubScopes[current + i]));
121          creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
122          evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
123        }
124        OperationCollection next = new OperationCollection();
125        next.Add(variableCreation);
126        next.Add(creation);
127        next.Add(evaluation);
128        next.Add(base.Apply());
129        return next;
130      } else {
131        return base.Apply();
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.