#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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.Drawing; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; namespace HeuristicLab.Problems.Programmable { [Item("Multi-objective Problem Definition", "This definition can be dynamically created through code and assigned to the problem.")] public class MultiObjectiveProblemDefinition : IMultiObjectiveProblemDefinitionHost, IMultiObjectiveProblemDefinition { private bool[] maximization; public bool[] Maximization { get { return maximization; } set { maximization = value; OnInstanceChanged(); } } private Func configuration; public Func Configuration { get { return configuration; } set { configuration = value; OnInstanceChanged(); } } private Func evaluate; public Func Evaluate { get { return evaluate; } set { evaluate = value; OnInstanceChanged(); } } private Action analyze; public Action Analyze { get { return analyze; } set { analyze = value; OnInstanceChanged(); } } private Func> neighbors; public Func> Neighbors { get { return neighbors; } set { neighbors = value; OnInstanceChanged(); } } protected MultiObjectiveProblemDefinition(MultiObjectiveProblemDefinition original, Cloner cloner) { cloner.RegisterClonedObject(original, this); name = original.name; description = original.description; maximization = original.maximization; configuration = original.configuration; evaluate = original.evaluate; analyze = original.analyze; neighbors = original.neighbors; } public MultiObjectiveProblemDefinition() : this((string)null, null) { } public MultiObjectiveProblemDefinition(string name) : this(name, null) { } public MultiObjectiveProblemDefinition(string name, string description) { this.name = name ?? ItemName; this.description = description ?? ItemDescription; configuration = () => new Configuration(); evaluate = (random, vector) => new[] { 0.0, 0.0 }; analyze = (vectors, doubles, results) => { }; neighbors = (random, vector) => new[] { (ParameterVector)vector.Clone() }; } public IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveProblemDefinition(this, cloner); } public IMultiObjectiveProblemDefinition Instance { get { return this; } } public event EventHandler InstanceChanged; protected void OnInstanceChanged() { var handler = InstanceChanged; if (handler != null) handler(this, EventArgs.Empty); } #region Problem definition explicit interface implementations dynamic IProblemDefinition.vars { get; set; } void IProblemDefinition.Initialize() { } Configuration IProblemDefinition.GetConfiguration() { return Configuration(); } bool[] IMultiObjectiveProblemDefinition.Maximization { get { return Maximization; } } double[] IMultiObjectiveProblemDefinition.Evaluate(IRandom random, ParameterVector vector) { return Evaluate(random, vector); } void IMultiObjectiveProblemDefinition.Analyze(ParameterVector[] vectors, double[][] qualities, ResultCollection results) { Analyze(vectors, qualities, results); } IEnumerable IMultiObjectiveProblemDefinition.GetNeighbors(IRandom random, ParameterVector vector) { return Neighbors(random, vector); } #endregion #region INamedItem protected string name; /// /// Calls and also /// eventually in the setter. public string Name { get { return name; } set { if (!CanChangeName) throw new NotSupportedException("Name cannot be changed."); if (!(name.Equals(value) || (value == null) && (name == string.Empty))) { CancelEventArgs e = value == null ? new CancelEventArgs(string.Empty) : new CancelEventArgs(value); OnNameChanging(e); if (!e.Cancel) { name = value == null ? string.Empty : value; OnNameChanged(); } } } } public virtual bool CanChangeName { get { return true; } } protected string description; public string Description { get { return description; } set { if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed."); if (!(description.Equals(value) || (value == null) && (description == string.Empty))) { description = value == null ? string.Empty : value; OnDescriptionChanged(); } } } public virtual bool CanChangeDescription { get { return true; } } /// /// Gets the string representation of the current instance in the format: Name: [null|Value]. /// /// The current instance as a string. public override string ToString() { return Name; } /// public event EventHandler> NameChanging; /// /// Fires a new NameChanging event. /// /// The event arguments of the changing. protected virtual void OnNameChanging(CancelEventArgs e) { var handler = NameChanging; if (handler != null) handler(this, e); } /// public event EventHandler NameChanged; /// /// Fires a new NameChanged event. /// /// Calls . protected virtual void OnNameChanged() { var handler = NameChanged; if (handler != null) handler(this, EventArgs.Empty); OnToStringChanged(); } /// public event EventHandler DescriptionChanged; /// /// Fires a new DescriptionChanged event. /// /// Calls . protected virtual void OnDescriptionChanged() { var handler = DescriptionChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion #region IItem public virtual string ItemName { get { return ItemAttribute.GetName(this.GetType()); } } public virtual string ItemDescription { get { return ItemAttribute.GetDescription(this.GetType()); } } public Version ItemVersion { get { return ItemAttribute.GetVersion(this.GetType()); } } public static Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; } } public virtual Image ItemImage { get { return ItemAttribute.GetImage(this.GetType()); } } public object Clone() { return Clone(new Cloner()); } public event EventHandler ItemImageChanged; protected virtual void OnItemImageChanged() { var handler = ItemImageChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ToStringChanged; protected virtual void OnToStringChanged() { var handler = ToStringChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion } }