Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/SimOptProblemInjector.cs @ 584

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

merged communication framework to trunk (ticket #279)

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8
9namespace HeuristicLab.SimOpt {
10  public class SimOptProblemInjector : OperatorBase {
11
12    public override string Description {
13      get { return @"Injects the parameters used for simulation parameter optimization"; }
14    }
15
16    private BoolData myMaximization;
17    public BoolData Maximization {
18      get { return myMaximization; }
19      set { myMaximization = value; }
20    }
21
22    private StringData myGeneName;
23    public StringData GeneName {
24      get { return myGeneName; }
25      set { myGeneName = value; }
26    }
27
28    private ConstrainedItemList myParameters;
29    public ConstrainedItemList Parameters {
30      get { return myParameters; }
31      set { myParameters = value; }
32    }
33
34    public SimOptProblemInjector()
35      : base() {
36      myParameters = new ConstrainedItemList();
37      myGeneName = new StringData("Items");
38      myMaximization = new BoolData(false);
39    }
40
41    public override IOperation Apply(IScope scope) {
42      scope.AddVariable(new Variable("Maximization", (BoolData)Maximization.Clone()));
43      scope.AddVariable(new Variable(myGeneName.Data, (ConstrainedItemList)Parameters.Clone()));
44      return null;
45    }
46
47    public override IView CreateView() {
48      return new SimOptProblemInjectorView(this);
49    }
50
51    #region Clone & Persistence Methods
52    public override object Clone(IDictionary<Guid, object> clonedObjects) {
53      SimOptProblemInjector clone = new SimOptProblemInjector();
54      clonedObjects.Add(Guid, clone);
55      clone.Maximization = (BoolData)myMaximization.Clone(clonedObjects);
56      clone.GeneName = (StringData)myGeneName.Clone(clonedObjects);
57      clone.myParameters = (ConstrainedItemList)myParameters.Clone(clonedObjects);
58      return clone;
59    }
60
61    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
62      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
63      // variable infos should not be persisted
64      XmlNode infosNode = node.SelectSingleNode("VariableInfos");
65      infosNode.RemoveAll();
66      XmlNode maxNode = PersistenceManager.Persist("Maximization", Maximization, document, persistedObjects);
67      node.AppendChild(maxNode);
68      XmlNode nameNode = PersistenceManager.Persist("GeneName", GeneName, document, persistedObjects);
69      node.AppendChild(nameNode);
70      XmlNode parametersNode = PersistenceManager.Persist("Items", Parameters, document, persistedObjects);
71      node.AppendChild(parametersNode);
72      return node;
73    }
74
75    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
76      base.Populate(node, restoredObjects);
77      if (node.SelectSingleNode("Maximization") != null) {
78        myMaximization = (BoolData)PersistenceManager.Restore(node.SelectSingleNode("Maximization"), restoredObjects);
79      }
80      if (node.SelectSingleNode("GeneName") != null) {
81        myGeneName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("GeneName"), restoredObjects);
82      }
83      IStorable items = (ConstrainedItemList)PersistenceManager.Restore(node.SelectSingleNode("Items"), restoredObjects);
84      try {
85        myParameters = (ConstrainedItemList)items;
86      } catch (InvalidCastException ice) {
87        IVariable var = (Variable)items;
88        myParameters = (ConstrainedItemList)var.Value;
89      }
90    }
91    #endregion
92  }
93}
Note: See TracBrowser for help on using the repository browser.