Changeset 11880


Ignore:
Timestamp:
02/04/15 00:03:14 (8 years ago)
Author:
abeham
Message:

#2174:

  • Added IImprovmentOperator interface to SingleObjectiveImprover
  • Added Random parameter to Analyze method and IStochasticOperator interface to the resp. analyzer
  • Updated code template text of the compiled single- and multi-objective problem definitions
  • Prevent ProblemDefinitionScript from silently ignoring exceptions
  • Added equality comparer in Encoding's cloning constructor
  • Small adaption of ProblemDefinitionScriptView to changes in new code editor
Location:
branches/ProgrammableProblem
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/ProblemDefinitionScriptView.Designer.cs

    r11398 r11880  
    142142      this.Controls.Add(this.splitContainer2);
    143143      this.Name = "ProblemDefinitionScriptView";
    144       this.Controls.SetChildIndex(this.compilationLabel, 0);
     144      this.Controls.SetChildIndex(this.infoTextLabel, 0);
    145145      this.Controls.SetChildIndex(this.compileButton, 0);
    146146      this.Controls.SetChildIndex(this.splitContainer2, 0);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11753 r11880  
    5252        encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>());
    5353
    54         T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType());
    55         if (newSolutionCreator == null) newSolutionCreator = encodingOperators.OfType<T>().First();
     54        T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
     55                              encodingOperators.OfType<T>().First();
    5656        SolutionCreator = newSolutionCreator;
    5757        OnOperatorsChanged();
     
    8383    [StorableConstructor]
    8484    protected Encoding(bool deserializing) : base(deserializing) { }
    85 
    8685    protected Encoding(Encoding<T> original, Cloner cloner)
    8786      : base(original, cloner) {
    88       encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone));
     87      encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone), new TypeEqualityComparer<IOperator>());
    8988      solutionCreator = cloner.Clone(original.solutionCreator);
    9089    }
     
    9796    public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
    9897    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
    99 
    10098
    10199    public event EventHandler SolutionCreatorChanged;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiObjectiveProblemDefinition.cs

    r11739 r11880  
    2727    bool[] Maximization { get; }
    2828    double[] Evaluate(Individual individual, IRandom random);
    29     void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results);
     29    void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random);
    3030  }
    3131}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/ISingleObjectiveProblemDefinition.cs

    r11753 r11880  
    2828    bool Maximization { get; }
    2929    double Evaluate(Individual individual, IRandom random);
    30     void Analyze(Individual[] individuals, double[] qualities, ResultCollection results);
     30    void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random);
    3131    IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random);
    3232  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/internal/IMultiObjectiveAnalysisOperator.cs

    r11739 r11880  
    2121
    2222using System;
     23using HeuristicLab.Core;
    2324using HeuristicLab.Optimization;
    2425
    2526namespace HeuristicLab.Problems.Programmable {
    2627  internal interface IMultiObjectiveAnalysisOperator : IEncodingOperator, IAnalyzer {
    27     Action<Individual[], double[][], ResultCollection> AnalyzeAction { get; set; }
     28    Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
    2829  }
    2930}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs

    r11739 r11880  
    2121
    2222using System;
     23using HeuristicLab.Core;
    2324using HeuristicLab.Optimization;
    2425
    2526namespace HeuristicLab.Problems.Programmable {
    2627  internal interface ISingleObjectiveAnalysisOperator : IEncodingOperator {
    27     Action<Individual[], double[], ResultCollection> AnalyzeAction { get; set; }
     28    Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
    2829  }
    2930}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/BasicProblem.cs

    r11814 r11880  
    139139
    140140      var operators = oldOperators.Intersect(newOperators, comparer).Select(op => cloner.Clone(op));
    141       operators = operators.Union(newOperators, comparer);
     141      operators = operators.Union(newOperators, comparer).ToList();
    142142
    143143      newEncoding.ConfigureOperators(operators);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveBasicProblem.cs

    r11814 r11880  
    5757    public abstract bool[] Maximization { get; }
    5858    public abstract double[] Evaluate(Individual individual, IRandom random);
    59     public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) { }
     59    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
    6060
    6161    protected override void OnEncodingChanged() {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveProgrammableProblem.cs

    r11814 r11880  
    5050      try {
    5151        ProblemScript.Compile();
    52       }
    53       catch (InvalidOperationException) {
     52      } catch (InvalidOperationException) {
    5453        //Compilation error
    5554      }
     
    7271      try {
    7372        ProblemScript.Compile();
    74       }
    75       catch (InvalidOperationException) {
     73      } catch (InvalidOperationException) {
    7674        //Compilation error
    7775      }
     
    9694    }
    9795
    98     public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
    99       ProblemDefinition.Analyze(individuals, qualities, results);
     96    public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
     97      ProblemDefinition.Analyze(individuals, qualities, results, random);
    10098    }
    10199  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/OneMaxNew.cs

    r11814 r11880  
    5555    }
    5656
    57     public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
    58       base.Analyze(individuals, qualities, results);
     57    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     58      base.Analyze(individuals, qualities, results, random);
    5959      var best = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderByDescending(z => z.Quality).First();
    6060      if (!results.ContainsKey("Best Solution")) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs

    r11797 r11880  
    6060    }
    6161
    62     void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
    63       CompiledProblemDefinition.Analyze(individuals, qualities, results);
     62    void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
     63      CompiledProblemDefinition.Analyze(individuals, qualities, results, random);
    6464    }
    6565  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs

    r11767 r11880  
    7676        inst.Initialize();
    7777        CompiledProblemDefinition = inst;
    78       }
    79       catch {
     78      } catch (Exception e) {
    8079        compiledProblemDefinition = null;
     80        throw;
    8181      }
    8282      return assembly;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.cs

    r11797 r11880  
    6060    }
    6161
    62     void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
    63       CompiledProblemDefinition.Analyze(individuals, qualities, results);
     62    void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     63      CompiledProblemDefinition.Analyze(individuals, qualities, results, random);
    6464    }
    6565    IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/Templates/CompiledMultiObjectiveProblemDefinition.cs

    r11786 r11880  
    1414
    1515    public override void Initialize() {
     16      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    1617      // Define the solution encoding which can also consist of multiple vectors, examples below
    1718      //Encoding = new BinaryEncoding("b", length: 5);
     
    1920      //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0);
    2021      //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
    21 
     22      // The encoding can also be a combination
    2223      //Encoding = new MultiEncoding()
    2324      //.Add(new BinaryEncoding("b", length: 5))
     
    2627      //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
    2728      ;
     29      // Add additional initialization code e.g. private variables that you need for evaluating
    2830    }
    2931
    3032    public double[] Evaluate(Individual individual, IRandom random) {
     33      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    3134      var qualities = new[] { 0.0, 0.0 };
    32       // use vars.yourVariable to access variables in the variable store i.e. yourVariable
    33       // qualities = new [] { individual.RealVector("r").Sum(x => x * x), individual.RealVector("r").Sum(x => x * x * x) };
     35      //qualities = new [] { individual.RealVector("r").Sum(x => x * x), individual.RealVector("r").Sum(x => x * x * x) };
    3436      return qualities;
    3537    }
    3638
    37     public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
    38       // write or update results given the range of vectors and resulting qualities
    39       // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable
     39    public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
     40      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
     41      // Write or update results given the range of vectors and resulting qualities
    4042    }
    41     // implement further classes and methods
     43    // Implement further classes and methods
    4244  }
    4345}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/Templates/CompiledSingleObjectiveProblemDefinition.cs

    r11753 r11880  
    1414
    1515    public override void Initialize() {
     16      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    1617      // Define the solution encoding which can also consist of multiple vectors, examples below
    1718      //Encoding = new BinaryEncoding("b", length: 5);
    18       //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 4);
     19      //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 2);
    1920      //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0);
    2021      //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
    21 
     22      // The encoding can also be a combination
    2223      //Encoding = new MultiEncoding()
    2324      //.Add(new BinaryEncoding("b", length: 5))
     
    2526      //.Add(new RealEncoding("r", length: 5, min: -1.0, max: 1.0))
    2627      //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
    27       //;
     28      ;
     29      // Add additional initialization code e.g. private variables that you need for evaluating
    2830    }
    2931
    3032    public double Evaluate(Individual individual, IRandom random) {
     33      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    3134      var quality = 0.0;
    32       // use vars.yourVariable to access variables in the variable store i.e. yourVariable
    3335      //quality = individual.RealVector("r").Sum(x => x * x);
    3436      return quality;
    3537    }
    3638
    37     public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
    38       // write or update results given the range of vectors and resulting qualities
    39       // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable
     39    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     40      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
     41      // Write or update results given the range of vectors and resulting qualities
     42      // Uncomment the following lines if you want to retrieve the best individual
     43      //var bestIndex = Maximization ?
     44      //         qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1
     45      //       : qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1;
     46      //var best = individuals[bestIndex];
    4047    }
    4148
    4249    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
     50      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    4351      // Create new vectors, based on the given one that represent small changes
    44       // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.)
     52      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
    4553      while (true) {
    46         // this is not an infinite loop as only a finite amount of samples will be drawn
    47         // it is possible to return a concrete amount of neighbors also
     54        // Algorithm will draw only a finite amount of samples
     55        // Change to a for-loop to return a concrete amount of neighbors
    4856        var neighbor = individual.Copy();
    49         //e.g. make a bit flip in a binary parameter
     57        // For instance, perform a single bit-flip in a binary parameter
    5058        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
    5159        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
     
    5462    }
    5563
    56     // implement further classes and methods
     64    // Implement further classes and methods
    5765  }
    5866}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveBasicProblem.cs

    r11814 r11880  
    6666    public abstract bool Maximization { get; }
    6767    public abstract double Evaluate(Individual individual, IRandom random);
    68     public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { }
     68    public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
    6969    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
    7070      return Enumerable.Empty<Individual>();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs

    r11814 r11880  
    5151      try {
    5252        ProblemScript.Compile();
    53       }
    54       catch (InvalidOperationException) {
     53      } catch (InvalidOperationException) {
    5554        //Compilation error
    5655      }
     
    7574      try {
    7675        ProblemScript.Compile();
    77       }
    78       catch (InvalidOperationException) {
     76      } catch (InvalidOperationException) {
    7977        //Compilation error
    8078      }
     
    9795    }
    9896
    99     public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
    100       ProblemDefinition.Analyze(individuals, qualities, results);
     97    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     98      ProblemDefinition.Analyze(individuals, qualities, results, random);
    10199    }
    102100    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs

    r11739 r11880  
    1313  [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")]
    1414  [StorableClass]
    15   public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator {
     15  public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator, IStochasticOperator {
    1616    public bool EnabledByDefault { get { return true; } }
    1717
     
    2828    }
    2929
    30     public Action<Individual[], double[][], ResultCollection> AnalyzeAction { get; set; }
     30    public ILookupParameter<IRandom> RandomParameter {
     31      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     32    }
     33
     34    public Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
    3135
    3236    [StorableConstructor]
     
    4751      var encoding = EncodingParameter.ActualValue;
    4852      var results = ResultsParameter.ActualValue;
     53      var random = RandomParameter.ActualValue;
    4954
    5055      IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
     
    5358
    5459      var individuals = scopes.Select(encoding.GetIndividual).ToArray();
    55       AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results);
     60      AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results, random);
    5661      return base.Apply();
    5762    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs

    r11739 r11880  
    1313  [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
    1414  [StorableClass]
    15   public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer {
     15  public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer, IStochasticOperator {
    1616    public bool EnabledByDefault { get { return true; } }
    1717
     
    2828    }
    2929
    30     public Action<Individual[], double[], ResultCollection> AnalyzeAction { get; set; }
     30    public ILookupParameter<IRandom> RandomParameter {
     31      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     32    }
     33
     34    public Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
    3135
    3236    [StorableConstructor]
     
    3741      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    3842      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
     43      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    3944    }
    4045
     
    4651      var encoding = EncodingParameter.ActualValue;
    4752      var results = ResultsParameter.ActualValue;
     53      var random = RandomParameter.ActualValue;
    4854
    4955      IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
     
    5258
    5359      var individuals = scopes.Select(encoding.GetIndividual).ToArray();
    54       AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results);
     60      AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random);
    5561      return base.Apply();
    5662    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs

    r11739 r11880  
    3434  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
    3535  [StorableClass]
    36   public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
     36  public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
    3737    public ILookupParameter<IRandom> RandomParameter {
    3838      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
Note: See TracChangeset for help on using the changeset viewer.