#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.CodeDom;
using System.Linq;
using System.Text;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.BinPacking;
namespace HeuristicLab.Problems.BinPacking {
[StorableClass]
public abstract class PackingSequenceProblem : SingleObjectiveBasicProblem
where TSolution : class, IItem
where TEncoding : class, IEncoding {
// persistence
[StorableConstructor]
protected PackingSequenceProblem(bool deserializing) : base(deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() { }
// cloning
protected PackingSequenceProblem(PackingSequenceProblem original, Cloner cloner)
: base(original, cloner) {
}
protected PackingSequenceProblem() : base() { }
public virtual bool IsBetter(double quality, double bestQuality) {
return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
}
public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results,
IRandom random) {
if (!results.ContainsKey("Best Solution Quality")) {
results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
}
if (!results.ContainsKey("Best Solution")) {
results.Add(new Result("Best Solution", typeof(TSolution)));
}
var bestQuality = Maximization ? qualities.Max() : qualities.Min();
if (results["Best Solution Quality"].Value == null ||
IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
var bestIdx = Array.IndexOf(qualities, bestQuality);
var bestClone = (IItem)solutions[bestIdx].Clone();
results["Best Solution"].Value = bestClone;
results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
}
}
}
}