Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/15/20 17:09:10 (4 years ago)
Author:
abeham
Message:

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4
Files:
5 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs

    r17226 r17747  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2626using System.Threading;
    2727using Google.ProtocolBuffers;
     28using HEAL.Attic;
    2829using HeuristicLab.Analysis;
    2930using HeuristicLab.Common;
     
    3233using HeuristicLab.Optimization;
    3334using HeuristicLab.Parameters;
    34 using HEAL.Attic;
    3535
    3636namespace HeuristicLab.Problems.ExternalEvaluation {
     
    4040  // BackwardsCompatibility3.3
    4141  // Rename class to SingleObjectiveExternalEvaluationProblem
    42   public class ExternalEvaluationProblem : SingleObjectiveProblem<IEncoding<IEncodedSolution>, IEncodedSolution>, IExternalEvaluationProblem {
     42  public class ExternalEvaluationProblem<TEncoding, TEncodedSolution> : SingleObjectiveProblem<TEncoding, TEncodedSolution>, IExternalEvaluationProblem
     43    where TEncoding : class, IEncoding
     44    where TEncodedSolution : class, IEncodedSolution {
    4345
    4446    public static new Image StaticItemImage {
     
    5658      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
    5759    }
    58     public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter {
    59       get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
    60     }
    61 
    62     private IFixedValueParameter<BoolValue> MaximizationParameter {
    63       get { return (IFixedValueParameter<BoolValue>)Parameters["Maximization"]; }
     60    public IFixedValueParameter<SingleObjectiveOptimizationSupportScript<TEncodedSolution>> SupportScriptParameter {
     61      get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript<TEncodedSolution>>)Parameters["SupportScript"]; }
    6462    }
    6563    #endregion
    6664
    6765    #region Properties
    68     public new IEncoding<IEncodedSolution> Encoding {
     66    public new TEncoding Encoding {
    6967      get { return base.Encoding; }
    7068      set { base.Encoding = value; }
     
    7977      get { return MessageBuilderParameter.Value; }
    8078    }
    81     public SingleObjectiveOptimizationSupportScript OptimizationSupportScript {
     79    public SingleObjectiveOptimizationSupportScript<TEncodedSolution> OptimizationSupportScript {
    8280      get { return SupportScriptParameter.Value; }
    8381    }
    84     private ISingleObjectiveOptimizationSupport OptimizationSupport {
     82    private ISingleObjectiveOptimizationSupport<TEncodedSolution> OptimizationSupport {
    8583      get { return SupportScriptParameter.Value; }
    8684    }
     
    8987    [StorableConstructor]
    9088    protected ExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
    91     protected ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
     89    protected ExternalEvaluationProblem(ExternalEvaluationProblem<TEncoding, TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
    9290    public override IDeepCloneable Clone(Cloner cloner) {
    93       return new ExternalEvaluationProblem(this, cloner);
     91      return new ExternalEvaluationProblem<TEncoding, TEncodedSolution>(this, cloner);
    9492    }
    95     public ExternalEvaluationProblem()
    96       : base() {
    97       Parameters.Remove("Maximization"); // readonly in base class
    98       Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
     93    public ExternalEvaluationProblem(TEncoding encoding)
     94      : base(encoding) {
     95      MaximizationParameter.ReadOnly = false;
     96      MaximizationParameter.Value = new BoolValue(); // is a read-only bool value in base class
    9997      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
    10098      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
    10199      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
    102       Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript()));
     100      Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript<TEncodedSolution>>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript<TEncodedSolution>(Results)));
    103101
    104102      Operators.Add(new BestScopeSolutionAnalyzer());
     
    106104
    107105    #region Single Objective Problem Overrides
    108     public override bool Maximization {
    109       get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; }
     106    public override ISingleObjectiveEvaluationResult Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken) {
     107      var qualityMessage = Evaluate(BuildSolutionMessage(solution), cancellationToken);
     108      if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_))
     109        throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage.");
     110      var quality = qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality;
     111      return new SingleObjectiveEvaluationResult(quality);
     112
     113    }
     114    public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) {
     115      return Cache == null
     116        ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken)
     117        : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken);
    110118    }
    111119
    112     public virtual void SetMaximization(bool maximization) {
    113       MaximizationParameter.Value.Value = maximization;
     120    public override void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutions,  IRandom random) {
     121      OptimizationSupport.Analyze(solutions, random);
    114122    }
    115 
    116     public override double Evaluate(IEncodedSolution individual, IRandom random) {
    117       var qualityMessage = Evaluate(BuildSolutionMessage(individual));
    118       if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_))
    119         throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage.");
    120       return qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality;
    121     }
    122     public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
    123       return Cache == null
    124         ? EvaluateOnNextAvailableClient(solutionMessage)
    125         : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
    126     }
    127 
    128     public override void Analyze(IEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    129       OptimizationSupport.Analyze(individuals, qualities, results, random);
    130     }
    131 
    132     public override IEnumerable<IEncodedSolution> GetNeighbors(IEncodedSolution individual, IRandom random) {
    133       return OptimizationSupport.GetNeighbors(individual, random);
     123    public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) {
     124      return OptimizationSupport.GetNeighbors(solutions, random);
    134125    }
    135126    #endregion
     
    143134    #region Evaluation
    144135    private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
    145     private object clientLock = new object();
     136    private readonly object clientLock = new object();
    146137
    147     private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     138    private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) {
    148139      IEvaluationServiceClient client = null;
    149140      lock (clientLock) {
     
    158149      try {
    159150        return client.Evaluate(message, GetQualityMessageExtensions());
    160       }
    161       finally {
     151      } finally {
    162152        lock (clientLock) {
    163153          activeClients.Remove(client);
     
    167157    }
    168158
    169     private SolutionMessage BuildSolutionMessage(IEncodedSolution solution, int solutionId = 0) {
     159    private SolutionMessage BuildSolutionMessage(TEncodedSolution solution, int solutionId = 0) {
    170160      lock (clientLock) {
    171161        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
     
    176166          try {
    177167            MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
    178           }
    179           catch (ArgumentException ex) {
     168          } catch (ArgumentException ex) {
    180169            throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
    181170          }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/HeuristicLab.Problems.ExternalEvaluation-3.4.csproj

    r16816 r17747  
    138138    <Compile Include="MultiObjectiveExternalEvaluationProblem.cs" />
    139139    <Compile Include="ExternalEvaluationProblemInstances.cs" />
    140     <Compile Include="SingleObjectiveExternalEvaluationProblem.cs" />
     140    <Compile Include="ExternalEvaluationProblem.cs" />
    141141    <Compile Include="Interfaces\IEvaluationServiceClient.cs" />
    142142    <Compile Include="Interfaces\IEvaluationChannel.cs" />
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Interfaces/ISingleObjectiveOptimizationSupport.cs

    r17745 r17747  
    3030    where TEncodedSolution : class, IEncodedSolution {
    3131
     32    void InitializeResults();
    3233    void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, IRandom random);
    3334    IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledOptimizationSupport.cs

    r17226 r17747  
    2020#endregion
    2121
     22using HeuristicLab.Optimization;
     23
    2224namespace HeuristicLab.Problems.ExternalEvaluation {
    2325  public abstract class CompiledOptimizationSupport {
    2426
    2527    public dynamic vars { get; set; }
     28    public ResultCollection Results { get; set; }
    2629  }
    2730}
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/MultiObjective/MultiObjectiveOptimizationSupportScript.cs

    r16816 r17747  
    3333    private MultiObjectiveOptimizationSupportScript(StorableConstructorFlag _) : base(_) { }
    3434    private MultiObjectiveOptimizationSupportScript(MultiObjectiveOptimizationSupportScript<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
    35     public MultiObjectiveOptimizationSupportScript() {
     35    public MultiObjectiveOptimizationSupportScript() : base() {
    3636      var codeTemplate = Templates.CompiledMultiObjectiveOptimizationSupport;
    3737      codeTemplate = codeTemplate.Replace("ENCODING_NAMESPACE", typeof(TEncodedSolution).Namespace);
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/OptimizationSupportScript.cs

    r17226 r17747  
    2323using System.Linq;
    2424using System.Reflection;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    26 using HEAL.Attic;
     27using HeuristicLab.Optimization;
    2728using HeuristicLab.Scripting;
    2829
     
    3839    }
    3940
     41    [Storable]
     42    private ResultCollection results;
     43    public ResultCollection Results {
     44      get { return results; }
     45    }
     46
    4047    [StorableConstructor]
    4148    protected OptimizationSupportScript(StorableConstructorFlag _) : base(_) { }
     
    4350      : base(original, cloner) {
    4451      variableStore = cloner.Clone(original.variableStore);
     52      results = cloner.Clone(original.results);
    4553    }
    46 
    47     protected OptimizationSupportScript()
    48       : base() {
     54   
     55    [Obsolete("Do not use this constructor.")]
     56    protected OptimizationSupportScript() : base() {
    4957      variableStore = new VariableStore();
    5058    }
    5159
    52     protected OptimizationSupportScript(string code)
    53       : base(code) {
     60    protected OptimizationSupportScript(ResultCollection results)
     61      : base() {
    5462      variableStore = new VariableStore();
     63      this.results = results;
    5564    }
    5665
     
    8695        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
    8796        inst.vars = new Variables(VariableStore);
     97        inst.Results = results;
    8898      } catch (Exception e) {
    8999        compiledInstance = null;
     
    93103      var concreteInst = inst as T;
    94104      if (concreteInst == null)
    95         throw new OptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
     105        throw new OptimizationSupportException($"The optimization support class does not implement {typeof(T).FullName}." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
    96106
    97107      CompiledInstance = concreteInst;
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjective/CompiledSingleObjectiveOptimizationSupport.cs

    r16816 r17747  
    88  public class CompiledSingleObjectiveOptimizationSupport : CompiledOptimizationSupport, ISingleObjectiveOptimizationSupport<SOLUTION_CLASS> {
    99
    10     public void Analyze(SOLUTION_CLASS[] solutions, double[] qualities, ResultCollection results, IRandom random) {
     10    // private Result<StringValue> myResult;
     11
     12    public void InitializeResults() {
     13      // Initialize the results that you intend to produce
     14      // Uncomment the field above and the code below to add to the problem's results
     15      // if (!Results.TryGetValue("My Result", out myResult))
     16      //   Results.Add(myResult = new Result<StringValue>("My Result", "My result description."));
     17    }
     18
     19    public void Analyze(ISingleObjectiveSolutionContext<SOLUTION_CLASS>[] solutionContexts, IRandom random) {
    1120      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    1221      // Write or update results given the range of vectors and resulting qualities
    13       // Uncomment the following lines if you want to retrieve the best individual
     22      // Uncomment the following lines if you want to retrieve the current best individual
    1423      // Maximization:
    15       // var bestIndex = qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1;
     24      // var best = solutionContexts.OrderByDescending(x => x.EvaluationResult.Quality).First();
    1625      // Minimization:
    17       // var bestIndex = qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1;
    18       // var best = solutions[bestIndex];
     26      // var best = solutionContexts.OrderBy(x => x.EvaluationResult.Quality).First();
     27      //
     28      // myResult.Value = new StringValue("Custom result");
    1929    }
    2030
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjective/SingleObjectiveOptimizationSupportScript.cs

    r17745 r17747  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HEAL.Attic;
     
    3637    private SingleObjectiveOptimizationSupportScript(StorableConstructorFlag _) : base(_) { }
    3738    private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
    38     public SingleObjectiveOptimizationSupportScript() : base() {
     39    [Obsolete("Do not use this constructor.")]
     40    public SingleObjectiveOptimizationSupportScript() : this(null) { }
     41    public SingleObjectiveOptimizationSupportScript(ResultCollection results) : base(results) {
    3942      var codeTemplate = Templates.CompiledSingleObjectiveOptimizationSupport;
    4043      codeTemplate = codeTemplate.Replace("ENCODING_NAMESPACE", typeof(TEncodedSolution).Namespace);
     
    4548    public override IDeepCloneable Clone(Cloner cloner) {
    4649      return new SingleObjectiveOptimizationSupportScript<TEncodedSolution>(this, cloner);
     50    }
     51
     52    void ISingleObjectiveOptimizationSupport<TEncodedSolution>.InitializeResults() {
     53      CompiledInstance.InitializeResults();
    4754    }
    4855
Note: See TracChangeset for help on using the changeset viewer.