Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11893 for branches


Ignore:
Timestamp:
02/04/15 21:38:28 (9 years ago)
Author:
abeham
Message:

#2174:

  • Added possibility to define neighborhood and analyze function for external evaluation problems
Location:
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4
Files:
4 added
3 edited
4 copied

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.DotSettings
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs

    r11892 r11893  
    2929using HeuristicLab.Core;
    3030using HeuristicLab.Data;
     31using HeuristicLab.Optimization;
    3132using HeuristicLab.Parameters;
    3233using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    5455      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
    5556    }
     57    public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter {
     58      get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
     59    }
    5660
    5761    public EvaluationCache Cache {
     
    6468      get { return MessageBuilderParameter.Value; }
    6569    }
     70    public SingleObjectiveOptimizationSupportScript OptimizationSupportScript {
     71      get { return SupportScriptParameter.Value; }
     72    }
     73    private ISingleObjectiveOptimizationSupport OptimizationSupport {
     74      get { return SupportScriptParameter.Value; }
     75    }
    6676
    6777    [StorableConstructor]
    6878    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
    69     private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
     79
     80    private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
     81      : base(original, cloner) {
     82      try {
     83        OptimizationSupportScript.Compile();
     84      } catch (SingleObjectiveOptimizationSupportException ex) {
     85        PluginInfrastructure.ErrorHandling.ShowErrorDialog("Support script doesn't compile.", ex);
     86      }
     87    }
    7088    public override IDeepCloneable Clone(Cloner cloner) {
    7189      return new ExternalEvaluationProblem(this, cloner);
     
    7694      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
    7795      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     96      Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript()));
     97    }
     98
     99    [StorableHook(HookType.AfterDeserialization)]
     100    private void AfterDeserialization() {
     101      try {
     102        OptimizationSupportScript.Compile();
     103      } catch (SingleObjectiveOptimizationSupportException ex) {
     104        PluginInfrastructure.ErrorHandling.ShowErrorDialog("Support script doesn't compile.", ex);
     105      }
    78106    }
    79107
     
    85113      return Cache == null ? EvaluateOnNextAvailableClient(BuildSolutionMessage(individual)).Quality
    86114        : Cache.GetValue(BuildSolutionMessage(individual), m => EvaluateOnNextAvailableClient(m).Quality);
     115    }
     116
     117    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     118      OptimizationSupport.Analyze(individuals, qualities, results, random);
     119    }
     120
     121    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
     122      return OptimizationSupport.GetNeighbors(individual, random);
    87123    }
    88124
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/HeuristicLab.Problems.ExternalEvaluation-3.4.csproj

    r11892 r11893  
    105105      <Private>False</Private>
    106106    </Reference>
     107    <Reference Include="HeuristicLab.Scripting-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     108      <SpecificVersion>False</SpecificVersion>
     109      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Scripting-3.3.dll</HintPath>
     110      <Private>False</Private>
     111    </Reference>
    107112    <Reference Include="System" />
    108113    <Reference Include="System.Core">
     
    138143    <Compile Include="Interfaces\IEvaluationChannel.cs" />
    139144    <Compile Include="Interfaces\IItemToSolutionMessageConverter.cs" />
     145    <Compile Include="Interfaces\ISingleObjectiveOptimizationSupport.cs" />
    140146    <Compile Include="Plugin.cs" />
     147    <Compile Include="Programmable\CompiledOptimizationSupport.cs" />
     148    <Compile Include="Programmable\CompiledSingleObjectiveOptimizationSupport.cs" />
     149    <Compile Include="Programmable\SingleObjectiveOptimizationSupportScript.cs" />
     150    <Compile Include="Programmable\SingleObjectiveOptimizationSupportScriptException.cs" />
     151    <Compile Include="Programmable\Templates.Designer.cs">
     152      <AutoGen>True</AutoGen>
     153      <DesignTime>True</DesignTime>
     154      <DependentUpon>Templates.resx</DependentUpon>
     155    </Compile>
    141156    <Compile Include="Properties\AssemblyInfo.cs" />
    142157    <None Include="Properties\AssemblyInfo.cs.frame" />
     
    234249      <Install>true</Install>
    235250    </BootstrapperPackage>
     251  </ItemGroup>
     252  <ItemGroup>
     253    <EmbeddedResource Include="Programmable\Templates.resx">
     254      <Generator>ResXFileCodeGenerator</Generator>
     255      <LastGenOutput>Templates.Designer.cs</LastGenOutput>
     256    </EmbeddedResource>
    236257  </ItemGroup>
    237258  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledOptimizationSupport.cs

    r11886 r11893  
    2020#endregion
    2121
    22 using System;
     22using System.Collections.Generic;
     23using HeuristicLab.Core;
     24using HeuristicLab.Optimization;
     25using HeuristicLab.Problems.Programmable;
    2326
    24 namespace HeuristicLab.Problems.Programmable {
    25   public abstract class CompiledProblemDefinition : IProblemDefinition {
    26     private IEncoding encoding;
    27     public IEncoding Encoding {
    28       get { return encoding; }
    29       protected set {
    30         if (value == null) throw new ArgumentNullException("The encoding must not be null.");
    31         encoding = value;
    32       }
    33     }
     27namespace HeuristicLab.Problems.ExternalEvaluation {
     28  public abstract class CompiledOptimizationSupport {
    3429
    3530    public dynamic vars { get; set; }
    36     public abstract void Initialize();
    37 
    38     protected CompiledProblemDefinition() { }
    39     protected CompiledProblemDefinition(IEncoding encoding)
    40       : base() {
    41       Encoding = encoding;
    42     }
    4331  }
    4432}
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledSingleObjectiveOptimizationSupport.cs

    r11886 r11893  
    55using HeuristicLab.Core;
    66using HeuristicLab.Data;
    7 using HeuristicLab.Encodings.PermutationEncoding;
    87using HeuristicLab.Optimization;
    98using HeuristicLab.Problems.Programmable;
    109
    11 namespace HeuristicLab.Problems.Programmable {
    12   public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
    13     public bool Maximization { get { return false; } }
    14 
    15     public override void Initialize() {
    16       // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    17       // Define the solution encoding which can also consist of multiple vectors, examples below
    18       //Encoding = new BinaryEncoding("b", length: 5);
    19       //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 2);
    20       //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0);
    21       //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
    22       // The encoding can also be a combination
    23       //Encoding = new MultiEncoding()
    24       //.Add(new BinaryEncoding("b", length: 5))
    25       //.Add(new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 4))
    26       //.Add(new RealEncoding("r", length: 5, min: -1.0, max: 1.0))
    27       //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
    28       ;
    29       // Add additional initialization code e.g. private variables that you need for evaluating
    30     }
    31 
    32     public double Evaluate(Individual individual, IRandom random) {
    33       // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    34       var quality = 0.0;
    35       //quality = individual.RealVector("r").Sum(x => x * x);
    36       return quality;
    37     }
     10namespace HeuristicLab.Problems.ExternalEvaluation {
     11  public class CompiledSingleObjectiveOptimizationSupport : CompiledOptimizationSupport, ISingleObjectiveOptimizationSupport {
    3812
    3913    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs

    r11886 r11893  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using System.Reflection;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     28using HeuristicLab.Optimization;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.ExternalEvaluation.Programmable;
     31using HeuristicLab.Problems.Programmable;
    2832using HeuristicLab.Scripting;
    2933
    30 namespace HeuristicLab.Problems.Programmable {
     34namespace HeuristicLab.Problems.ExternalEvaluation {
    3135  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
    3236  [StorableClass]
    33   public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
    34     protected bool SuppressEvents { get; set; }
     37  public sealed class SingleObjectiveOptimizationSupportScript : Script, ISingleObjectiveOptimizationSupport {
    3538
    3639    [Storable]
     
    4043    }
    4144
     45    protected override string CodeTemplate {
     46      get { return Templates.CompiledSingleObjectiveOptimizationSupport; }
     47    }
     48
    4249    [StorableConstructor]
    43     protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
    44     protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
     50    private SingleObjectiveOptimizationSupportScript(bool deserializing) : base(deserializing) { }
     51    private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner)
    4552      : base(original, cloner) {
    4653      variableStore = cloner.Clone(original.variableStore);
    4754    }
    48     protected ProblemDefinitionScript()
     55    public SingleObjectiveOptimizationSupportScript()
    4956      : base() {
    5057      variableStore = new VariableStore();
    5158    }
    5259
    53     IEncoding IProblemDefinition.Encoding {
    54       get { return CompiledProblemDefinition.Encoding; }
     60    public override IDeepCloneable Clone(Cloner cloner) {
     61      return new SingleObjectiveOptimizationSupportScript(this, cloner);
    5562    }
    5663
    57     private readonly object locker = new object();
    58     private volatile IProblemDefinition compiledProblemDefinition;
    59     protected IProblemDefinition CompiledProblemDefinition {
     64    private volatile ISingleObjectiveOptimizationSupport compiledInstance;
     65    private ISingleObjectiveOptimizationSupport CompiledInstance {
    6066      get {
    61         if (compiledProblemDefinition == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
    62         return compiledProblemDefinition;
     67        if (compiledInstance == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
     68        return compiledInstance;
    6369      }
    64       private set {
    65         compiledProblemDefinition = value;
    66         OnProblemDefinitionChanged();
    67       }
     70      set { compiledInstance = value; }
    6871    }
    6972
     
    7174      var assembly = base.Compile();
    7275      var types = assembly.GetTypes();
    73       if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
    74         throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
    75       if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
    76         throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
     76      if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
     77        throw new SingleObjectiveOptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
     78      if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
     79        throw new SingleObjectiveOptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
    7780
    78       CompiledProblemDefinition inst;
     81      CompiledOptimizationSupport inst;
    7982      try {
    80         inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
     83        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
     84        inst.vars = new Variables(VariableStore);
    8185      } catch (Exception e) {
    82         compiledProblemDefinition = null;
    83         throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
     86        compiledInstance = null;
     87        throw new SingleObjectiveOptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
    8488      }
    8589
    86       try {
    87         inst.vars = new Variables(VariableStore);
    88         inst.Initialize();
    89       } catch (Exception e) {
    90         compiledProblemDefinition = null;
    91         throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
    92       }
     90      var soInst = inst as ISingleObjectiveOptimizationSupport;
     91      if (soInst == null)
     92        throw new SingleObjectiveOptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
    9393
    94       try {
    95         CompiledProblemDefinition = inst;
    96       } catch (Exception e) {
    97         compiledProblemDefinition = null;
    98         throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e);
    99       }
     94      CompiledInstance = soInst;
    10095
    10196      return assembly;
     
    10499    protected override void OnCodeChanged() {
    105100      base.OnCodeChanged();
    106       compiledProblemDefinition = null;
     101      compiledInstance = null;
    107102    }
    108103
    109     public event EventHandler ProblemDefinitionChanged;
    110     protected virtual void OnProblemDefinitionChanged() {
    111       var handler = ProblemDefinitionChanged;
    112       if (handler != null) handler(this, EventArgs.Empty);
     104    void ISingleObjectiveOptimizationSupport.Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     105      CompiledInstance.Analyze(individuals, qualities, results, random);
     106    }
     107
     108    IEnumerable<Individual> ISingleObjectiveOptimizationSupport.GetNeighbors(Individual individual, IRandom random) {
     109      return CompiledInstance.GetNeighbors(individual, random);
    113110    }
    114111  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScriptException.cs

    r11886 r11893  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    423using System.Runtime.Serialization;
    5 using System.Text;
    6 using System.Threading.Tasks;
    724
    8 namespace HeuristicLab.Problems.Programmable {
     25namespace HeuristicLab.Problems.ExternalEvaluation {
    926  [Serializable]
    10   public class ProblemDefinitionScriptException : Exception {
    11     public ProblemDefinitionScriptException() { }
    12     public ProblemDefinitionScriptException(string message) : base(message) { }
    13     public ProblemDefinitionScriptException(string message, Exception inner) : base(message, inner) { }
     27  public class SingleObjectiveOptimizationSupportException : Exception {
     28    public SingleObjectiveOptimizationSupportException() { }
     29    public SingleObjectiveOptimizationSupportException(string message) : base(message) { }
     30    public SingleObjectiveOptimizationSupportException(string message, Exception inner) : base(message, inner) { }
    1431
    15     protected ProblemDefinitionScriptException(SerializationInfo info, StreamingContext context)
     32    protected SingleObjectiveOptimizationSupportException(SerializationInfo info, StreamingContext context)
    1633      : base(info, context) { }
    1734  }
Note: See TracChangeset for help on using the changeset viewer.