Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/SimOptCrossoverBase.cs @ 5615

Last change on this file since 5615 was 592, checked in by abeham, 16 years ago

Put the GPL license in the files from the communication framework and simulation optimization project (branch 3.1)

File size: 3.4 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 HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.SimOpt {
30  public abstract class SimOptCrossoverBase : OperatorBase {
31    public SimOptCrossoverBase()
32      : base() {
33      AddVariableInfo(new VariableInfo("Random", "The PRNG to use", typeof(IRandom), VariableKind.In));
34      AddVariableInfo(new VariableInfo("Items", "Paramter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
35      AddVariableInfo(new VariableInfo("Index", "Which parameter to cross", typeof(IntData), VariableKind.In));
36      GetVariableInfo("Index").Local = true;
37      AddVariable(new Variable("Index", new IntData(-1)));
38    }
39
40    public override IOperation Apply(IScope scope) {
41      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
42      ConstrainedItemList childVector = GetVariableValue<ConstrainedItemList>("Items", scope, false);
43      int index = GetVariableValue<IntData>("Index", scope, false).Data;
44      if (index < 0 || index >= childVector.Count) throw new InvalidOperationException("ERROR in SimOptCrossover: Index is out of range of the parameter vector.");
45
46      for (int i = 1; i < scope.SubScopes.Count; i++) {
47        object parent1, parent2;
48        if (i == 1) {
49          IItem p1 = scope.SubScopes[0].GetVariableValue<ConstrainedItemList>("Items", false)[index];
50          if (p1 is IVariable)
51            parent1 = ((((IVariable)p1).Value as IObjectData).Data as ICloneable).Clone();
52          else
53            parent1 = ((p1 as IObjectData).Data as ICloneable).Clone();
54        } else {
55          IItem p1 = childVector[index];
56          if (p1 is IVariable)
57            parent1 = (((IVariable)p1).Value as IObjectData).Data;
58          else
59            parent1 = ((IObjectData)p1).Data;
60        }
61        IItem p2 = scope.SubScopes[i].GetVariableValue<ConstrainedItemList>("Items", false)[index];
62        if (p2 is IVariable)
63          parent2 = (((IVariable)p2).Value as IObjectData).Data;
64        else
65          parent2 = ((IObjectData)p2).Data;
66       
67        object child = Cross(random, parent1, parent2, scope);
68
69        IItem parameter = childVector[index];
70        if (parameter is IVariable)
71          (((IVariable)parameter).Value as IObjectData).Data = child;
72        else {
73          ((IObjectData)parameter).Data = child;
74        }
75      }
76      return null;
77    }
78
79    protected abstract object Cross(IRandom random, object parent1, object parent2, IScope scope);
80  }
81}
Note: See TracBrowser for help on using the repository browser.