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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
31 | [StorableClass]
|
---|
32 | [Item("VariableVectorAction", "")]
|
---|
33 | public class VariableVectorAction : Item, IAction {
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private Dictionary<string, IActionVariable> variableDictionary;
|
---|
37 | public Dictionary<string, IActionVariable> VariableDictionary {
|
---|
38 | get { return variableDictionary; }
|
---|
39 | protected set { variableDictionary = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public IOrderedEnumerable<string> Order { get { return VariableDictionary.Keys.OrderBy(x => x); } }
|
---|
43 |
|
---|
44 | public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected VariableVectorAction(bool deserializing) { }
|
---|
48 | protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
|
---|
49 | VariableDictionary = new Dictionary<string, IActionVariable>(original.VariableDictionary.Count);
|
---|
50 | foreach (var item in original.VariableDictionary) {
|
---|
51 | VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
|
---|
52 | }
|
---|
53 | }
|
---|
54 | public VariableVectorAction()
|
---|
55 | : base() {
|
---|
56 | VariableDictionary = new Dictionary<string, IActionVariable>();
|
---|
57 | }
|
---|
58 | public VariableVectorAction(int capacity)
|
---|
59 | : base() {
|
---|
60 | VariableDictionary = new Dictionary<string, IActionVariable>(capacity);
|
---|
61 | }
|
---|
62 | public VariableVectorAction(IDictionary<string, IActionVariable> dictionary)
|
---|
63 | : base() {
|
---|
64 | VariableDictionary = new Dictionary<string, IActionVariable>(dictionary);
|
---|
65 | }
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new VariableVectorAction(this, cloner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public VariableVectorAction GetEmptyCopy() {
|
---|
71 | VariableVectorAction emptyCopy = new VariableVectorAction(VariableDictionary.Count);
|
---|
72 | emptyCopy.Add(VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
|
---|
73 | return emptyCopy;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public VariableVectorAction GetSetCopy() {
|
---|
77 | VariableVectorAction setCopy = new VariableVectorAction(VariableDictionary.Count);
|
---|
78 | setCopy.Add(VariableDictionary.Values.Select(x => x.GetSetCopy()));
|
---|
79 | return setCopy;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public bool Match(IAction target) {
|
---|
83 | var targetCast = target as VariableVectorAction;
|
---|
84 | if (targetCast == null) { return false; }
|
---|
85 | if (!this.Order.SequenceEqual(targetCast.Order)) { return false; }
|
---|
86 | foreach (var keyValuePair in targetCast.VariableDictionary) {
|
---|
87 | if (!VariableDictionary[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void Add(IEnumerable<IActionVariable> action) {
|
---|
95 | foreach (var variable in action) {
|
---|
96 | VariableDictionary.Add(variable.VariableName, variable);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override string ToString() {
|
---|
101 | StringBuilder sb = new StringBuilder();
|
---|
102 | bool first = true;
|
---|
103 | foreach (var variable in VariableDictionary.Values) {
|
---|
104 | if (first) {
|
---|
105 | first = false;
|
---|
106 | } else {
|
---|
107 | sb.Append(";");
|
---|
108 | }
|
---|
109 | sb.Append(variable.ToString());
|
---|
110 | }
|
---|
111 | return sb.ToString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void Randomize(IRandom random) {
|
---|
115 | foreach (var variable in VariableDictionary.Values) {
|
---|
116 | variable.Randomize(random);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public bool Identical(VariableVectorAction target) {
|
---|
121 | if (!this.Order.SequenceEqual(target.Order)) { return false; }
|
---|
122 | foreach (var keyValuePair in target.VariableDictionary) {
|
---|
123 | if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) {
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|