Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/3.2/SimOptSquentialSubOperatorCrossover.cs @ 1530

Last change on this file since 1530 was 1530, checked in by gkronber, 15 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.SimOpt {
33  public class SimOptSquentialSubOperatorCrossover : OperatorBase {
34    private UniformSequentialSubScopesProcessor usssp, usssp2, usssp3, usssp4;
35    private SequentialSubScopesProcessor sssp;
36    private SimOptParameterExtractor extractor;
37    private SimOptParameterPacker packer;
38    private SimOptCrossoverPreparator preparator;
39    private MergingReducer merger;
40    private bool uptodate;
41
42    public override string Description {
43      get {
44        return @"This operator encapsulates the functionality of crossing the parameters of a simulation parameter vector. It works as follows:
451. The parameters of all parents are extracted [UniformSequentialSubScopeProcessor which applies a SimOptParameterExtractor]
462. The parents are prepared for crossing by grouping the respective parameters [SimOptCrossoverPreparator]
473. The parameters are crossed [UniformSequentialSubScopeProcessor which applies SequentialSubScopeProcessor which applies the SubOperators of this operator on each parameter group (except for the last one)]
484. Assigning the crossed parameters to the respective children [MergingReducer]
495. Update the parameters [UniformSequentialSubScopeProcessor which applies SimOptParameterPacker]
50
51Should the packing fail due to constraint violations, the operator will execute the last of its suboperators.";
52      }
53    }
54
55    public SimOptSquentialSubOperatorCrossover()
56      : base() {
57      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.New));
58      AddVariableInfo(new VariableInfo("Parents", "The number of parents per child", typeof(IntData), VariableKind.In));
59      uptodate = false;
60    }
61
62    public override IOperation Apply(IScope scope) {
63      string itemsActualName = GetVariableInfo("Items").ActualName;
64      string parentsActualName = GetVariableInfo("Parents").ActualName;
65      int parameters = SubOperators.Count - 1;
66
67      if (!uptodate) {
68        usssp = new UniformSequentialSubScopesProcessor();
69        extractor = new SimOptParameterExtractor();
70        usssp.AddSubOperator(extractor);
71
72        preparator = new SimOptCrossoverPreparator();
73
74        usssp2 = new UniformSequentialSubScopesProcessor();
75        sssp = new SequentialSubScopesProcessor();
76        for (int i = 0; i < parameters; i++) {
77          sssp.AddSubOperator(SubOperators[i]);
78        }
79        usssp2.AddSubOperator(sssp);
80
81        usssp3 = new UniformSequentialSubScopesProcessor();
82        merger = new MergingReducer();
83        usssp3.AddSubOperator(merger);
84
85        usssp4 = new UniformSequentialSubScopesProcessor();
86        packer = new SimOptParameterPacker();
87        packer.AddSubOperator(SubOperators[SubOperators.Count - 1]);
88        usssp4.AddSubOperator(packer);
89        uptodate = true;
90      }
91      // Setting the actual names is necessary as the operator does not know if they've changed
92      extractor.GetVariableInfo("Items").ActualName = itemsActualName;
93      preparator.GetVariableInfo("Parents").ActualName = parentsActualName;
94      packer.GetVariableInfo("Items").ActualName = itemsActualName;
95
96      CompositeOperation co = new CompositeOperation();
97      co.AddOperation(new AtomicOperation(usssp, scope));
98      co.AddOperation(new AtomicOperation(preparator, scope));
99      co.AddOperation(new AtomicOperation(usssp2, scope));
100      co.AddOperation(new AtomicOperation(usssp3, scope));
101      co.AddOperation(new AtomicOperation(usssp4, scope));
102      return co;
103    }
104
105    protected override void OnSubOperatorAdded(IOperator subOperator, int index) {
106      base.OnSubOperatorAdded(subOperator, index);
107      uptodate = false;
108    }
109
110    protected override void OnSubOperatorRemoved(IOperator subOperator, int index) {
111      base.OnSubOperatorRemoved(subOperator, index);
112      uptodate = false;
113    }
114
115    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
116      base.Populate(node, restoredObjects);
117      uptodate = false;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.