#region License Information /* HeuristicLab * Copyright (C) 2002-2018 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 HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; namespace HeuristicLab.Problems.Modifiers { [StorableType("D5B5D05B-FBF5-434F-AB25-E382D83FAA0B")] [Item("MultiObjectiveEndPointProblemModifier", "A dummy problem modifier leading to a multi-objective basic problem")] public class MultiObjectiveEndPointProblemModifier : ProblemModifier where T : class, IEncoding { private MultiObjectiveBasicProblem problem; [Storable] public MultiObjectiveBasicProblem Problem { get => problem; set { if (problem == value) return; if (problem != null) { problem.SolutionCreatorChanged -= OnUpdateRequired; if (problem.Encoding?.Parameters != null) { foreach (var encodingParameter in problem.Encoding.Parameters.OfType()) encodingParameter.ValueChanged -= OnParameterValueChanged; } } problem = value; UpdateState(); try { //this is to catch inconsistent states during deserialization var enc = problem.Encoding.Parameters; } catch (NullReferenceException) { return; } problem.SolutionCreatorChanged += OnUpdateRequired; if (problem.Encoding?.Parameters == null) return; foreach (var encodingParameter in problem.Encoding.Parameters.OfType()) encodingParameter.ValueChanged += OnParameterValueChanged; } } private void OnUpdateRequired(object o, EventArgs e) { UpdateState(); } private void OnParameterValueChanged(object o, EventArgs e) { UpdateState(); ((IValueParameter)o).Value.ToStringChanged += OnUpdateRequired; } public override IEncoding Encoding => Problem?.Encoding; public override BoolArray Maximization { get { if (Problem == null) return new BoolArray(new[] { false, false }); var dict = new Dictionary(); Problem.CollectParameterValues(dict); return dict.ContainsKey("Maximization") ? (BoolArray)dict["Maximization"] : new BoolArray(problem.Maximization); } } #region constructors and cloning public override ISolutionCreator SolutionCreator => Problem?.SolutionCreator; [StorableConstructor] protected MultiObjectiveEndPointProblemModifier(StorableConstructorFlag _) : base(_) { } protected MultiObjectiveEndPointProblemModifier(MultiObjectiveEndPointProblemModifier original, Cloner cloner) : base(original, cloner) { Problem = cloner.Clone(original?.Problem); } public MultiObjectiveEndPointProblemModifier() { } public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveEndPointProblemModifier(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (problem == null) return; problem.SolutionCreatorChanged += OnUpdateRequired; if (problem.Encoding?.Parameters == null) return; foreach (var encodingParameter in problem.Encoding.Parameters.OfType()) encodingParameter.ValueChanged += OnParameterValueChanged; } #endregion public override double[] ModifiedEvaluate(Individual individual, IRandom random) { return Problem.Evaluate(individual, random); } public override void ModifiedAnalyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { Problem.Analyze(individuals, qualities, results, random); } public void UpdateState() { InvokeEncodingChanged(); InvokeMaximizationChanged(); InvokeSolutionCreatorChanged(); } } }