[3852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3852] | 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 |
|
---|
[3861] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[4068] | 26 | using HeuristicLab.Analysis;
|
---|
[3861] | 27 | using HeuristicLab.Collections;
|
---|
[3862] | 28 | using HeuristicLab.Common;
|
---|
[3852] | 29 | using HeuristicLab.Core;
|
---|
[3861] | 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
[4068] | 32 | using HeuristicLab.Optimization.Operators;
|
---|
[3861] | 33 | using HeuristicLab.Parameters;
|
---|
[3852] | 34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
| 37 | [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
|
---|
[3862] | 38 | [Creatable("Problems")]
|
---|
[3852] | 39 | [StorableClass]
|
---|
[5809] | 40 | public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
[4419] | 41 | public string Filename { get; set; }
|
---|
| 42 |
|
---|
[7201] | 43 | public static new Image StaticItemImage {
|
---|
[5287] | 44 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[3859] | 45 | }
|
---|
[4419] | 46 |
|
---|
[3861] | 47 | public new ParameterCollection Parameters {
|
---|
| 48 | get { return base.Parameters; }
|
---|
| 49 | }
|
---|
| 50 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
| 51 | get { return Parameters; }
|
---|
| 52 | }
|
---|
[3859] | 53 |
|
---|
[3852] | 54 | #region Parameters
|
---|
[6189] | 55 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
| 56 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
[3859] | 57 | }
|
---|
[3872] | 58 | public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
|
---|
[3859] | 59 | get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 62 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 63 | }
|
---|
[5809] | 64 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[3859] | 65 | get { return MaximizationParameter; }
|
---|
| 66 | }
|
---|
[3861] | 67 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
|
---|
| 68 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
[3859] | 69 | }
|
---|
[5809] | 70 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
[3859] | 71 | get { return SolutionCreatorParameter; }
|
---|
| 72 | }
|
---|
[5809] | 73 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
[3859] | 74 | get { return EvaluatorParameter; }
|
---|
| 75 | }
|
---|
| 76 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 77 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 78 | }
|
---|
[5809] | 79 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
[3859] | 80 | get { return BestKnownQualityParameter; }
|
---|
| 81 | }
|
---|
| 82 | public OptionalValueParameter<IScope> BestKnownSolutionParameter {
|
---|
| 83 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
| 84 | }
|
---|
[3861] | 85 | public ValueParameter<ItemList<IOperator>> OperatorsParameter {
|
---|
| 86 | get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
|
---|
| 87 | }
|
---|
[6183] | 88 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
| 89 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
| 90 | }
|
---|
[3852] | 91 | #endregion
|
---|
| 92 |
|
---|
[3860] | 93 | #region Properties
|
---|
| 94 | public BoolValue Maximization {
|
---|
| 95 | get { return MaximizationParameter.Value; }
|
---|
| 96 | set { MaximizationParameter.Value = value; }
|
---|
| 97 | }
|
---|
[3861] | 98 | public ISolutionCreator SolutionCreator {
|
---|
[3860] | 99 | get { return SolutionCreatorParameter.Value; }
|
---|
| 100 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 101 | }
|
---|
[5809] | 102 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
[3860] | 103 | get { return SolutionCreatorParameter.Value; }
|
---|
| 104 | }
|
---|
[3861] | 105 | public IExternalEvaluationProblemEvaluator Evaluator {
|
---|
[3860] | 106 | get { return EvaluatorParameter.Value; }
|
---|
| 107 | set { EvaluatorParameter.Value = value; }
|
---|
| 108 | }
|
---|
[5809] | 109 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
[3860] | 110 | get { return EvaluatorParameter.Value; }
|
---|
| 111 | }
|
---|
[5809] | 112 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
[3860] | 113 | get { return EvaluatorParameter.Value; }
|
---|
| 114 | }
|
---|
| 115 | public DoubleValue BestKnownQuality {
|
---|
| 116 | get { return BestKnownQualityParameter.Value; }
|
---|
| 117 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 118 | }
|
---|
| 119 | public IEnumerable<IOperator> Operators {
|
---|
[3861] | 120 | get { return OperatorsParameter.Value; }
|
---|
[3860] | 121 | }
|
---|
[3872] | 122 | private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
|
---|
| 123 | get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3860] | 124 | }
|
---|
| 125 | #endregion
|
---|
| 126 |
|
---|
[3861] | 127 | [StorableConstructor]
|
---|
[3864] | 128 | private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 129 | private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
|
---|
| 130 | : base(original, cloner) {
|
---|
[7383] | 131 | RegisterEventHandlers();
|
---|
[4722] | 132 | }
|
---|
| 133 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 134 | return new ExternalEvaluationProblem(this, cloner);
|
---|
| 135 | }
|
---|
[3852] | 136 | public ExternalEvaluationProblem()
|
---|
| 137 | : base() {
|
---|
[3859] | 138 | ExternalEvaluator evaluator = new ExternalEvaluator();
|
---|
[3872] | 139 | UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
|
---|
[3859] | 140 |
|
---|
[6189] | 141 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[3859] | 142 | Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
|
---|
[3862] | 143 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
|
---|
[3861] | 144 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
| 145 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
| 146 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
| 147 | Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
|
---|
[6183] | 148 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[3861] | 149 |
|
---|
| 150 | InitializeOperators();
|
---|
[7383] | 151 | RegisterEventHandlers();
|
---|
[3852] | 152 | }
|
---|
[6189] | 153 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 154 | private void AfterDeserialization() {
|
---|
| 155 | // BackwardsCompatibility3.3
|
---|
| 156 | #region Backwards compatible code, remove with 3.4
|
---|
| 157 | if (!Parameters.ContainsKey("Clients")) {
|
---|
| 158 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
| 159 | if (Parameters.ContainsKey("Client")) {
|
---|
| 160 | var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
|
---|
| 161 | if (client != null)
|
---|
| 162 | ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
|
---|
| 163 | Parameters.Remove("Client");
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | #endregion
|
---|
[7383] | 167 | RegisterEventHandlers();
|
---|
[6189] | 168 | }
|
---|
[3861] | 169 |
|
---|
| 170 | #region Events
|
---|
| 171 | public event EventHandler SolutionCreatorChanged;
|
---|
| 172 | private void OnSolutionCreatorChanged() {
|
---|
| 173 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 174 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 175 | }
|
---|
| 176 | public event EventHandler EvaluatorChanged;
|
---|
| 177 | private void OnEvaluatorChanged() {
|
---|
| 178 | EventHandler handler = EvaluatorChanged;
|
---|
| 179 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 180 | }
|
---|
| 181 | public event EventHandler OperatorsChanged;
|
---|
| 182 | private void OnOperatorsChanged() {
|
---|
| 183 | EventHandler handler = OperatorsChanged;
|
---|
| 184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 185 | }
|
---|
| 186 | public event EventHandler Reset;
|
---|
| 187 | private void OnReset() {
|
---|
| 188 | EventHandler handler = Reset;
|
---|
| 189 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 193 | OnSolutionCreatorChanged();
|
---|
| 194 | }
|
---|
| 195 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[3872] | 196 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 197 | ParameterizeOperators();
|
---|
[3861] | 198 | OnEvaluatorChanged();
|
---|
| 199 | }
|
---|
| 200 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[3872] | 201 | ParameterizeOperators();
|
---|
[3861] | 202 | }
|
---|
| 203 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 204 | OnOperatorsChanged();
|
---|
| 205 | }
|
---|
| 206 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
| 207 | OnOperatorsChanged();
|
---|
| 208 | }
|
---|
| 209 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
| 210 | OnOperatorsChanged();
|
---|
| 211 | }
|
---|
| 212 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
| 213 | OnOperatorsChanged();
|
---|
| 214 | }
|
---|
| 215 | #endregion
|
---|
| 216 |
|
---|
[4722] | 217 | #region Helper
|
---|
[7383] | 218 | private void RegisterEventHandlers() {
|
---|
[3861] | 219 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 220 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 221 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[3862] | 222 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
[3861] | 223 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
|
---|
| 224 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
| 225 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
|
---|
| 226 | }
|
---|
[4098] | 227 | private void InitializeOperators() {
|
---|
| 228 | ItemList<IOperator> operators = OperatorsParameter.Value;
|
---|
| 229 | operators.Add(new BestScopeSolutionAnalyzer());
|
---|
| 230 | ParameterizeAnalyzers();
|
---|
| 231 | }
|
---|
[3861] | 232 | private void ParameterizeAnalyzers() {
|
---|
[3872] | 233 | BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 234 | BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 235 | BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
| 236 | BestScopeSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 237 | BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
[3861] | 238 | }
|
---|
| 239 | private void ParameterizeEvaluator() {
|
---|
[6189] | 240 | Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
|
---|
[3861] | 241 | }
|
---|
[3872] | 242 | private void ParameterizeOperators() {
|
---|
| 243 | // This is a best effort approach to wiring
|
---|
| 244 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
| 245 | foreach (IOperator op in OperatorsParameter.Value) {
|
---|
| 246 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
| 247 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 248 | }
|
---|
| 249 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
| 250 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
[3861] | 254 | #endregion
|
---|
[3852] | 255 | }
|
---|
| 256 | }
|
---|