Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Analyzers/BestBinPackingSolutionAnalyzer.cs @ 9440

Last change on this file since 9440 was 9440, checked in by jhelm, 11 years ago

#1966: Implemented new encoding (MultiComponentVector/MCV); Implemented move-operators for MCV and GV encodings; Implemented new decoding-methods for PS, GV and MCV encodings (ExtremePoint-based packing);

File size: 4.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.BinPacking.Interfaces;
34using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
35using HeuristicLab.Problems.BinPacking.Shapes;
36
37namespace HeuristicLab.Problems.BinPacking.Analyzers {     
38  [Item("BestBinPackingSolutionAnalyzer", "An operator for analyzing the best solution of BinPacking Problems given in packingPlan-representation.")]
39  [StorableClass]
40  public class BestBinPackingSolutionAnalyzer<D, B, I> : BinPackingAnalyzer<D, B, I>, IStochasticOperator
41    where D : class, IPackingDimensions
42    where B : PackingShape<D>, IPackingBin
43    where I : PackingShape<D>, IPackingItem {
44
45    protected BestBinPackingSolutionAnalyzer(BestBinPackingSolutionAnalyzer<D,B,I> original, Cloner cloner)
46      : base(original, cloner) {
47    }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new BestBinPackingSolutionAnalyzer<D,B,I>(this, cloner);
50    }
51
52
53    public ILookupParameter<IRandom> RandomParameter {
54      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
55    }
56    public IRandom Random {
57      get { return RandomParameter.ActualValue; }
58    }
59
60    public BestBinPackingSolutionAnalyzer()
61      : base() {
62      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
63    }
64
65    public override IOperation Apply() {
66      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
67      ItemArray<PackingPlan<D, B, I>> solutions = PackingPlanParameter.ActualValue;
68      ResultCollection results = ResultsParameter.ActualValue;
69      bool max = MaximizationParameter.ActualValue.Value;
70      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
71
72      int i = -1;
73      if (!max)
74        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
75      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
76
77      if (bestKnownQuality == null ||
78          max && qualities[i].Value > bestKnownQuality.Value ||
79          !max && qualities[i].Value < bestKnownQuality.Value) {
80        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
81        BestKnownSolutionParameter.ActualValue = (PackingPlan<D, B, I>)solutions[i].Clone();
82      }
83
84      PackingPlan<D, B, I> bestSolution = BestSolutionParameter.ActualValue;
85      if (bestSolution == null) {
86        bestSolution = (PackingPlan<D, B, I>)solutions[i].Clone();
87        bestSolution.Quality = (DoubleValue)qualities[i].Clone();
88        BestSolutionParameter.ActualValue = bestSolution;
89        results.Add(new Result("Best Packing Solution", bestSolution));
90      } else {
91        if (max && bestSolution.Quality.Value < qualities[i].Value ||
92          !max && bestSolution.Quality.Value > qualities[i].Value) {
93          bestSolution.Quality.Value = qualities[i].Value;
94          bestSolution.PackingItemPositions = new ObservableDictionary<int, D> (solutions[i].PackingItemPositions);
95        }
96      }
97
98      return base.Apply();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.