Changeset 11996


Ignore:
Timestamp:
02/12/15 21:49:06 (7 years ago)
Author:
mkommend
Message:

#2282: Fixed maximization flag and solution creators for binary problems.

Location:
trunk/sources
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/BasicProblem.cs

    r11982 r11996  
    6767      Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
    6868      oldEncoding = Encoding;
     69      Parameterize();
    6970      RegisterEvents();
    7071    }
     
    9192
    9293    protected virtual void OnEncodingChanged() {
     94      Parameterize();
     95
     96      OnOperatorsChanged();
     97      OnReset();
     98    }
     99
     100    private void Parameterize() {
    93101      if (oldEncoding != null) {
    94102        AdaptEncodingOperators(oldEncoding, Encoding);
     
    107115        Encoding.SolutionCreator);
    108116      Parameters.Remove(SolutionCreatorParameter);
    109       Parameters.Add(solutionCreatorParam); 
    110       ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;     
     117      Parameters.Add(solutionCreatorParam);
     118      ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
    111119
    112120      var multiEncoding = Encoding as MultiEncoding;
    113121      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
    114 
    115       OnOperatorsChanged();
    116       OnReset();
    117122    }
    118123
  • trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs

    r11970 r11996  
    4141    protected MultiObjectiveBasicProblem()
    4242      : base() {
    43       Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
     43      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
    4444
    4545      Operators.Add(Evaluator);
     
    5757    public abstract double[] Evaluate(Individual individual, IRandom random);
    5858    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
    59 
    60     protected override void OnEncodingChanged() {
    61       base.OnEncodingChanged();
    62       Parameters["Maximization"].ActualValue = new BoolArray(Maximization);
    63     }
    6459
    6560    protected override void OnOperatorsChanged() {
  • trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveBasicProblem.cs

    r11990 r11996  
    5959    protected SingleObjectiveBasicProblem()
    6060      : base() {
    61       Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
     61      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
    6262      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
    6363
     
    8282    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
    8383      return Enumerable.Empty<Individual>();
    84     }
    85 
    86 
    87     protected override void OnEncodingChanged() {
    88       base.OnEncodingChanged();
    89       var max = (BoolValue)Parameters["Maximization"].ActualValue;
    90       max.Value = Maximization;
    9184    }
    9285
  • trunk/sources/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs

    r11990 r11996  
    2323
    2424using System;
     25using System.Linq;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    6768    }
    6869
     70    public abstract double Evaluate(BinaryVector vector, IRandom random);
    6971    public sealed override double Evaluate(Individual individual, IRandom random) {
    7072      return Evaluate(individual.BinaryVector(), random);
    7173    }
    7274
    73     public abstract double Evaluate(BinaryVector vector, IRandom random);
     75    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     76      base.Analyze(individuals, qualities, results, random);
     77      var best = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderByDescending(z => z.Quality).First();
     78      if (!results.ContainsKey("Best Solution")) {
     79        results.Add(new Result("Best Solution", typeof(BinaryVector)));
     80      }
     81      results["Best Solution"].Value = best.Individual.BinaryVector();
     82    }
    7483
    7584    protected override void OnEncodingChanged() {
  • trunk/sources/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs

    r11987 r11996  
    6666      : base() {
    6767      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
    68       Length = 49;
     68      Encoding.Length = 49;
    6969    }
    7070
  • trunk/sources/HeuristicLab.Problems.Binary/3.3/HIFFProblem.cs

    r11987 r11996  
    4848    public HIFFProblem()
    4949      : base() {
    50       Length = 64;
     50        Encoding.Length = 64;
    5151    }
    5252    // In the GECCO paper, Section 4.1
  • trunk/sources/HeuristicLab.Problems.Binary/3.3/OneMaxProblem.cs

    r11990 r11996  
    3939    public OneMaxProblem()
    4040      : base() {
    41       Encoding = new BinaryVectorEncoding("BinaryVector", 10);
    42       BestKnownQuality = Length;
     41      Encoding.Length = 10;
     42      BestKnownQuality = Encoding.Length;
    4343    }
    4444
     
    4949    public override IDeepCloneable Clone(Cloner cloner) {
    5050      return new OneMaxProblem(this, cloner);
    51     }
    52 
    53     public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    54       base.Analyze(individuals, qualities, results, random);
    55       var best = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderByDescending(z => z.Quality).First();
    56       if (!results.ContainsKey("Best Solution")) {
    57         results.Add(new Result("Best Solution", typeof(BinaryVector)));
    58       }
    59       results["Best Solution"].Value = best.Individual.BinaryVector();
    6051    }
    6152
  • trunk/sources/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11961 r11996  
    2424using HeuristicLab.Common.Resources;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Optimization;
    2728using HeuristicLab.Parameters;
     
    7374
    7475    private void OnProblemDefinitionChanged() {
     76      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
     77      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
     78
    7579      Encoding = ProblemDefinition.Encoding;
    7680      OnOperatorsChanged();
  • trunk/sources/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11984 r11996  
    2626using HeuristicLab.Common.Resources;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2829using HeuristicLab.Optimization;
    2930using HeuristicLab.Parameters;
     
    7677
    7778    private void OnProblemDefinitionChanged() {
     79      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
     80      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
     81
    7882      Encoding = ProblemDefinition.Encoding;
     83      OnOperatorsChanged();
     84      OnReset();
    7985    }
    8086
Note: See TracChangeset for help on using the changeset viewer.