#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Optimization;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Common;
using HeuristicLab.Parameters;
using HeuristicLab.Data;
using HeuristicLab.Problems.Scheduling.Encodings;
namespace HeuristicLab.Problems.Scheduling.Analyzers {
[Item("BestSchedulingSolutionAnalyzer", "An operator for analyzing the best solution of Scheduling Problems given in schedule-representation.")]
[StorableClass]
public sealed class BestSchedulingSolutionAnalyzer : JSSPOperator, IAnalyzer, IStochasticOperator {
[StorableConstructor]
private BestSchedulingSolutionAnalyzer(bool deserializing) : base(deserializing) { }
private BestSchedulingSolutionAnalyzer(BestSchedulingSolutionAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BestSchedulingSolutionAnalyzer(this, cloner);
}
public ILookupParameter RandomParameter {
get { return (LookupParameter)Parameters["Random"]; }
}
public IRandom Random {
get { return RandomParameter.ActualValue; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public LookupParameter BestSolutionParameter {
get { return (LookupParameter)Parameters["BestSolution"]; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public LookupParameter BestKnownQualityParameter {
get { return (LookupParameter)Parameters["BestKnownQuality"]; }
}
public LookupParameter BestKnownSolutionParameter {
get { return (LookupParameter)Parameters["BestKnownSolution"]; }
}
public ScopeTreeLookupParameter SchedulingSolutionParameter {
get { return (ScopeTreeLookupParameter)Parameters["DecodedSchedulingSolution"]; }
}
public BestSchedulingSolutionAnalyzer () {
Parameters.Add(new LookupParameter("Random", "The pseudo random number generator."));
Parameters.Add(new LookupParameter("Maximization", "True if the problem is a maximization problem."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the JSSP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("DecodedSchedulingSolution", "The solutions from which the best solution has to be chosen from."));
Parameters.Add(new LookupParameter("BestSolution", "The best JSSP solution."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best JSSP solution should be stored."));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this JSSP instance."));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution of this JSSP instance."));
}
public override IOperation Apply() {
ItemList jobs = Jobs;
ItemArray qualities = QualityParameter.ActualValue;
ItemArray solutions = SchedulingSolutionParameter.ActualValue;
ResultCollection results = ResultsParameter.ActualValue;
bool max = MaximizationParameter.ActualValue.Value;
DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
int i = -1;
if (!max)
i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
if (bestKnownQuality == null ||
max && qualities[i].Value > bestKnownQuality.Value ||
!max && qualities[i].Value < bestKnownQuality.Value) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
BestKnownSolutionParameter.ActualValue = (Schedule)solutions[i].Clone();
}
Schedule bestSolution = BestSolutionParameter.ActualValue;
if (bestSolution == null) {
bestSolution = (Schedule)solutions[i].Clone();
bestSolution.Quality.Value = qualities [i].Value;
BestSolutionParameter.ActualValue = bestSolution;
results.Add(new Result("Best Scheduling Solution", bestSolution));
} else {
if (max && bestSolution.Quality.Value < qualities[i].Value ||
!max && bestSolution.Quality.Value > qualities[i].Value) {
bestSolution = (Schedule)solutions[i].Clone();
bestSolution.Quality.Value = qualities[i].Value;
}
}
return base.Apply();
}
}
}