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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 IValueLookupParameter<BoolValue> ParallelParameter {
|
---|
56 | get { return (IValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
57 | }
|
---|
58 | private ScopeParameter CurrentScopeParameter {
|
---|
59 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
60 | }
|
---|
61 | public IScope CurrentScope {
|
---|
62 | get { return CurrentScopeParameter.ActualValue; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | protected CoveringOperator(bool deserializing) : base(deserializing) { }
|
---|
68 | protected CoveringOperator(CoveringOperator original, Cloner cloner)
|
---|
69 | : base(original, cloner) {
|
---|
70 | }
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new CoveringOperator(this, cloner);
|
---|
73 | }
|
---|
74 | public CoveringOperator()
|
---|
75 | : base() {
|
---|
76 | Parameters.Add(new LookupParameter<IClassifier>("CoverClassifier"));
|
---|
77 | Parameters.Add(new LookupParameter<IItemSet<IClassifier>>("ActionsInMatchSet"));
|
---|
78 | Parameters.Add(new LookupParameter<IItemSet<IClassifier>>("PossibleActions"));
|
---|
79 | Parameters.Add(new LookupParameter<IntValue>("MinimalNumberOfUniqueActions"));
|
---|
80 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator"));
|
---|
81 | Parameters.Add(new ValueLookupParameter<ICoveringSolutionCreator>("SolutionCreator"));
|
---|
82 | Parameters.Add(new LookupParameter<IRandom>("Random"));
|
---|
83 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
|
---|
84 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
|
---|
85 | }
|
---|
86 |
|
---|
87 | public override IOperation Apply() {
|
---|
88 | int count = MinimalNumberOfUniqueActionsParameter.ActualValue.Value - ActionsInMatchSetParameter.ActualValue.Count;
|
---|
89 | ICoveringSolutionCreator creator = SolutionCreatorParameter.ActualValue;
|
---|
90 | IOperator evaluator = EvaluatorParameter.ActualValue;
|
---|
91 | bool parallel = ParallelParameter.ActualValue.Value;
|
---|
92 |
|
---|
93 | IItemSet<IClassifier> clone = (IItemSet<IClassifier>)PossibleActionsParameter.ActualValue.Clone();
|
---|
94 | clone.ExceptWith(ActionsInMatchSetParameter.ActualValue);
|
---|
95 |
|
---|
96 | if (count > clone.Count) {
|
---|
97 | throw new ArgumentException("More classifiers with unique actions shall be created than unique actions are available.");
|
---|
98 | }
|
---|
99 |
|
---|
100 | int current = CurrentScope.SubScopes.Count;
|
---|
101 | for (int i = 0; i < count; i++) {
|
---|
102 | CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
|
---|
103 | }
|
---|
104 |
|
---|
105 | creator.ActionParameter.ActualName = "Action";
|
---|
106 | OperationCollection variableCreation = new OperationCollection() { Parallel = parallel };
|
---|
107 | OperationCollection creation = new OperationCollection();
|
---|
108 | OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
|
---|
109 | for (int i = 0; i < count; i++) {
|
---|
110 | VariableCreator variableCreator = new VariableCreator();
|
---|
111 | int pos = RandomParameter.ActualValue.Next(clone.Count);
|
---|
112 | IClassifier action = clone.ElementAt(pos);
|
---|
113 | clone.Remove(action);
|
---|
114 | variableCreator.CollectedValues.Add(new ValueParameter<IClassifier>("Action", action));
|
---|
115 | variableCreation.Add(ExecutionContext.CreateOperation(variableCreator, CurrentScope.SubScopes[current + i]));
|
---|
116 | creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
|
---|
117 | evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
|
---|
118 | }
|
---|
119 | OperationCollection next = new OperationCollection();
|
---|
120 | next.Add(variableCreation);
|
---|
121 | next.Add(creation);
|
---|
122 | next.Add(evaluation);
|
---|
123 | next.Add(base.Apply());
|
---|
124 | return next;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|