[5112] | 1 | using System;
|
---|
[5653] | 2 | using System.Collections;
|
---|
[5112] | 3 | using System.Collections.Generic;
|
---|
[5665] | 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
[5653] | 6 | using HeuristicLab.Common;
|
---|
[5112] | 7 | using HeuristicLab.Core;
|
---|
[5653] | 8 | using HeuristicLab.Data;
|
---|
| 9 | using HeuristicLab.Optimization;
|
---|
[5112] | 10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 11 |
|
---|
| 12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 13 | // todo: storable, name, descr, ...
|
---|
| 14 | [StorableClass]
|
---|
[5653] | 15 | public class ParameterConfigurationTree : ParameterizedValueConfiguration, IEnumerable {
|
---|
[5212] | 16 |
|
---|
[5112] | 17 | [Storable]
|
---|
[6018] | 18 | private DoubleValue quality;
|
---|
| 19 | public DoubleValue Quality {
|
---|
| 20 | get { return quality; }
|
---|
[5112] | 21 | set {
|
---|
[6018] | 22 | if (quality != value) {
|
---|
| 23 | quality = value;
|
---|
[5112] | 24 | OnQualityChanged();
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | [Storable]
|
---|
[6018] | 30 | private DoubleArray normalizedQualityAverages;
|
---|
| 31 | public DoubleArray NormalizedQualityAverages {
|
---|
| 32 | get { return normalizedQualityAverages; }
|
---|
[5212] | 33 | set {
|
---|
[6018] | 34 | if (normalizedQualityAverages != value) {
|
---|
| 35 | normalizedQualityAverages = value;
|
---|
[5212] | 36 | }
|
---|
| 37 | }
|
---|
[5112] | 38 | }
|
---|
| 39 |
|
---|
| 40 | [Storable]
|
---|
[6018] | 41 | private DoubleArray normalizedQualityDeviations;
|
---|
| 42 | public DoubleArray NormalizedQualityDeviations {
|
---|
| 43 | get { return normalizedQualityDeviations; }
|
---|
| 44 | set {
|
---|
| 45 | if (normalizedQualityDeviations != value) {
|
---|
| 46 | normalizedQualityDeviations = value;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [Storable]
|
---|
| 52 | private DoubleArray normalizedEvaluatedSolutions;
|
---|
| 53 | public DoubleArray NormalizedEvaluatedSolutions {
|
---|
| 54 | get { return normalizedEvaluatedSolutions; }
|
---|
| 55 | set {
|
---|
| 56 | if (normalizedEvaluatedSolutions != value) {
|
---|
| 57 | normalizedEvaluatedSolutions = value;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
[8091] | 61 |
|
---|
[6018] | 62 | [Storable]
|
---|
[5212] | 63 | private DoubleArray bestQualities;
|
---|
| 64 | public DoubleArray BestQualities {
|
---|
| 65 | get { return bestQualities; }
|
---|
| 66 | set {
|
---|
| 67 | if (bestQualities != value) {
|
---|
| 68 | bestQualities = value;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[5112] | 71 | }
|
---|
| 72 |
|
---|
| 73 | [Storable]
|
---|
[5212] | 74 | private DoubleArray averageQualities;
|
---|
| 75 | public DoubleArray AverageQualities {
|
---|
| 76 | get { return averageQualities; }
|
---|
| 77 | set { averageQualities = value; }
|
---|
[5112] | 78 | }
|
---|
| 79 |
|
---|
| 80 | [Storable]
|
---|
[5212] | 81 | private DoubleArray worstQualities;
|
---|
| 82 | public DoubleArray WorstQualities {
|
---|
| 83 | get { return worstQualities; }
|
---|
| 84 | set { worstQualities = value; }
|
---|
[5112] | 85 | }
|
---|
| 86 |
|
---|
| 87 | [Storable]
|
---|
[5212] | 88 | private DoubleArray qualityVariances;
|
---|
| 89 | public DoubleArray QualityVariances {
|
---|
| 90 | get { return qualityVariances; }
|
---|
| 91 | set { qualityVariances = value; }
|
---|
[5112] | 92 | }
|
---|
| 93 |
|
---|
| 94 | [Storable]
|
---|
[5212] | 95 | private DoubleArray qualityStandardDeviations;
|
---|
| 96 | public DoubleArray QualityStandardDeviations {
|
---|
| 97 | get { return qualityStandardDeviations; }
|
---|
| 98 | set { qualityStandardDeviations = value; }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | [Storable]
|
---|
| 102 | private ItemList<TimeSpanValue> averageExecutionTimes;
|
---|
| 103 | public ItemList<TimeSpanValue> AverageExecutionTimes {
|
---|
| 104 | get { return averageExecutionTimes; }
|
---|
| 105 | set { averageExecutionTimes = value; }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | [Storable]
|
---|
[6018] | 109 | private DoubleArray averageEvaluatedSolutions;
|
---|
| 110 | public DoubleArray AverageEvaluatedSolutions {
|
---|
| 111 | get { return averageEvaluatedSolutions; }
|
---|
| 112 | set { averageEvaluatedSolutions = value; }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | [Storable]
|
---|
[5112] | 116 | private IntValue repetitions;
|
---|
| 117 | public IntValue Repetitions {
|
---|
| 118 | get { return repetitions; }
|
---|
| 119 | set { repetitions = value; }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | [Storable]
|
---|
| 123 | protected RunCollection runs;
|
---|
| 124 | public RunCollection Runs {
|
---|
| 125 | get { return runs; }
|
---|
| 126 | set { runs = value; }
|
---|
[5184] | 127 | }
|
---|
[5112] | 128 |
|
---|
[5184] | 129 | [Storable]
|
---|
[5112] | 130 | protected IDictionary<string, IItem> parameters;
|
---|
| 131 | public IDictionary<string, IItem> Parameters {
|
---|
| 132 | get { return parameters; }
|
---|
| 133 | set { parameters = value; }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[5665] | 136 | public ParameterizedValueConfiguration AlgorithmConfiguration {
|
---|
| 137 | get {
|
---|
| 138 | return this.ParameterConfigurations.ElementAt(0).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
| 139 | }
|
---|
[5313] | 140 | }
|
---|
| 141 |
|
---|
[5665] | 142 | public ParameterizedValueConfiguration ProblemConfiguration {
|
---|
| 143 | get {
|
---|
| 144 | return this.ParameterConfigurations.ElementAt(1).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[5112] | 148 | #region constructors and cloning
|
---|
[5665] | 149 | public ParameterConfigurationTree(IAlgorithm algorithm, IProblem problem)
|
---|
| 150 | : base(null, algorithm.GetType(), false) {
|
---|
| 151 | this.Optimize = false;
|
---|
| 152 | this.IsOptimizable = false;
|
---|
[5112] | 153 | this.parameters = new Dictionary<string, IItem>();
|
---|
[5313] | 154 | this.Name = algorithm.ItemName;
|
---|
[5665] | 155 |
|
---|
| 156 | var algproblemitem = new AlgorithmProblemItem();
|
---|
| 157 | algproblemitem.AlgorithmParameter.Value = algorithm;
|
---|
| 158 | algproblemitem.ProblemParameter.Value = problem;
|
---|
| 159 | this.discoverValidValues = false;
|
---|
[6018] | 160 |
|
---|
[5665] | 161 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Algorithm", algproblemitem.AlgorithmParameter));
|
---|
| 162 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Problem", algproblemitem.ProblemParameter));
|
---|
[6489] | 163 |
|
---|
| 164 | // problems can be modified in the list of problem instances, so the parameters which are not Optimize=true,
|
---|
| 165 | // must not be modifiable in the parameter configuration tree. otherwise the parameter values would be ambiguous
|
---|
| 166 | ProblemConfiguration.ValuesReadOnly = true;
|
---|
[5112] | 167 | }
|
---|
[5665] | 168 | public ParameterConfigurationTree() { }
|
---|
[5112] | 169 | [StorableConstructor]
|
---|
| 170 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
| 171 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
| 172 | : base(original, cloner) {
|
---|
[6018] | 173 | this.quality = cloner.Clone(original.quality);
|
---|
| 174 | this.normalizedQualityAverages = cloner.Clone(original.normalizedQualityAverages);
|
---|
| 175 | this.normalizedQualityDeviations = cloner.Clone(original.normalizedQualityDeviations);
|
---|
| 176 | this.normalizedEvaluatedSolutions = cloner.Clone(original.normalizedEvaluatedSolutions);
|
---|
[5212] | 177 | this.bestQualities = cloner.Clone(original.BestQualities);
|
---|
| 178 | this.averageQualities = cloner.Clone(original.averageQualities);
|
---|
| 179 | this.worstQualities = cloner.Clone(original.worstQualities);
|
---|
| 180 | this.qualityStandardDeviations = cloner.Clone(original.qualityStandardDeviations);
|
---|
| 181 | this.qualityVariances = cloner.Clone(original.qualityVariances);
|
---|
| 182 | this.averageExecutionTimes = cloner.Clone(original.averageExecutionTimes);
|
---|
[6018] | 183 | this.averageEvaluatedSolutions = cloner.Clone(original.averageEvaluatedSolutions);
|
---|
[5184] | 184 | this.repetitions = cloner.Clone(original.repetitions);
|
---|
| 185 | this.runs = cloner.Clone(original.runs);
|
---|
[5112] | 186 | this.parameters = new Dictionary<string, IItem>();
|
---|
[8091] | 187 | if (original.parameters != null) {
|
---|
| 188 | foreach (var p in original.parameters) {
|
---|
| 189 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
| 190 | }
|
---|
[5112] | 191 | }
|
---|
| 192 | }
|
---|
| 193 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 194 | return new ParameterConfigurationTree(this, cloner);
|
---|
| 195 | }
|
---|
| 196 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 197 | private void AfterDeserialization() {
|
---|
[8091] | 198 | if (ProblemConfiguration != null) ProblemConfiguration.ValuesReadOnly = true;
|
---|
[5112] | 199 | }
|
---|
| 200 | #endregion
|
---|
| 201 |
|
---|
| 202 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
[5212] | 203 | values.Add("RunsAverageExecutionTimes", AverageExecutionTimes);
|
---|
[6018] | 204 | values.Add("RunsAverageEvaluatedSolutions", AverageEvaluatedSolutions);
|
---|
[5112] | 205 | values.Add("Repetitions", Repetitions);
|
---|
[5212] | 206 | values.Add("RunsBestQualities", BestQualities);
|
---|
| 207 | values.Add("RunsAverageQualities", AverageQualities);
|
---|
| 208 | values.Add("RunsWorstQualities", WorstQualities);
|
---|
| 209 | values.Add("RunsQualityVariances", QualityVariances);
|
---|
| 210 | values.Add("RunsQualityStandardDeviations", QualityStandardDeviations);
|
---|
[6018] | 211 | values.Add("QualitiesNormalized", NormalizedQualityAverages);
|
---|
| 212 | values.Add("AverageQualityNormalized", Quality);
|
---|
[5112] | 213 | values.Add("Runs", Runs);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 217 | foreach (var p in parameters) {
|
---|
| 218 | values.Add(p);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | #region Events
|
---|
| 223 | public event EventHandler QualityChanged;
|
---|
| 224 | private void OnQualityChanged() {
|
---|
| 225 | var handler = QualityChanged;
|
---|
| 226 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
| 230 | OnQualityChanged();
|
---|
| 231 | }
|
---|
| 232 | #endregion
|
---|
| 233 |
|
---|
| 234 | public override void Parameterize(IParameterizedItem item) {
|
---|
[5207] | 235 | this.parameters.Clear();
|
---|
[5665] | 236 | var algorithm = item as IAlgorithm;
|
---|
| 237 | var problem = algorithm.Problem;
|
---|
| 238 |
|
---|
[6421] | 239 | ProblemConfiguration.Parameterize(problem);
|
---|
[5665] | 240 | AlgorithmConfiguration.Parameterize(algorithm);
|
---|
| 241 |
|
---|
[5927] | 242 | algorithm.CollectParameterValues(this.Parameters);
|
---|
[5112] | 243 | }
|
---|
[5144] | 244 |
|
---|
[5184] | 245 | public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
|
---|
[5144] | 246 | Experiment experiment = new Experiment();
|
---|
[5653] | 247 | foreach (ParameterizedValueConfiguration combination in this) {
|
---|
[5144] | 248 | IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
|
---|
[5184] | 249 | clonedAlg.Name = combination.ParameterInfoString;
|
---|
[5144] | 250 | combination.Parameterize(clonedAlg);
|
---|
[5184] | 251 | clonedAlg.StoreAlgorithmInEachRun = false;
|
---|
| 252 | if (createBatchRuns) {
|
---|
| 253 | BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
|
---|
[5313] | 254 | batchRun.Optimizer = clonedAlg;
|
---|
[5184] | 255 | batchRun.Repetitions = repetitions;
|
---|
| 256 | experiment.Optimizers.Add(batchRun);
|
---|
| 257 | } else {
|
---|
| 258 | experiment.Optimizers.Add(clonedAlg);
|
---|
| 259 | }
|
---|
[5144] | 260 | }
|
---|
| 261 | return experiment;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[5184] | 264 | public Experiment GenerateExperiment(IAlgorithm algorithm) {
|
---|
| 265 | return GenerateExperiment(algorithm, false, 0);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[5144] | 268 | public IEnumerator GetEnumerator() {
|
---|
| 269 | IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
|
---|
| 270 | enumerator.Reset();
|
---|
| 271 | return enumerator;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[5328] | 274 | /// <summary>
|
---|
| 275 | /// returns the number of possible parameter combinations
|
---|
| 276 | /// </summary>
|
---|
| 277 | /// <param name="max">algorithm stops counting when max is reached. zero for infinite counting</param>
|
---|
| 278 | /// <returns></returns>
|
---|
| 279 | public long GetCombinationCount(long max) {
|
---|
[5144] | 280 | long cnt = 0;
|
---|
| 281 | foreach (var c in this) {
|
---|
| 282 | cnt++;
|
---|
[5328] | 283 | if (max > 0 && cnt >= max) {
|
---|
| 284 | return cnt;
|
---|
| 285 | }
|
---|
[5144] | 286 | }
|
---|
| 287 | return cnt;
|
---|
| 288 | }
|
---|
[5303] | 289 |
|
---|
| 290 | public IOptimizable GetRandomOptimizable(IRandom random) {
|
---|
| 291 | List<IOptimizable> allOptimizables = GetAllOptimizables();
|
---|
| 292 | return allOptimizables[random.Next(allOptimizables.Count)];
|
---|
| 293 | }
|
---|
[5313] | 294 |
|
---|
| 295 | public override string ToString() {
|
---|
| 296 | return this.Name;
|
---|
| 297 | }
|
---|
[5576] | 298 |
|
---|
[6197] | 299 | public IRun ToRun(bool clearParameters) {
|
---|
| 300 | return ToRun(this.ParameterInfoString, clearParameters);
|
---|
[5576] | 301 | }
|
---|
| 302 |
|
---|
[6197] | 303 | public IRun ToRun(string name, bool clearParameters) {
|
---|
[5576] | 304 | IRun run = new Run();
|
---|
| 305 | run.Name = name;
|
---|
| 306 | this.CollectResultValues(run.Results);
|
---|
| 307 | this.CollectParameterValues(run.Parameters);
|
---|
[8091] | 308 | if (clearParameters) MetaOptimizationUtil.ClearParameters(run, this.GetOptimizedParameterNames());
|
---|
[5576] | 309 | return run;
|
---|
| 310 | }
|
---|
[5665] | 311 |
|
---|
| 312 | public override string ParameterInfoString {
|
---|
| 313 | get {
|
---|
| 314 | string algorithmInfo = this.AlgorithmConfiguration.ParameterInfoString;
|
---|
| 315 | string problemInfo = this.ProblemConfiguration.ParameterInfoString;
|
---|
| 316 | var sb = new StringBuilder();
|
---|
| 317 | if (!string.IsNullOrEmpty(algorithmInfo)) {
|
---|
[6018] | 318 | sb.Append("Algorithm (");
|
---|
[5665] | 319 | sb.Append(algorithmInfo);
|
---|
[6018] | 320 | sb.Append(")");
|
---|
[5665] | 321 | }
|
---|
| 322 | if (!string.IsNullOrEmpty(problemInfo)) {
|
---|
| 323 | if (sb.Length > 0)
|
---|
| 324 | sb.Append(", ");
|
---|
[6018] | 325 | sb.Append("Problem( ");
|
---|
[5665] | 326 | sb.Append(problemInfo);
|
---|
[6018] | 327 | sb.Append(")");
|
---|
[5665] | 328 | }
|
---|
| 329 | return sb.ToString();
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
[5927] | 332 |
|
---|
| 333 | public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
|
---|
| 334 | AlgorithmConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
| 335 | ProblemConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
| 336 | }
|
---|
[5112] | 337 | }
|
---|
| 338 | }
|
---|