1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization.Operators.LCS {
|
---|
31 | /// <summary>
|
---|
32 | /// An operator which creates new solutions. Evaluation of the new solutions is executed in parallel, if an engine is used which supports parallelization.
|
---|
33 | /// </summary>
|
---|
34 | [Item("NicheSolutionCreator", "")]
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class NicheSolutionCreator : SingleSuccessorOperator {
|
---|
37 | private const string INITIALNICHEPARAMETERNAME = "InitialNiche";
|
---|
38 |
|
---|
39 | public ValueLookupParameter<IntValue> NumberOfSolutionsParameter {
|
---|
40 | get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfSolutions"]; }
|
---|
41 | }
|
---|
42 | public ValueLookupParameter<IGAssistSolutionCreator> SolutionCreatorParameter {
|
---|
43 | get { return (ValueLookupParameter<IGAssistSolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
44 | }
|
---|
45 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
46 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
47 | }
|
---|
48 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
49 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
50 | }
|
---|
51 | public IValueLookupParameter<BoolValue> NichingParameter {
|
---|
52 | get { return (IValueLookupParameter<BoolValue>)Parameters["Niching"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<IGAssistProblemData> GAssistNichesProblemDataParameter {
|
---|
55 | get { return (ILookupParameter<IGAssistProblemData>)Parameters["GAssistNichesProblemData"]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<IGAssistNiche> DefaultClassParameter {
|
---|
58 | get { return (ILookupParameter<IGAssistNiche>)Parameters["DefaultClass"]; }
|
---|
59 | }
|
---|
60 | private ScopeParameter CurrentScopeParameter {
|
---|
61 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
62 | }
|
---|
63 | public IScope CurrentScope {
|
---|
64 | get { return CurrentScopeParameter.ActualValue; }
|
---|
65 | }
|
---|
66 | public IntValue NumberOfSolutions {
|
---|
67 | get { return NumberOfSolutionsParameter.Value; }
|
---|
68 | set { NumberOfSolutionsParameter.Value = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [StorableConstructor]
|
---|
72 | private NicheSolutionCreator(bool deserializing) : base(deserializing) { }
|
---|
73 | private NicheSolutionCreator(NicheSolutionCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
74 | public NicheSolutionCreator()
|
---|
75 | : base() {
|
---|
76 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSolutions", "The number of solutions that should be created."));
|
---|
77 | Parameters.Add(new ValueLookupParameter<IGAssistSolutionCreator>("SolutionCreator", "The operator which is used to create new solutions."));
|
---|
78 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator which is used to evaluate new solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
79 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
|
---|
80 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
|
---|
81 | Parameters.Add(new ValueLookupParameter<BoolValue>("Niching", ""));
|
---|
82 | Parameters.Add(new LookupParameter<IGAssistProblemData>("GAssistNichesProblemData", ""));
|
---|
83 | Parameters.Add(new LookupParameter<IGAssistNiche>("DefaultClass"));
|
---|
84 | }
|
---|
85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
86 | private void AfterDeserialization() {
|
---|
87 | if (!Parameters.ContainsKey("Parallel")) Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true))); // backwards compatibility
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new NicheSolutionCreator(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IOperation Apply() {
|
---|
95 | int count = NumberOfSolutionsParameter.ActualValue.Value;
|
---|
96 | var creator = SolutionCreatorParameter.ActualValue;
|
---|
97 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
98 | bool parallel = ParallelParameter.ActualValue.Value;
|
---|
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 | OperationCollection niche = new OperationCollection();
|
---|
105 | OperationCollection creation = new OperationCollection();
|
---|
106 | OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
|
---|
107 | var vcList = new List<VariableCreator>();
|
---|
108 | if (DefaultClassParameter.ActualValue != null) {
|
---|
109 | //default rule has been set beforehand
|
---|
110 | var vc = new VariableCreator();
|
---|
111 | vc.CollectedValues.Add(new ValueParameter<IGAssistNiche>(INITIALNICHEPARAMETERNAME, DefaultClassParameter.ActualValue));
|
---|
112 | vcList.Add(vc);
|
---|
113 | } else {
|
---|
114 | //default rule is set to auto
|
---|
115 | foreach (var curNiche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
|
---|
116 | var vc = new VariableCreator();
|
---|
117 | vc.CollectedValues.Add(new ValueParameter<IGAssistNiche>(INITIALNICHEPARAMETERNAME, curNiche));
|
---|
118 | vcList.Add(vc);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | creator.GAssistNicheParameter.ActualName = INITIALNICHEPARAMETERNAME;
|
---|
122 | for (int i = 0; i < count; i++) {
|
---|
123 | //only if niching is disabled and no default class has been set, then no niche has to be created in the scope
|
---|
124 | if (NichingParameter.ActualValue.Value || DefaultClassParameter.ActualValue != null) {
|
---|
125 | niche.Add(ExecutionContext.CreateOperation(vcList[i % vcList.Count], CurrentScope.SubScopes[current + i]));
|
---|
126 | }
|
---|
127 | if (creator != null) creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
|
---|
128 | if (evaluator != null) evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
|
---|
129 | }
|
---|
130 | OperationCollection next = new OperationCollection();
|
---|
131 | next.Add(niche);
|
---|
132 | next.Add(creation);
|
---|
133 | next.Add(evaluation);
|
---|
134 | next.Add(base.Apply());
|
---|
135 | return next;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|