1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
15 | [Item("ChildrenCreator", "An operator which is used to prepare crossover. The sub-scopes of the current scope the operator is applied on represent the parents. The operator creates new and empty scopes for each child, adds the scopes that represent the child's parents as sub-scopes to the child and adds the child as sub-scope to the current scope.")]
|
---|
16 | [StorableClass]
|
---|
17 | public class AlgorithmSubScopesCreator : SingleSuccessorOperator {
|
---|
18 | #region Parameter properties
|
---|
19 | public ILookupParameter<TypeValue> AlgorithmTypeParameter {
|
---|
20 | get { return (ILookupParameter<TypeValue>)Parameters[MetaOptimizationProblem.AlgorithmTypeParameterName]; }
|
---|
21 | }
|
---|
22 | public ILookupParameter<IItemList<IProblem>> ProblemsParameter {
|
---|
23 | get { return (ILookupParameter<IItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
24 | }
|
---|
25 | public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
26 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
27 | }
|
---|
28 | public LookupParameter<IntValue> RepetitionsParameter {
|
---|
29 | get { return (LookupParameter<IntValue>)Parameters[MetaOptimizationProblem.RepetitionsParameterName]; }
|
---|
30 | }
|
---|
31 | private ScopeParameter CurrentScopeParameter {
|
---|
32 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
33 | }
|
---|
34 | public IScope CurrentScope {
|
---|
35 | get { return CurrentScopeParameter.ActualValue; }
|
---|
36 | }
|
---|
37 | #endregion
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected AlgorithmSubScopesCreator(bool deserializing) : base(deserializing) { }
|
---|
41 | public AlgorithmSubScopesCreator()
|
---|
42 | : base() {
|
---|
43 | Parameters.Add(new LookupParameter<TypeValue>(MetaOptimizationProblem.AlgorithmTypeParameterName, ""));
|
---|
44 | Parameters.Add(new LookupParameter<IItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
|
---|
45 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
46 | Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
|
---|
47 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
|
---|
48 | }
|
---|
49 | protected AlgorithmSubScopesCreator(AlgorithmSubScopesCreator original, Cloner cloner)
|
---|
50 | : base(original, cloner) {
|
---|
51 | }
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new AlgorithmSubScopesCreator(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IOperation Apply() {
|
---|
57 | ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
|
---|
58 | IItemList<IProblem> problems = ProblemsParameter.ActualValue;
|
---|
59 | int repetitions = RepetitionsParameter.ActualValue.Value;
|
---|
60 | Type algorithmType = AlgorithmTypeParameter.ActualValue.Value;
|
---|
61 |
|
---|
62 | for (int i = 0; i < repetitions; i++) {
|
---|
63 | for (int j = 0; j < problems.Count; j++) {
|
---|
64 | IScope child = new Scope(string.Format("Problem {0}, Repetition {1}", j, i));
|
---|
65 | var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
|
---|
66 | algorithm.Problem = (IProblem)problems[j].Clone();
|
---|
67 | ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
|
---|
68 |
|
---|
69 | //((GeneticAlgorithm)algorithm).PopulationSize.Value = 10;
|
---|
70 | //((GeneticAlgorithm)algorithm).MaximumGenerations.Value = 10;
|
---|
71 |
|
---|
72 | parameterConfiguration.Parameterize(algorithm);
|
---|
73 | child.Variables.Add(new Variable("Algorithm", algorithm));
|
---|
74 | child.Variables.Add(new Variable("ProblemIndex", new IntValue(j)));
|
---|
75 | child.Variables.Add(new Variable("RepetitionIndex", new IntValue(i)));
|
---|
76 | CurrentScope.SubScopes.Add(child);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return base.Apply();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | } |
---|