#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Programmable { [Item("Single-objective Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] [StorableClass] public class SingleObjectiveScript : ProgrammableProblemScript, IStorableContent { public string Filename { get; set; } [StorableConstructor] protected SingleObjectiveScript(bool deserializing) : base(deserializing) { } protected SingleObjectiveScript(SingleObjectiveScript original, Cloner cloner) : base(original, cloner) { } public SingleObjectiveScript() { Code = CodeTemplate; } public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveScript(this, cloner); } public new ISingleObjectiveProblemDefinition Instance { get { return (ISingleObjectiveProblemDefinition)base.Instance; } protected set { base.Instance = value; } } protected override string CodeTemplate { get { return @"using System; using System.Linq; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Problems.Programmable; public class ProblemDefinition : ISingleObjectiveProblemDefinition { public ProblemDefinition() { // initialize private fields } public bool IsMaximizationProblem { get { return false; } } public Configuration GetConfiguration() { return new Configuration() // .AddBinary(""b"", length: 5) // .AddInteger(""i"", length: 5, min: 2, max: 14, step: 4) // .AddReal(""r"", length: 5, min: -1.0, max: 1.0) // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute) ; } public double Evaluate(IRandom random, ParameterVector vector) { var quality = 0.0; // quality = vector.Real(""r"").Select(x => x * x).Sum(); return quality; } public IEnumerable GetNeighbors(IRandom random, ParameterVector vector) { // Create new vectors, based on the given one that represent small changes // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.) while (true) { var neighbor = (ParameterVector)vector.Clone(); //e.g. make a bit flip in a binary parameter //var bIndex = random.Next(neighbor.Binary(""b"").Length); //neighbor.Binary(""b"")[bIndex] = !neighbor.Binary(""b"")[bIndex]; yield return neighbor; } } // implement further classes and methods }"; } } } }