Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14324


Ignore:
Timestamp:
10/05/16 15:34:15 (8 years ago)
Author:
bburlacu
Message:

#2679: Refactor problems (rename to goal seeking, simplify code, simplify interface, remove unused methods)

Location:
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Analyzers/BestSolutionAnalyzer.cs

    r14321 r14324  
    3232using HeuristicLab.Problems.DataAnalysis;
    3333
    34 namespace HeuristicLab.ProcessParameterOptimization {
     34namespace HeuristicLab.GoalSeeking {
    3535  [StorableClass]
    3636  [Item("BestSolutionAnalyzer", "An analyzer which identifies the best solution from the SingleObjectiveProcessParameterOptimizationProblem")]
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/GoalSeekingProblem.csproj

    r14321 r14324  
    99    <OutputType>Library</OutputType>
    1010    <AppDesignerFolder>Properties</AppDesignerFolder>
    11     <RootNamespace>HeuristicLab.ProcessParameterOptimization</RootNamespace>
    12     <AssemblyName>HeuristicLab.ProcessParameterOptimization-3.3</AssemblyName>
     11    <RootNamespace>HeuristicLab.GoalSeekingProblem</RootNamespace>
     12    <AssemblyName>HeuristicLab.GoalSeekingProblem-3.4</AssemblyName>
    1313    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1414    <FileAlignment>512</FileAlignment>
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/IGoalSeekingProblem.cs

    r14321 r14324  
    2727using HeuristicLab.Problems.DataAnalysis;
    2828
    29 namespace HeuristicLab.ProcessParameterOptimization {
     29namespace HeuristicLab.GoalSeeking {
    3030  public interface IGoalSeekingProblem : IProblem {
    3131    IRegressionProblemData ProblemData { get; set; }
    32     double GetVariableValue(string variableName);
    33     void SetVariableValue(string variableName, double variableValue);
    3432
    3533    //rounding ?
    36     double[] GetEstimatedGoalValues(IEnumerable<double> parameters, bool round = false);
     34    IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false);
    3735    int Row { get; set; }
    3836
     
    4442
    4543    #region targets
    46     IEnumerable<string> ActiveTargets { get; }
    47     IEnumerable<string> Targets { get; }
    48     ICheckedItemList<StringValue> TargetList { get; }
    4944    double GetTargetGoal(string target);
    5045    void SetTargetGoal(string target, double goal);
     
    6459
    6560    #region parameters
    66     IEnumerable<string> ActiveParameters { get; }
    6761    ICheckedItemList<StringValue> ControllableParameters { get; }
    6862    DoubleMatrix ControllableParameterBounds { get; }
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/MultiObjectiveGoalSeekingProblem.cs

    r14321 r14324  
    3434using HeuristicLab.Problems.DataAnalysis;
    3535
    36 namespace HeuristicLab.ProcessParameterOptimization {
    37   [Item("Process parameter optimization problem (multi-objective)", "Represents a single objective optimization problem which uses configurable regression models to evaluate targets from a given dataset.")]
     36namespace HeuristicLab.GoalSeeking {
     37  [Item("Goal seeking problem (multi-objective)", "Represents a single objective optimization problem which uses configurable regression models to evaluate targets from a given dataset.")]
    3838  [Creatable("Problems")]
    3939  [StorableClass]
     
    5656    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
    5757      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
    58     }
    59     public IValueParameter<IDataset> ModifiableDatasetParameter {
    60       get { return (IValueParameter<IDataset>)Parameters[ModifiableDatasetParameterName]; }
    6158    }
    6259    public IValueParameter<CheckedItemList<StringValue>> ControllableParametersParameter {
     
    129126
    130127    #region targets
    131     public IEnumerable<string> ActiveTargets {
    132       get { return TargetList.CheckedItems.Select(x => x.Value.Value); }
    133     }
    134 
    135     public IEnumerable<string> Targets {
    136       get { return TargetList.Select(x => x.Value); }
    137     }
    138 
    139128    public ICheckedItemList<StringValue> TargetList {
    140129      get { return TargetsParameter.Value; }
    141130      set { TargetsParameter.Value = (CheckedItemList<StringValue>)value; }
    142131    }
     132    // convenience method
     133    private IEnumerable<string> ActiveTargets {
     134      get { return TargetList.CheckedItems.Select(x => x.Value.Value); }
     135    }
    143136    #endregion
    144137
    145138    #region parameters
    146     public IEnumerable<string> ActiveParameters {
    147       get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); }
    148     }
    149 
    150139    public ICheckedItemList<StringValue> ControllableParameters {
    151140      get { return ControllableParametersParameter.Value; }
    152141      set { ControllableParametersParameter.Value = (CheckedItemList<StringValue>)value; }
    153142    }
    154 
     143    // convenience method
     144    private IEnumerable<string> ActiveParameters {
     145      get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); }
     146    }
    155147    public DoubleMatrix ControllableParameterBounds {
    156148      get { return ControllableParameterBoundsParameter.Value; }
     
    162154    #region IProcessParameterOptimizationProblem methods
    163155    #region models
    164     public double GetVariableValue(string variableName) {
    165       return dataset.GetDoubleValue(variableName, 0);
    166     }
    167 
    168     public void SetVariableValue(string variableName, double variableValue) {
    169       dataset.SetVariableValue(variableValue, variableName, 0); // will throw if the variable is not present in the dataset
    170     }
    171 
    172     public double[] GetEstimatedGoalValues(IEnumerable<double> parameters, bool round = false) {
     156    public IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false) {
    173157      var ds = (ModifiableDataset)dataset.Clone();
    174       var estimatedValues = new double[ActiveTargets.Count()];
    175       var e1 = parameters.GetEnumerator();
    176       var e2 = ActiveParameters.GetEnumerator();
    177       while (e1.MoveNext() && e2.MoveNext())
    178         ds.SetVariableValue(e1.Current, e2.Current, 0);
     158      foreach (var parameter in ActiveParameters.Zip(parameterValues, (p, v) => new { Name = p, Value = v })) {
     159        ds.SetVariableValue(parameter.Value, parameter.Name, 0);
     160      }
    179161      var rows = new[] { 0 }; // actually just one row
    180       int i = 0;
    181       var models = ModelCollection.ToList();
    182       foreach (var target in TargetList.CheckedItems) {
    183         var average = 0d;
    184         var count = 0;
    185         var targetName = target.Value.Value;
    186         foreach (var model in models) {
    187           if (targetName != model.TargetVariable)
    188             continue;
    189           average += model.GetEstimatedValues(ds, rows).Single();
    190           count++;
    191         }
    192         var estimatedValue = average / count;
    193         if (round) {
    194           var accuracy = GetTargetStepSize(targetName);
    195           estimatedValues[i] = RoundToNearestStepMultiple(estimatedValue, accuracy);
    196         } else {
    197           estimatedValues[i] = estimatedValue;
    198         }
    199         ++i;
    200       }
     162
     163      var estimatedValues =
     164        round ? ActiveTargets.Select(t => RoundToNearestStepMultiple(GetModels(t).Average(m => m.GetEstimatedValues(ds, rows).Single()), GetTargetStepSize(t)))
     165              : ActiveTargets.Select(t => GetModels(t).Average(m => m.GetEstimatedValues(ds, rows).Single()));
    201166      return estimatedValues;
    202167    }
     
    245210
    246211    public double GetTargetGoal(string target) {
    247       if (!Targets.Contains(target))
     212      if (!IsValidTarget(target))
    248213        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    249214      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    252217
    253218    public void SetTargetGoal(string target, double goal) {
    254       if (!Targets.Contains(target))
     219      if (!IsValidTarget(target))
    255220        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    256221      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    260225
    261226    public double GetTargetWeight(string target) {
    262       if (!Targets.Contains(target))
     227      if (!IsValidTarget(target))
    263228        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    264229      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    267232
    268233    public void SetTargetWeight(string target, double weight) {
    269       if (!Targets.Contains(target))
     234      if (!IsValidTarget(target))
    270235        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    271236      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    275240
    276241    public double GetTargetVariance(string target) {
    277       if (!Targets.Contains(target))
     242      if (!IsValidTarget(target))
    278243        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    279244      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    282247
    283248    public void SetTargetVariance(string target, double variance) {
    284       if (!Targets.Contains(target))
     249      if (!IsValidTarget(target))
    285250        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    286251      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    290255
    291256    public double GetTargetStepSize(string target) {
    292       if (!Targets.Contains(target))
     257      if (!IsValidTarget(target))
    293258        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    294259      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    297262
    298263    public void SetTargetStepSize(string target, double stepSize) {
    299       if (!Targets.Contains(target))
     264      if (!IsValidTarget(target))
    300265        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    301266      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    313278
    314279    #region process parameters
    315     public void SetParameterBounds(string parameterName, double min, double max, double step) {
    316       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameterName).Count();
    317       if (i < ControllableParameterBounds.Rows) {
    318         ControllableParameterBounds[i, 0] = min;
    319         ControllableParameterBounds[i, 1] = max;
    320         ControllableParameterBounds[i, 2] = step;
    321         UpdateEncoding();
    322         OnParametersChanged(this, EventArgs.Empty);
    323       } else {
    324         throw new ArgumentException(string.Format("SetParameterBounds: Invalid parameter name {0}", parameterName));
    325       }
    326 
    327     }
    328 
    329     public double GetParameterStepSize(string parameter) {
    330       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
    331       if (i < ControllableParameterBounds.Rows)
    332         return ControllableParameterBounds[i, 2];
    333       throw new ArgumentException(string.Format("GetParameterStepSize: Invalid parameter name {0}", parameter));
    334     }
    335 
    336     public void SetParameterStepSize(string parameter, double stepSize) {
    337       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
    338       if (i < ControllableParameterBounds.Rows) {
    339         ControllableParameterBounds[i, 2] = stepSize;
    340         OnParametersChanged(this, EventArgs.Empty);
    341       } else {
    342         throw new ArgumentException(string.Format("SetParameterStepSize: Invalid parameter name {0}", parameter));
    343       }
    344     }
    345 
    346 
    347     public bool GetParameterActive(string parameter) {
    348       var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
    349       if (item == null)
    350         throw new ArgumentException(string.Format("GetParameterActive: Invalid target name {0}", parameter));
    351       return ControllableParameters.ItemChecked(item);
    352     }
    353 
    354     public void SetParameterActive(string parameter, bool active) {
    355       var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
    356       if (item == null)
    357         throw new ArgumentException(string.Format("SetParameterActive: Invalid target name {0}", parameter));
    358       ControllableParameters.SetItemCheckedState(item, active);
    359       OnParametersChanged(this, EventArgs.Empty);
    360     }
    361 
    362280    /// <summary>
    363281    /// Returns the parameter bounds (min and max) and the step size for the specified parameter
     
    376294    }
    377295
     296    public void SetParameterBounds(string parameterName, double min, double max, double step) {
     297      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameterName).Count();
     298      if (i < ControllableParameterBounds.Rows) {
     299        ControllableParameterBounds[i, 0] = min;
     300        ControllableParameterBounds[i, 1] = max;
     301        ControllableParameterBounds[i, 2] = step;
     302        UpdateEncoding();
     303        OnParametersChanged(this, EventArgs.Empty);
     304      } else {
     305        throw new ArgumentException(string.Format("SetParameterBounds: Invalid parameter name {0}", parameterName));
     306      }
     307
     308    }
     309
     310    public double GetParameterStepSize(string parameter) {
     311      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
     312      if (i < ControllableParameterBounds.Rows)
     313        return ControllableParameterBounds[i, 2];
     314      throw new ArgumentException(string.Format("GetParameterStepSize: Invalid parameter name {0}", parameter));
     315    }
     316
     317    public void SetParameterStepSize(string parameter, double stepSize) {
     318      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
     319      if (i < ControllableParameterBounds.Rows) {
     320        ControllableParameterBounds[i, 2] = stepSize;
     321        OnParametersChanged(this, EventArgs.Empty);
     322        return;
     323      }
     324      throw new ArgumentException(string.Format("SetParameterStepSize: Invalid parameter name {0}", parameter));
     325    }
     326
     327    public bool GetParameterActive(string parameter) {
     328      var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
     329      if (item == null)
     330        throw new ArgumentException(string.Format("GetParameterActive: Invalid target name {0}", parameter));
     331      return ControllableParameters.ItemChecked(item);
     332    }
     333
     334    public void SetParameterActive(string parameter, bool active) {
     335      var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
     336      if (item == null)
     337        throw new ArgumentException(string.Format("SetParameterActive: Invalid target name {0}", parameter));
     338      ControllableParameters.SetItemCheckedState(item, active);
     339      OnParametersChanged(this, EventArgs.Empty);
     340    }
     341
    378342    public void SetControllableParameters(IEnumerable<string> parameterNames) {
    379343      ControllableParameters = new CheckedItemList<StringValue>();
     
    410374    }
    411375    #endregion // process parameters
    412     #endregion // IProcessParameterOprimizationProblem methods
     376    #endregion // IGoalSeekingProblem methods
    413377
    414378    #region data members
     
    431395    private MultiObjectiveGoalSeekingProblem(bool deserializing) : base(deserializing) { }
    432396
    433     private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner)
    434       : base(original, cloner) {
     397    private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) {
    435398      this.dataset = cloner.Clone(original.dataset);
    436399      this.problemData = cloner.Clone(original.problemData);
     
    512475        ++i;
    513476      }
    514       i = 0;
    515       var qualities = new double[ActiveTargets.Count()];
    516477      var estimatedValues = GetEstimatedGoalValues(vector, round: true);
    517       // round target estimated values and calculate qualities
    518       foreach (var target in ActiveTargets) {
    519         var estimatedValue = estimatedValues[i];
    520         var goal = GetTargetGoal(target);
    521         var weight = GetTargetWeight(target);
    522         var variance = GetTargetVariance(target);
    523         qualities[i] = weight * Math.Pow(estimatedValue - goal, 2) / variance;
    524         ++i;
    525       }
    526       return qualities;
     478      var qualities = TargetList.CheckedItems.Zip(estimatedValues, (t, v) => new { Name = t.Value.Value, Index = t.Index, EstimatedValue = v })
     479                                .Select(target => {
     480                                  var goal = TargetGoals[target.Index, 0];
     481                                  var weight = TargetGoals[target.Index, 1];
     482                                  var variance = TargetGoals[target.Index, 2];
     483                                  return weight * Math.Pow(target.EstimatedValue - goal, 2) / variance;
     484                                });
     485      return qualities.ToArray();
    527486    }
    528487
     
    561520          continue;
    562521        var vector = individuals[i].RealVector();
    563         var estimatedValues = GetEstimatedGoalValues(vector);
     522        var estimatedValues = GetEstimatedGoalValues(vector).ToList();
    564523        var rowValues = new double[columnNames.Count];
    565524        rowValues[0] = qualitySum;
     
    658617    private void UpdateControllableParameters() {
    659618      if (ProblemData == null) return;
    660       SetControllableParameters(ProblemData.Dataset.DoubleVariables);
     619       if (ProblemData == null) return;
     620      var variablesUsedForPrediction = ModelCollection.Any()
     621        ? ModelCollection.SelectMany(x => x.VariablesUsedForPrediction).Distinct()
     622        : ProblemData.Dataset.DoubleVariables;
     623      SetControllableParameters(variablesUsedForPrediction);
    661624    }
    662625
     
    715678      }
    716679    }
    717     #endregion
    718 
    719     #region util
     680
     681    private bool IsValidTarget(string target) {
     682      return TargetList.Any(x => x.Value == target);
     683    }
    720684    private static double RoundToNearestStepMultiple(double value, double step) {
    721685      return step * (long)Math.Round(value / step);
    722686    }
    723 
     687    private IEnumerable<IRegressionModel> GetModels(string target) {
     688      return ModelCollection.Where(x => x.TargetVariable == target);
     689    }
    724690    private class DoubleEqualityComparer : IEqualityComparer<double> {
    725691      public bool Equals(double x, double y) { return x.IsAlmost(y); }
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Plugin.cs

    r14321 r14324  
    2222using HeuristicLab.PluginInfrastructure;
    2323
    24 namespace HeuristicLab.ProcessParameterOptimization {
    25   [Plugin("HeuristicLab.ProcessParameterOptimization", "3.3.0.0")]
    26   [PluginFile("HeuristicLab.ProcessParameterOptimization-3.3.dll", PluginFileType.Assembly)]
     24namespace HeuristicLab.GoalSeekingProblem {
     25  [Plugin("HeuristicLab.GoalSeekingProblem", "3.4.0.0")]
     26  [PluginFile("HeuristicLab.GoalSeekingProblem-3.4.dll", PluginFileType.Assembly)]
    2727  [PluginDependency("HeuristicLab.Collections", "3.3")]
    2828  [PluginDependency("HeuristicLab.Common", "3.3")]
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Properties/AssemblyInfo.cs

    r14321 r14324  
    5454// [assembly: AssemblyVersion("1.0.*")]
    5555[assembly: AssemblyVersion("3.3.0.0")]
    56 [assembly: AssemblyFileVersion("3.3.11.480")]
     56[assembly: AssemblyFileVersion("3.3.11.14321")]
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/SingleObjectiveGoalSeekingProblem.cs

    r14321 r14324  
    3434using HeuristicLab.Problems.DataAnalysis;
    3535
    36 namespace HeuristicLab.ProcessParameterOptimization {
    37   [Item("Process parameter optimization problem (single-objective)", "Represents a single objective optimization problem which uses configurable regression solutions to evaluate targets from a given dataset.")]
     36namespace HeuristicLab.GoalSeeking {
     37  [Item("Goal seeking problem (single-objective)", "Represents a single objective optimization problem which uses configurable regression solutions to evaluate targets from a given dataset.")]
    3838  [Creatable("Problems")]
    3939  [StorableClass]
    4040  public sealed class SingleObjectiveGoalSeekingProblem : SingleObjectiveBasicProblem<RealVectorEncoding>, IGoalSeekingProblem {
     41    #region parameter names
    4142    private const string ModifiableDatasetParameterName = "Dataset";
    4243    private const string ProblemDataParameterName = "ProblemData";
     
    5051    private const string AllowedRangesParameterName = "AllowedRanges";
    5152    private const string QualitySumCutoffParameterName = "QualitySumCutoff";
     53    #endregion
    5254
    5355    #region parameters
     
    6163      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
    6264    }
    63     public IValueParameter<IDataset> ModifiableDatasetParameter {
    64       get { return (IValueParameter<IDataset>)Parameters[ModifiableDatasetParameterName]; }
    65     }
    6665    public IValueParameter<CheckedItemList<StringValue>> ControllableParametersParameter {
    6766      get { return (IValueParameter<CheckedItemList<StringValue>>)Parameters[ControllableParametersParameterName]; }
     
    9190      get { return TargetGoalsParameter.Value; }
    9291      set { TargetGoalsParameter.Value = value; }
    93     }
    94     // the AllowedRanges property is only used by the pareto folding analyzer
    95     public RealVector AllowedRanges {
    96       get { return AllowedRangesParameter.Value; }
    97       set { AllowedRangesParameter.Value = value; }
    98     }
    99     public double QualitySumCutoff {
    100       get { return QualitySumCutoffParameter.Value.Value; }
    101       set { QualitySumCutoffParameter.Value.Value = value; }
    10292    }
    10393    #endregion
     
    138128
    139129    #region targets
    140     public IEnumerable<string> ActiveTargets {
    141       get { return TargetList.CheckedItems.Select(x => x.Value.Value); }
    142     }
    143 
    144     public IEnumerable<string> Targets {
    145       get { return TargetList.Select(x => x.Value); }
    146     }
    147 
    148130    public ICheckedItemList<StringValue> TargetList {
    149131      get { return TargetsParameter.Value; }
    150132      set { TargetsParameter.Value = (CheckedItemList<StringValue>)value; }
    151133    }
     134    private IEnumerable<string> ActiveTargets {
     135      get { return TargetList.CheckedItems.Select(x => x.Value.Value); }
     136    }
    152137    #endregion
    153138
    154139    #region parameters
    155     public IEnumerable<string> ActiveParameters {
     140    private IEnumerable<string> ActiveParameters {
    156141      get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); }
    157142    }
    158 
    159143    public ICheckedItemList<StringValue> ControllableParameters {
    160144      get { return ControllableParametersParameter.Value; }
    161145      set { ControllableParametersParameter.Value = (CheckedItemList<StringValue>)value; }
    162146    }
    163 
    164147    public DoubleMatrix ControllableParameterBounds {
    165148      get { return ControllableParameterBoundsParameter.Value; }
     
    171154    #region IProcessParameterOptimizationProblem methods
    172155    #region solutions
    173     public double GetVariableValue(string variableName) {
    174       return dataset.GetDoubleValue(variableName, 0);
    175     }
    176 
    177     public void SetVariableValue(string variableName, double variableValue) {
    178       dataset.SetVariableValue(variableValue, variableName, 0); // will throw if the variable is not present in the dataset
    179     }
    180 
    181     public double[] GetEstimatedGoalValues(IEnumerable<double> parameters, bool round = false) {
     156    public IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false) {
    182157      var ds = (ModifiableDataset)dataset.Clone();
    183       var estimatedValues = new double[ActiveTargets.Count()];
    184       var e1 = parameters.GetEnumerator();
    185       var e2 = ActiveParameters.GetEnumerator();
    186       while (e1.MoveNext() && e2.MoveNext())
    187         ds.SetVariableValue(e1.Current, e2.Current, 0);
     158      foreach (var parameter in ActiveParameters.Zip(parameterValues, (p, v) => new { Name = p, Value = v })) {
     159        ds.SetVariableValue(parameter.Value, parameter.Name, 0);
     160      }
    188161      var rows = new[] { 0 }; // actually just one row
    189       int i = 0;
    190       var models = ModelCollection.ToList();
    191       foreach (var target in TargetList.CheckedItems) {
    192         var average = 0d;
    193         var count = 0;
    194         var targetName = target.Value.Value;
    195 
    196         foreach (var model in models) {
    197           if (targetName != model.TargetVariable)
    198             continue;
    199           average += model.GetEstimatedValues(ds, rows).Single();
    200           count++;
    201         }
    202         var estimatedValue = average / count;
    203         if (round) {
    204           var accuracy = GetTargetStepSize(targetName);
    205           estimatedValues[i] = RoundToNearestStepMultiple(estimatedValue, accuracy);
    206         } else {
    207           estimatedValues[i] = estimatedValue;
    208         }
    209         ++i;
    210       }
     162
     163      var estimatedValues =
     164        round ? ActiveTargets.Select(t => RoundToNearestStepMultiple(GetModels(t).Average(m => m.GetEstimatedValues(ds, rows).Single()), GetTargetStepSize(t)))
     165              : ActiveTargets.Select(t => GetModels(t).Average(m => m.GetEstimatedValues(ds, rows).Single()));
     166
    211167      return estimatedValues;
    212168    }
     
    255211
    256212    public double GetTargetGoal(string target) {
    257       if (!Targets.Contains(target))
     213      if (!IsValidTarget(target))
    258214        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    259215      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    262218
    263219    public void SetTargetGoal(string target, double goal) {
    264       if (!Targets.Contains(target))
     220      if (!IsValidTarget(target))
    265221        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    266222      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    270226
    271227    public double GetTargetWeight(string target) {
    272       if (!Targets.Contains(target))
     228      if (!IsValidTarget(target))
    273229        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    274230      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    277233
    278234    public void SetTargetWeight(string target, double weight) {
    279       if (!Targets.Contains(target))
     235      if (!IsValidTarget(target))
    280236        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    281237      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    285241
    286242    public double GetTargetVariance(string target) {
    287       if (!Targets.Contains(target))
     243      if (!IsValidTarget(target))
    288244        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    289245      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    292248
    293249    public void SetTargetVariance(string target, double variance) {
    294       if (!Targets.Contains(target))
     250      if (!IsValidTarget(target))
    295251        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    296252      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    300256
    301257    public double GetTargetStepSize(string target) {
    302       if (!Targets.Contains(target))
     258      if (!IsValidTarget(target))
    303259        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    304260      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    307263
    308264    public void SetTargetStepSize(string target, double stepSize) {
    309       if (!Targets.Contains(target))
     265      if (!IsValidTarget(target))
    310266        throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target));
    311267      int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count();
     
    323279
    324280    #region process parameters
    325     public void SetParameterBounds(string parameterName, double min, double max, double step) {
    326       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameterName).Count();
    327       if (i < ControllableParameterBounds.Rows) {
    328         ControllableParameterBounds[i, 0] = min;
    329         ControllableParameterBounds[i, 1] = max;
    330         ControllableParameterBounds[i, 2] = step;
    331         UpdateEncoding();
    332         OnParametersChanged(this, EventArgs.Empty);
    333       } else {
    334         throw new ArgumentException(string.Format("SetParameterBounds: Invalid parameter name {0}", parameterName));
    335       }
    336     }
    337 
    338     public double GetParameterStepSize(string parameter) {
    339       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
    340       if (i < ControllableParameterBounds.Rows)
    341         return ControllableParameterBounds[i, 2];
    342       throw new ArgumentException(string.Format("GetParameterStepSize: Invalid parameter name {0}", parameter));
    343     }
    344 
    345     public void SetParameterStepSize(string parameter, double stepSize) {
    346       int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
    347       if (i < ControllableParameterBounds.Rows) {
    348         ControllableParameterBounds[i, 2] = stepSize;
    349         OnParametersChanged(this, EventArgs.Empty);
    350       } else {
    351         throw new ArgumentException(string.Format("SetParameterStepSize: Invalid parameter name {0}", parameter));
    352       }
    353     }
    354 
    355     public bool GetParameterActive(string parameter) {
    356       var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
    357       if (item == null)
    358         throw new ArgumentException(string.Format("GetParameterActive: Invalid target name {0}", parameter));
    359       return ControllableParameters.ItemChecked(item);
    360     }
    361 
    362     public void SetParameterActive(string parameter, bool active) {
    363       var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
    364       if (item == null)
    365         throw new ArgumentException(string.Format("SetParameterActive: Invalid target name {0}", parameter));
    366       ControllableParameters.SetItemCheckedState(item, active);
    367       OnParametersChanged(this, EventArgs.Empty);
    368     }
    369 
    370281    /// <summary>
    371282    /// Returns the parameter bounds (min and max) and the step size for the specified parameter
     
    384295    }
    385296
     297    public void SetParameterBounds(string parameterName, double min, double max, double step) {
     298      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameterName).Count();
     299      if (i < ControllableParameterBounds.Rows) {
     300        ControllableParameterBounds[i, 0] = min;
     301        ControllableParameterBounds[i, 1] = max;
     302        ControllableParameterBounds[i, 2] = step;
     303        UpdateEncoding();
     304        OnParametersChanged(this, EventArgs.Empty);
     305      } else {
     306        throw new ArgumentException(string.Format("SetParameterBounds: Invalid parameter name {0}", parameterName));
     307      }
     308    }
     309
     310    public double GetParameterStepSize(string parameter) {
     311      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
     312      if (i < ControllableParameterBounds.Rows)
     313        return ControllableParameterBounds[i, 2];
     314      throw new ArgumentException(string.Format("GetParameterStepSize: Invalid parameter name {0}", parameter));
     315    }
     316
     317    public void SetParameterStepSize(string parameter, double stepSize) {
     318      int i = ControllableParameterBounds.RowNames.TakeWhile(x => x != parameter).Count();
     319      if (i < ControllableParameterBounds.Rows) {
     320        ControllableParameterBounds[i, 2] = stepSize;
     321        OnParametersChanged(this, EventArgs.Empty);
     322        return;
     323      }
     324      throw new ArgumentException(string.Format("SetParameterStepSize: Invalid parameter name {0}", parameter));
     325    }
     326
     327    public bool GetParameterActive(string parameter) {
     328      var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
     329      if (item == null)
     330        throw new ArgumentException(string.Format("GetParameterActive: Invalid target name {0}", parameter));
     331      return ControllableParameters.ItemChecked(item);
     332    }
     333
     334    public void SetParameterActive(string parameter, bool active) {
     335      var item = ControllableParameters.SingleOrDefault(x => x.Value == parameter);
     336      if (item == null)
     337        throw new ArgumentException(string.Format("SetParameterActive: Invalid target name {0}", parameter));
     338      ControllableParameters.SetItemCheckedState(item, active);
     339      OnParametersChanged(this, EventArgs.Empty);
     340    }
     341
    386342    public void SetControllableParameters(IEnumerable<string> parameterNames) {
    387343      ControllableParameters = new CheckedItemList<StringValue>();
     
    418374    }
    419375    #endregion // process parameters
    420     #endregion // IProcessParameterOprimizationProblem methods
     376    #endregion // IGoalSeekingProblem methods
    421377
    422378    #region data members
     
    425381
    426382    public override bool Maximization {
    427       get { return true; }
     383      get { return false; }
    428384    }
    429385    #endregion
     
    433389    private SingleObjectiveGoalSeekingProblem(bool deserializing) : base(deserializing) { }
    434390
    435     private SingleObjectiveGoalSeekingProblem(SingleObjectiveGoalSeekingProblem original, Cloner cloner)
    436       : base(original, cloner) {
     391    private SingleObjectiveGoalSeekingProblem(SingleObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) {
    437392      this.dataset = cloner.Clone(original.dataset);
    438393      this.problemData = cloner.Clone(original.problemData);
     
    487442      var vector = individual.RealVector();
    488443      vector.ElementNames = ActiveParameters;
    489 
    490444      int i = 0;
    491445      // round vector according to parameter step sizes
     
    495449        ++i;
    496450      }
    497       i = 0;
    498       var quality = 0d;
    499451      var estimatedValues = GetEstimatedGoalValues(vector, round: true);
    500       // round target estimated values and calculate qualities
    501       foreach (var target in ActiveTargets) {
    502         var estimatedValue = estimatedValues[i];
    503         var goal = GetTargetGoal(target);
    504         var weight = GetTargetWeight(target);
    505         var variance = GetTargetVariance(target);
    506         quality += weight * Math.Pow(estimatedValue - goal, 2) / variance;
    507         ++i;
    508       }
    509       return quality / i;
     452      var quality = TargetList.CheckedItems.Zip(estimatedValues, (t, v) => new { Name = t.Value.Value, Index = t.Index, EstimatedValue = v })
     453                                .Average(target => {
     454                                  var goal = TargetGoals[target.Index, 0];
     455                                  var weight = TargetGoals[target.Index, 1];
     456                                  var variance = TargetGoals[target.Index, 2];
     457                                  return weight * Math.Pow(target.EstimatedValue - goal, 2) / variance;
     458                                });
     459      return quality;
    510460    }
    511461
     
    576526    private void UpdateControllableParameters() {
    577527      if (ProblemData == null) return;
    578       SetControllableParameters(ProblemData.Dataset.DoubleVariables);
     528      var variablesUsedForPrediction = ModelCollection.Any()
     529        ? ModelCollection.SelectMany(x => x.VariablesUsedForPrediction).Distinct()
     530        : ProblemData.Dataset.DoubleVariables;
     531      SetControllableParameters(variablesUsedForPrediction);
    579532    }
    580533
     
    628581      }
    629582    }
    630     #endregion
    631 
    632     #region util
     583    private bool IsValidTarget(string target) {
     584      return TargetList.Any(x => x.Value == target);
     585    }
    633586    private static double RoundToNearestStepMultiple(double value, double step) {
    634587      return step * (long)Math.Round(value / step);
    635588    }
     589    private IEnumerable<IRegressionModel> GetModels(string target) {
     590      return ModelCollection.Where(x => x.TargetVariable == target);
     591    }
    636592    #endregion
    637593  }
Note: See TracChangeset for help on using the changeset viewer.