Changeset 14324
- Timestamp:
- 10/05/16 15:34:15 (8 years ago)
- 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 32 32 using HeuristicLab.Problems.DataAnalysis; 33 33 34 namespace HeuristicLab. ProcessParameterOptimization{34 namespace HeuristicLab.GoalSeeking { 35 35 [StorableClass] 36 36 [Item("BestSolutionAnalyzer", "An analyzer which identifies the best solution from the SingleObjectiveProcessParameterOptimizationProblem")] -
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/GoalSeekingProblem.csproj
r14321 r14324 9 9 <OutputType>Library</OutputType> 10 10 <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> 13 13 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 14 14 <FileAlignment>512</FileAlignment> -
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/IGoalSeekingProblem.cs
r14321 r14324 27 27 using HeuristicLab.Problems.DataAnalysis; 28 28 29 namespace HeuristicLab. ProcessParameterOptimization{29 namespace HeuristicLab.GoalSeeking { 30 30 public interface IGoalSeekingProblem : IProblem { 31 31 IRegressionProblemData ProblemData { get; set; } 32 double GetVariableValue(string variableName);33 void SetVariableValue(string variableName, double variableValue);34 32 35 33 //rounding ? 36 double[] GetEstimatedGoalValues(IEnumerable<double> parameters, bool round = false);34 IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false); 37 35 int Row { get; set; } 38 36 … … 44 42 45 43 #region targets 46 IEnumerable<string> ActiveTargets { get; }47 IEnumerable<string> Targets { get; }48 ICheckedItemList<StringValue> TargetList { get; }49 44 double GetTargetGoal(string target); 50 45 void SetTargetGoal(string target, double goal); … … 64 59 65 60 #region parameters 66 IEnumerable<string> ActiveParameters { get; }67 61 ICheckedItemList<StringValue> ControllableParameters { get; } 68 62 DoubleMatrix ControllableParameterBounds { get; } -
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/MultiObjectiveGoalSeekingProblem.cs
r14321 r14324 34 34 using HeuristicLab.Problems.DataAnalysis; 35 35 36 namespace HeuristicLab. ProcessParameterOptimization{37 [Item(" Process parameter optimizationproblem (multi-objective)", "Represents a single objective optimization problem which uses configurable regression models to evaluate targets from a given dataset.")]36 namespace 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.")] 38 38 [Creatable("Problems")] 39 39 [StorableClass] … … 56 56 public IValueParameter<IRegressionProblemData> ProblemDataParameter { 57 57 get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } 58 }59 public IValueParameter<IDataset> ModifiableDatasetParameter {60 get { return (IValueParameter<IDataset>)Parameters[ModifiableDatasetParameterName]; }61 58 } 62 59 public IValueParameter<CheckedItemList<StringValue>> ControllableParametersParameter { … … 129 126 130 127 #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 139 128 public ICheckedItemList<StringValue> TargetList { 140 129 get { return TargetsParameter.Value; } 141 130 set { TargetsParameter.Value = (CheckedItemList<StringValue>)value; } 142 131 } 132 // convenience method 133 private IEnumerable<string> ActiveTargets { 134 get { return TargetList.CheckedItems.Select(x => x.Value.Value); } 135 } 143 136 #endregion 144 137 145 138 #region parameters 146 public IEnumerable<string> ActiveParameters {147 get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); }148 }149 150 139 public ICheckedItemList<StringValue> ControllableParameters { 151 140 get { return ControllableParametersParameter.Value; } 152 141 set { ControllableParametersParameter.Value = (CheckedItemList<StringValue>)value; } 153 142 } 154 143 // convenience method 144 private IEnumerable<string> ActiveParameters { 145 get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); } 146 } 155 147 public DoubleMatrix ControllableParameterBounds { 156 148 get { return ControllableParameterBoundsParameter.Value; } … … 162 154 #region IProcessParameterOptimizationProblem methods 163 155 #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) { 173 157 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 } 179 161 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())); 201 166 return estimatedValues; 202 167 } … … 245 210 246 211 public double GetTargetGoal(string target) { 247 if (! Targets.Contains(target))212 if (!IsValidTarget(target)) 248 213 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 249 214 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 252 217 253 218 public void SetTargetGoal(string target, double goal) { 254 if (! Targets.Contains(target))219 if (!IsValidTarget(target)) 255 220 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 256 221 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 260 225 261 226 public double GetTargetWeight(string target) { 262 if (! Targets.Contains(target))227 if (!IsValidTarget(target)) 263 228 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 264 229 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 267 232 268 233 public void SetTargetWeight(string target, double weight) { 269 if (! Targets.Contains(target))234 if (!IsValidTarget(target)) 270 235 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 271 236 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 275 240 276 241 public double GetTargetVariance(string target) { 277 if (! Targets.Contains(target))242 if (!IsValidTarget(target)) 278 243 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 279 244 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 282 247 283 248 public void SetTargetVariance(string target, double variance) { 284 if (! Targets.Contains(target))249 if (!IsValidTarget(target)) 285 250 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 286 251 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 290 255 291 256 public double GetTargetStepSize(string target) { 292 if (! Targets.Contains(target))257 if (!IsValidTarget(target)) 293 258 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 294 259 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 297 262 298 263 public void SetTargetStepSize(string target, double stepSize) { 299 if (! Targets.Contains(target))264 if (!IsValidTarget(target)) 300 265 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 301 266 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 313 278 314 279 #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 362 280 /// <summary> 363 281 /// Returns the parameter bounds (min and max) and the step size for the specified parameter … … 376 294 } 377 295 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 378 342 public void SetControllableParameters(IEnumerable<string> parameterNames) { 379 343 ControllableParameters = new CheckedItemList<StringValue>(); … … 410 374 } 411 375 #endregion // process parameters 412 #endregion // I ProcessParameterOprimizationProblem methods376 #endregion // IGoalSeekingProblem methods 413 377 414 378 #region data members … … 431 395 private MultiObjectiveGoalSeekingProblem(bool deserializing) : base(deserializing) { } 432 396 433 private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner) 434 : base(original, cloner) { 397 private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) { 435 398 this.dataset = cloner.Clone(original.dataset); 436 399 this.problemData = cloner.Clone(original.problemData); … … 512 475 ++i; 513 476 } 514 i = 0;515 var qualities = new double[ActiveTargets.Count()];516 477 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(); 527 486 } 528 487 … … 561 520 continue; 562 521 var vector = individuals[i].RealVector(); 563 var estimatedValues = GetEstimatedGoalValues(vector) ;522 var estimatedValues = GetEstimatedGoalValues(vector).ToList(); 564 523 var rowValues = new double[columnNames.Count]; 565 524 rowValues[0] = qualitySum; … … 658 617 private void UpdateControllableParameters() { 659 618 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); 661 624 } 662 625 … … 715 678 } 716 679 } 717 #endregion 718 719 #region util 680 681 private bool IsValidTarget(string target) { 682 return TargetList.Any(x => x.Value == target); 683 } 720 684 private static double RoundToNearestStepMultiple(double value, double step) { 721 685 return step * (long)Math.Round(value / step); 722 686 } 723 687 private IEnumerable<IRegressionModel> GetModels(string target) { 688 return ModelCollection.Where(x => x.TargetVariable == target); 689 } 724 690 private class DoubleEqualityComparer : IEqualityComparer<double> { 725 691 public bool Equals(double x, double y) { return x.IsAlmost(y); } -
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Plugin.cs
r14321 r14324 22 22 using HeuristicLab.PluginInfrastructure; 23 23 24 namespace HeuristicLab. ProcessParameterOptimization{25 [Plugin("HeuristicLab. ProcessParameterOptimization", "3.3.0.0")]26 [PluginFile("HeuristicLab. ProcessParameterOptimization-3.3.dll", PluginFileType.Assembly)]24 namespace HeuristicLab.GoalSeekingProblem { 25 [Plugin("HeuristicLab.GoalSeekingProblem", "3.4.0.0")] 26 [PluginFile("HeuristicLab.GoalSeekingProblem-3.4.dll", PluginFileType.Assembly)] 27 27 [PluginDependency("HeuristicLab.Collections", "3.3")] 28 28 [PluginDependency("HeuristicLab.Common", "3.3")] -
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Properties/AssemblyInfo.cs
r14321 r14324 54 54 // [assembly: AssemblyVersion("1.0.*")] 55 55 [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 34 34 using HeuristicLab.Problems.DataAnalysis; 35 35 36 namespace HeuristicLab. ProcessParameterOptimization{37 [Item(" Process parameter optimizationproblem (single-objective)", "Represents a single objective optimization problem which uses configurable regression solutions to evaluate targets from a given dataset.")]36 namespace 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.")] 38 38 [Creatable("Problems")] 39 39 [StorableClass] 40 40 public sealed class SingleObjectiveGoalSeekingProblem : SingleObjectiveBasicProblem<RealVectorEncoding>, IGoalSeekingProblem { 41 #region parameter names 41 42 private const string ModifiableDatasetParameterName = "Dataset"; 42 43 private const string ProblemDataParameterName = "ProblemData"; … … 50 51 private const string AllowedRangesParameterName = "AllowedRanges"; 51 52 private const string QualitySumCutoffParameterName = "QualitySumCutoff"; 53 #endregion 52 54 53 55 #region parameters … … 61 63 get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } 62 64 } 63 public IValueParameter<IDataset> ModifiableDatasetParameter {64 get { return (IValueParameter<IDataset>)Parameters[ModifiableDatasetParameterName]; }65 }66 65 public IValueParameter<CheckedItemList<StringValue>> ControllableParametersParameter { 67 66 get { return (IValueParameter<CheckedItemList<StringValue>>)Parameters[ControllableParametersParameterName]; } … … 91 90 get { return TargetGoalsParameter.Value; } 92 91 set { TargetGoalsParameter.Value = value; } 93 }94 // the AllowedRanges property is only used by the pareto folding analyzer95 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; }102 92 } 103 93 #endregion … … 138 128 139 129 #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 148 130 public ICheckedItemList<StringValue> TargetList { 149 131 get { return TargetsParameter.Value; } 150 132 set { TargetsParameter.Value = (CheckedItemList<StringValue>)value; } 151 133 } 134 private IEnumerable<string> ActiveTargets { 135 get { return TargetList.CheckedItems.Select(x => x.Value.Value); } 136 } 152 137 #endregion 153 138 154 139 #region parameters 155 p ublicIEnumerable<string> ActiveParameters {140 private IEnumerable<string> ActiveParameters { 156 141 get { return ControllableParameters.CheckedItems.Select(x => x.Value.Value); } 157 142 } 158 159 143 public ICheckedItemList<StringValue> ControllableParameters { 160 144 get { return ControllableParametersParameter.Value; } 161 145 set { ControllableParametersParameter.Value = (CheckedItemList<StringValue>)value; } 162 146 } 163 164 147 public DoubleMatrix ControllableParameterBounds { 165 148 get { return ControllableParameterBoundsParameter.Value; } … … 171 154 #region IProcessParameterOptimizationProblem methods 172 155 #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) { 182 157 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 } 188 161 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 211 167 return estimatedValues; 212 168 } … … 255 211 256 212 public double GetTargetGoal(string target) { 257 if (! Targets.Contains(target))213 if (!IsValidTarget(target)) 258 214 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 259 215 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 262 218 263 219 public void SetTargetGoal(string target, double goal) { 264 if (! Targets.Contains(target))220 if (!IsValidTarget(target)) 265 221 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 266 222 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 270 226 271 227 public double GetTargetWeight(string target) { 272 if (! Targets.Contains(target))228 if (!IsValidTarget(target)) 273 229 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 274 230 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 277 233 278 234 public void SetTargetWeight(string target, double weight) { 279 if (! Targets.Contains(target))235 if (!IsValidTarget(target)) 280 236 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 281 237 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 285 241 286 242 public double GetTargetVariance(string target) { 287 if (! Targets.Contains(target))243 if (!IsValidTarget(target)) 288 244 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 289 245 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 292 248 293 249 public void SetTargetVariance(string target, double variance) { 294 if (! Targets.Contains(target))250 if (!IsValidTarget(target)) 295 251 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 296 252 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 300 256 301 257 public double GetTargetStepSize(string target) { 302 if (! Targets.Contains(target))258 if (!IsValidTarget(target)) 303 259 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 304 260 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 307 263 308 264 public void SetTargetStepSize(string target, double stepSize) { 309 if (! Targets.Contains(target))265 if (!IsValidTarget(target)) 310 266 throw new ArgumentException(string.Format("The target variable name \"{0}\" does not exist in the dataset.", target)); 311 267 int i = TargetGoals.RowNames.TakeWhile(x => x != target).Count(); … … 323 279 324 280 #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 370 281 /// <summary> 371 282 /// Returns the parameter bounds (min and max) and the step size for the specified parameter … … 384 295 } 385 296 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 386 342 public void SetControllableParameters(IEnumerable<string> parameterNames) { 387 343 ControllableParameters = new CheckedItemList<StringValue>(); … … 418 374 } 419 375 #endregion // process parameters 420 #endregion // I ProcessParameterOprimizationProblem methods376 #endregion // IGoalSeekingProblem methods 421 377 422 378 #region data members … … 425 381 426 382 public override bool Maximization { 427 get { return true; }383 get { return false; } 428 384 } 429 385 #endregion … … 433 389 private SingleObjectiveGoalSeekingProblem(bool deserializing) : base(deserializing) { } 434 390 435 private SingleObjectiveGoalSeekingProblem(SingleObjectiveGoalSeekingProblem original, Cloner cloner) 436 : base(original, cloner) { 391 private SingleObjectiveGoalSeekingProblem(SingleObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) { 437 392 this.dataset = cloner.Clone(original.dataset); 438 393 this.problemData = cloner.Clone(original.problemData); … … 487 442 var vector = individual.RealVector(); 488 443 vector.ElementNames = ActiveParameters; 489 490 444 int i = 0; 491 445 // round vector according to parameter step sizes … … 495 449 ++i; 496 450 } 497 i = 0;498 var quality = 0d;499 451 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; 510 460 } 511 461 … … 576 526 private void UpdateControllableParameters() { 577 527 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); 579 532 } 580 533 … … 628 581 } 629 582 } 630 #endregion631 632 #region util583 private bool IsValidTarget(string target) { 584 return TargetList.Any(x => x.Value == target); 585 } 633 586 private static double RoundToNearestStepMultiple(double value, double step) { 634 587 return step * (long)Math.Round(value / step); 635 588 } 589 private IEnumerable<IRegressionModel> GetModels(string target) { 590 return ModelCollection.Where(x => x.TargetVariable == target); 591 } 636 592 #endregion 637 593 }
Note: See TracChangeset
for help on using the changeset viewer.