Ignore:
Timestamp:
12/22/16 16:52:32 (4 years ago)
Author:
bburlacu
Message:

#2679: Add analyzer for SingleObjectiveGoalSeekingProblem. Add result aggregation in the GoalSeekingOptimizer. Remove unused dataset parameter from the problems.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/SingleObjectiveGoalSeekingProblem.cs

    r14379 r14526  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2829using HeuristicLab.Encodings.RealVectorEncoding;
    2930using HeuristicLab.Optimization;
     
    3839  public sealed class SingleObjectiveGoalSeekingProblem : SingleObjectiveBasicProblem<RealVectorEncoding>, IGoalSeekingProblem {
    3940    #region parameter names
    40     private const string ModifiableDatasetParameterName = "Dataset";
    4141    private const string InputsParameterName = "Inputs";
    4242    private const string GoalsParameterName = "Goals";
     
    136136    public SingleObjectiveGoalSeekingProblem() {
    137137      dataset = new ModifiableDataset();
    138       Parameters.Add(new ValueParameter<IDataset>(ModifiableDatasetParameterName, dataset) { Hidden = true });
    139138      Parameters.Add(new ValueParameter<CheckedItemList<InputParameter>>(InputsParameterName));
    140139      Parameters.Add(new ValueParameter<CheckedItemList<GoalParameter>>(GoalsParameterName));
     
    161160      return quality;
    162161    }
     162
     163    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     164      var zipped = individuals.Zip(qualities, (ind, qual) => new { Individual = ind, Quality = qual }).OrderBy(x => x.Quality);
     165      var best = Maximization ? zipped.Last() : zipped.First();
     166      var realVector = best.Individual.RealVector();
     167      const string resultName = "Best Solution";
     168
     169      var columnNames = new List<string>();
     170      foreach (var goal in ActiveGoals) {
     171        columnNames.Add(goal.Name);
     172        columnNames.Add(goal.Name + " (estimated)");
     173      }
     174      foreach (var input in ActiveInputs) {
     175        columnNames.Add(input.Name);
     176        columnNames.Add(input.Name + " (estimated)");
     177        columnNames.Add(input.Name + " (deviation)");
     178      }
     179
     180      var m = new DoubleMatrix(1, columnNames.Count) { ColumnNames = columnNames };
     181      int i = 0;
     182
     183      var goals = ActiveGoals.Zip(GetEstimatedGoalValues(realVector, round: true),
     184        (goal, value) => new { TargetValue = goal.Goal, EstimatedValue = value });
     185
     186      foreach (var goal in goals) {
     187        m[0, i] = goal.TargetValue;
     188        m[0, i + 1] = goal.EstimatedValue;
     189        i += 2;
     190      }
     191
     192      var inputs = ActiveInputs.Zip(realVector,
     193        (input, value) => new { ActualValue = input.Value, EstimatedValue = value });
     194
     195      foreach (var input in inputs) {
     196        m[0, i] = input.ActualValue;
     197        m[0, i + 1] = input.EstimatedValue;
     198        m[0, i + 2] = m[0, i] - m[0, i + 1];
     199        i += 3;
     200      }
     201
     202      if (!results.ContainsKey(resultName)) {
     203        results.Add(new Result(resultName, m));
     204      } else {
     205        results[resultName].Value = m;
     206      }
     207
     208      base.Analyze(individuals, qualities, results, random);
     209    }
     210
    163211    #region event handlers
    164 
    165212    private void RegisterEvents() {
    166213      ModelsParameter.Value.ItemsAdded += ModelCollection_ItemsChanged;
Note: See TracChangeset for help on using the changeset viewer.