#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Programmable { [Item("Single-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] [StorableClass] public sealed class SingleObjectiveProblemDefinitionScript : ProblemDefinitionScript, ISingleObjectiveProblemDefinition, IStorableContent { public string Filename { get; set; } 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.Optimization; using HeuristicLab.Problems.Programmable; public class CustomProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition { public bool IsMaximizationProblem { get { return false; } } public CustomProblemDefinition() { // Define the solution encoding which can also consist of multiple vectors, examples below // Encoding = new BinaryEncoding(""b"", length: 5); // Encoding = new IntegerEncoding(""i"", lenght: 5, min: 2, max: 14, step: 4); // Encoding = new RealEncoding(""r"", length: 5, min: -1.0, max: 1.0); // Encoding = new PermutationEncoding(""P"", length: 5, type: PermutationTypes.Absolute); // Encoding = new MultiEncoding() // .AddBinaryVector(""b"", length: 5) // .AddIntegerVector(""i"", length: 5, min: 2, max: 14, step: 4) // .AddRealVector(""r"", length: 5, min: -1.0, max: 1.0) // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute) ; } public override void Initialize() { // when the definition is created here you can initialize variables in the variable store } public double Evaluate(IRandom random, Individual individual) { var quality = 0.0; // use vars.yourVariable to access variables in the variable store i.e. yourVariable // quality = individual.RealVector(""r"").Sum(x => x * x); return quality; } public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { // write or update results given the range of vectors and resulting qualities // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable } public override IEnumerable GetNeighbors(IRandom random, Individual individual) { // 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) { // this is not an infinite loop as only a finite amount of samples will be drawn // it is possible to return a concrete amount of neighbors also var neighbor = (Individual)individual.Clone(); //e.g. make a bit flip in a binary parameter //var bIndex = random.Next(neighbor.BinaryVector(""b"").Length); //neighbor.BinaryVector(""b"")[bIndex] = !neighbor.BinaryVector(""b"")[bIndex]; yield return neighbor; } } // implement further classes and methods }"; } } [StorableConstructor] private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { } private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { } public SingleObjectiveProblemDefinitionScript() :base(){ Code = CodeTemplate; } public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProblemDefinitionScript(this, cloner); } public new ISingleObjectiveProblemDefinition CompiledProblemDefinition { get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; } } bool ISingleObjectiveProblemDefinition.Maximization { get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; } } double ISingleObjectiveProblemDefinition.Evaluate(Individual individual, IRandom random) { return CompiledProblemDefinition.Evaluate(individual, random); } void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { CompiledProblemDefinition.Analyze(individuals, qualities, results); } } }