[16916] | 1 | using System;
|
---|
| 2 | using HEAL.Attic;
|
---|
[7128] | 3 | using HeuristicLab.Collections;
|
---|
| 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Optimization;
|
---|
| 7 | using HeuristicLab.Parameters;
|
---|
| 8 |
|
---|
[16916] | 9 | namespace HeuristicLab.Operators {
|
---|
[7128] | 10 |
|
---|
[16916] | 11 | [Item("EngineAlgorithmOperator", "An operator the encapsulates a complete algorithm.")]
|
---|
| 12 | [StorableType("8BEF3D75-7DA3-4683-95C5-AF7B6AC0A810")]
|
---|
| 13 | public class EngineAlgorithmOperator : AlgorithmOperator {
|
---|
[7128] | 14 |
|
---|
[16916] | 15 | [Storable]
|
---|
| 16 | private OperatorGraph algorithmOperatorGraph;
|
---|
[7128] | 17 |
|
---|
[16916] | 18 | [Storable]
|
---|
| 19 | protected EngineAlgorithm algorithm;
|
---|
| 20 | public EngineAlgorithm Algorithm {
|
---|
| 21 | get { return algorithm; }
|
---|
| 22 | set {
|
---|
| 23 | if (value == algorithm)
|
---|
| 24 | return;
|
---|
| 25 | if (algorithm != null) {
|
---|
| 26 | DeregisterAlgorithmEvents();
|
---|
| 27 | if (algorithmOperatorGraph != null)
|
---|
| 28 | DeregisterOperatorGraphEvents();
|
---|
| 29 | OperatorGraph.InitialOperator = null;
|
---|
| 30 | OperatorGraph.Operators.Clear();
|
---|
[7128] | 31 | }
|
---|
[16916] | 32 | algorithm = value;
|
---|
| 33 | OnAlgorithmChanged();
|
---|
| 34 | if (algorithm != null) {
|
---|
| 35 | foreach (IOperator op in algorithm.OperatorGraph.Operators)
|
---|
| 36 | OperatorGraph.Operators.Add(op);
|
---|
| 37 | OperatorGraph.InitialOperator = algorithm.OperatorGraph.InitialOperator;
|
---|
| 38 | RegisterAlgorithmEvents();
|
---|
| 39 | if (algorithm.OperatorGraph != null) {
|
---|
| 40 | algorithmOperatorGraph = algorithm.OperatorGraph;
|
---|
| 41 | RegisterOperatorGraphEvents();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
[7128] | 46 |
|
---|
[16916] | 47 | #region Events
|
---|
| 48 | public event EventHandler AlgorithmChanged;
|
---|
[7128] | 49 |
|
---|
[16916] | 50 | protected virtual void OnAlgorithmChanged() {
|
---|
| 51 | UpdateParameters();
|
---|
| 52 | EventHandler handler = AlgorithmChanged;
|
---|
| 53 | if (handler != null)
|
---|
| 54 | handler(this, EventArgs.Empty);
|
---|
| 55 | }
|
---|
[7128] | 56 |
|
---|
[16916] | 57 | private void RegisterAlgorithmEvents() {
|
---|
| 58 | algorithm.OperatorGraphChanged += algorithm_OperatorGraphChanged;
|
---|
| 59 | algorithm.ProblemChanged += new EventHandler(algorithm_ProblemChanged);
|
---|
| 60 | }
|
---|
[7128] | 61 |
|
---|
[16916] | 62 | private void algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
| 63 | UpdateParameters();
|
---|
| 64 | }
|
---|
[7128] | 65 |
|
---|
[16916] | 66 | private void DeregisterAlgorithmEvents() {
|
---|
| 67 | algorithm.OperatorGraphChanged -= algorithm_OperatorGraphChanged;
|
---|
| 68 | }
|
---|
[7128] | 69 |
|
---|
[16916] | 70 | private void algorithm_OperatorGraphChanged(object sender, EventArgs e) {
|
---|
| 71 | if (algorithmOperatorGraph != null)
|
---|
| 72 | DeregisterOperatorGraphEvents();
|
---|
| 73 | algorithmOperatorGraph = null;
|
---|
| 74 | if (algorithm.OperatorGraph != null) {
|
---|
| 75 | algorithmOperatorGraph = algorithm.OperatorGraph;
|
---|
| 76 | RegisterOperatorGraphEvents();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
[7128] | 79 |
|
---|
[16916] | 80 | private void RegisterOperatorGraphEvents() {
|
---|
| 81 | algorithmOperatorGraph.InitialOperatorChanged += algorithmOperatorGraph_InitialOperatorChanged;
|
---|
| 82 | algorithmOperatorGraph.Operators.CollectionReset += algorithmOperatorGraph_Operators_Changed;
|
---|
| 83 | algorithmOperatorGraph.Operators.ItemsAdded += algorithmOperatorGraph_Operators_Changed;
|
---|
| 84 | algorithmOperatorGraph.Operators.ItemsRemoved += algorithmOperatorGraph_Operators_Changed;
|
---|
| 85 | }
|
---|
[7128] | 86 |
|
---|
[16916] | 87 | private void DeregisterOperatorGraphEvents() {
|
---|
| 88 | algorithmOperatorGraph.InitialOperatorChanged -= algorithmOperatorGraph_InitialOperatorChanged;
|
---|
| 89 | algorithmOperatorGraph.Operators.CollectionReset -= algorithmOperatorGraph_Operators_Changed;
|
---|
| 90 | algorithmOperatorGraph.Operators.ItemsAdded -= algorithmOperatorGraph_Operators_Changed;
|
---|
| 91 | algorithmOperatorGraph.Operators.ItemsRemoved -= algorithmOperatorGraph_Operators_Changed;
|
---|
| 92 | }
|
---|
[7128] | 93 |
|
---|
[16916] | 94 | private void algorithmOperatorGraph_Operators_Changed(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
| 95 | OperatorGraph.Operators.Clear();
|
---|
| 96 | foreach (IOperator op in algorithmOperatorGraph.Operators) {
|
---|
| 97 | OperatorGraph.Operators.Add(op);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[7128] | 100 |
|
---|
[16916] | 101 | private void algorithmOperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
|
---|
| 102 | OperatorGraph.InitialOperator = algorithmOperatorGraph.InitialOperator;
|
---|
| 103 | }
|
---|
[7128] | 104 |
|
---|
[16916] | 105 | private void UpdateParameters() {
|
---|
| 106 | // TODO: match previously set parameter values
|
---|
| 107 | IOperator successor = Successor;
|
---|
| 108 | Parameters.Clear();
|
---|
| 109 | Parameters.Add(new OperatorParameter("Successor", successor));
|
---|
| 110 | var alg = Algorithm as IParameterizedItem;
|
---|
| 111 | foreach (var param in alg.Parameters) {
|
---|
| 112 | Parameters.Add(WrapParameter(param));
|
---|
| 113 | }
|
---|
| 114 | if (Algorithm.Problem != null) {
|
---|
| 115 | foreach (var param in Algorithm.Problem.Parameters) {
|
---|
| 116 | Parameters.Add(WrapParameter(param));
|
---|
[7128] | 117 | }
|
---|
[16916] | 118 | }
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<NamedItemCollection<IResult>>("Results", "Results collection as passed through to the inner algorithn", "Results") { GetsCollected = false });
|
---|
| 120 | }
|
---|
[7128] | 121 |
|
---|
[16916] | 122 | private static IValueLookupParameter WrapParameter(IParameter param) {
|
---|
| 123 | IValueLookupParameter v = (IValueLookupParameter)Activator.CreateInstance(typeof(ValueLookupParameter<>).MakeGenericType(param.DataType), new object[] { param.Name, param.Description });
|
---|
| 124 | v.ActualName = param.Name;
|
---|
| 125 | IValueParameter valueParam = param as IValueParameter;
|
---|
| 126 | if (valueParam != null)
|
---|
| 127 | v.Value = valueParam.Value;
|
---|
| 128 | return v;
|
---|
| 129 | }
|
---|
| 130 | #endregion
|
---|
[7128] | 131 |
|
---|
[16916] | 132 | #region Construction & Cloning
|
---|
| 133 | [StorableConstructor]
|
---|
| 134 | protected EngineAlgorithmOperator(StorableConstructorFlag _) : base(_) { }
|
---|
| 135 | protected EngineAlgorithmOperator(EngineAlgorithmOperator original, Cloner cloner)
|
---|
| 136 | : base(original, cloner) {
|
---|
| 137 | this.algorithm = cloner.Clone(original.algorithm);
|
---|
| 138 | this.algorithmOperatorGraph = cloner.Clone(original.algorithmOperatorGraph);
|
---|
| 139 | }
|
---|
| 140 | public EngineAlgorithmOperator() { }
|
---|
| 141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 142 | return new EngineAlgorithmOperator(this, cloner);
|
---|
| 143 | }
|
---|
| 144 | #endregion
|
---|
[7128] | 145 |
|
---|
[16916] | 146 | public override IOperation Apply() {
|
---|
| 147 | var alg = Algorithm as IParameterizedItem;
|
---|
| 148 | foreach (var param in alg.Parameters) {
|
---|
| 149 | param.ActualValue = Parameters[param.Name].ActualValue;
|
---|
| 150 | }
|
---|
| 151 | if (Algorithm.Problem != null) {
|
---|
| 152 | foreach (var param in Algorithm.Problem.Parameters) {
|
---|
| 153 | param.ActualValue = Parameters[param.Name].ActualValue;
|
---|
[7128] | 154 | }
|
---|
[16916] | 155 | }
|
---|
| 156 | return base.Apply();
|
---|
| 157 | }
|
---|
[16877] | 158 |
|
---|
[16916] | 159 | }
|
---|
[7128] | 160 | }
|
---|