#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 <http://www.gnu.org/licenses/>.
 */
#endregion

using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;

namespace HeuristicLab.Problems.PFSP
{
    /// <summary>
    /// A base class for operators which evaluate TSP solutions given in path representation using city coordinates.
    /// </summary>
    [Item("MakeSpanEvaluator", "A base class for operators which evaluate TSP solutions given in path representation using city coordinates.")]
    [StorableClass]
    public abstract class MakeSpanEvaluator : PFSPEvaluator, IMakespanEvaluator
    {
        private object locker = new object();

        public ILookupParameter<Permutation> PermutationParameter { get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; } }
        public ILookupParameter<JobMatrix> JobMatrixParameter { get { return (ILookupParameter<JobMatrix>)Parameters["JobMatrix"]; } }

        [StorableConstructor]
        protected MakeSpanEvaluator(bool deserializing) : base(deserializing) { }
        protected MakeSpanEvaluator(MakeSpanEvaluator original, Cloner cloner) : base(original, cloner) { }
        protected MakeSpanEvaluator()
          : base()
        {
            Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
            Parameters.Add(new LookupParameter<JobMatrix>("JobMatrix", "The matrix which contains the distances between the cities."));
        }

        [StorableHook(HookType.AfterDeserialization)]
        private void AfterDeserialization()
        {
            // BackwardsCompatibility3.3
            #region Backwards compatible code (remove with 3.4)
            LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["JobMatrix"] as LookupParameter<DoubleMatrix>;
            if (oldDistanceMatrixParameter != null)
            {
                Parameters.Remove(oldDistanceMatrixParameter);
                Parameters.Add(new LookupParameter<JobMatrix>("JobMatrix", "The matrix which contains the distances between the cities."));
                JobMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
            }
            #endregion
        }

        public static double Apply(MakeSpanEvaluator evaluator, DoubleMatrix matrix, Permutation tour)
        {
            double length = evaluator.CalculateMakespan(matrix, tour);
            return length;
        }

        public sealed override IOperation InstrumentedApply()
        {
            Permutation p = PermutationParameter.ActualValue;
            DoubleMatrix c = JobMatrixParameter.ActualValue;
            if (c == null) throw new InvalidOperationException("No coordinates were given.");

            double length = CalculateMakespan(c, p);
            QualityParameter.ActualValue = new DoubleValue(length);
            
            return base.InstrumentedApply();
        }

        protected abstract double CalculateMakespan(DoubleMatrix matrix, Permutation tour);
    }
}
