[13645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13645] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Threading;
|
---|
[13658] | 25 | using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies;
|
---|
[13645] | 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
|
---|
| 36 | [Item("MCTS Symbolic Regression", "Monte carlo tree search for symbolic regression. Useful mainly as a base learner in gradient boosting.")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 250)]
|
---|
| 39 | public class MctsSymbolicRegressionAlgorithm : BasicAlgorithm {
|
---|
[14558] | 40 | public override Type ProblemType
|
---|
| 41 | {
|
---|
[13645] | 42 | get { return typeof(IRegressionProblem); }
|
---|
| 43 | }
|
---|
[14558] | 44 | public new IRegressionProblem Problem
|
---|
| 45 | {
|
---|
[13645] | 46 | get { return (IRegressionProblem)base.Problem; }
|
---|
| 47 | set { base.Problem = value; }
|
---|
| 48 | }
|
---|
[14558] | 49 | public override bool SupportsPause
|
---|
| 50 | {
|
---|
| 51 | get { return false; }
|
---|
| 52 | }
|
---|
[13645] | 53 |
|
---|
| 54 | #region ParameterNames
|
---|
| 55 | private const string IterationsParameterName = "Iterations";
|
---|
| 56 | private const string MaxVariablesParameterName = "Maximum variables";
|
---|
| 57 | private const string ScaleVariablesParameterName = "Scale variables";
|
---|
| 58 | private const string AllowedFactorsParameterName = "Allowed factors";
|
---|
| 59 | private const string ConstantOptimizationIterationsParameterName = "Iterations (constant optimization)";
|
---|
[13658] | 60 | private const string PolicyParameterName = "Policy";
|
---|
[13645] | 61 | private const string SeedParameterName = "Seed";
|
---|
| 62 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
| 63 | private const string UpdateIntervalParameterName = "UpdateInterval";
|
---|
| 64 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
| 65 | private const string PunishmentFactorParameterName = "PunishmentFactor";
|
---|
| 66 |
|
---|
| 67 | private const string VariableProductFactorName = "product(xi)";
|
---|
| 68 | private const string ExpFactorName = "exp(c * product(xi))";
|
---|
| 69 | private const string LogFactorName = "log(c + sum(c*product(xi))";
|
---|
| 70 | private const string InvFactorName = "1 / (1 + sum(c*product(xi))";
|
---|
| 71 | private const string FactorSumsName = "sum of multiple terms";
|
---|
| 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region ParameterProperties
|
---|
[14558] | 75 | public IFixedValueParameter<IntValue> IterationsParameter
|
---|
| 76 | {
|
---|
[13645] | 77 | get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
| 78 | }
|
---|
[14558] | 79 | public IFixedValueParameter<IntValue> MaxVariableReferencesParameter
|
---|
| 80 | {
|
---|
[13645] | 81 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxVariablesParameterName]; }
|
---|
| 82 | }
|
---|
[14558] | 83 | public IFixedValueParameter<BoolValue> ScaleVariablesParameter
|
---|
| 84 | {
|
---|
[13645] | 85 | get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleVariablesParameterName]; }
|
---|
| 86 | }
|
---|
[14558] | 87 | public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter
|
---|
| 88 | {
|
---|
[13645] | 89 | get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
|
---|
| 90 | }
|
---|
[14558] | 91 | public IValueParameter<IPolicy> PolicyParameter
|
---|
| 92 | {
|
---|
[13658] | 93 | get { return (IValueParameter<IPolicy>)Parameters[PolicyParameterName]; }
|
---|
[13645] | 94 | }
|
---|
[14558] | 95 | public IFixedValueParameter<DoubleValue> PunishmentFactorParameter
|
---|
| 96 | {
|
---|
[13645] | 97 | get { return (IFixedValueParameter<DoubleValue>)Parameters[PunishmentFactorParameterName]; }
|
---|
| 98 | }
|
---|
[14558] | 99 | public IValueParameter<ICheckedItemList<StringValue>> AllowedFactorsParameter
|
---|
| 100 | {
|
---|
[13645] | 101 | get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters[AllowedFactorsParameterName]; }
|
---|
| 102 | }
|
---|
[14558] | 103 | public IFixedValueParameter<IntValue> SeedParameter
|
---|
| 104 | {
|
---|
[13645] | 105 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
| 106 | }
|
---|
[14558] | 107 | public FixedValueParameter<BoolValue> SetSeedRandomlyParameter
|
---|
| 108 | {
|
---|
[13645] | 109 | get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
| 110 | }
|
---|
[14558] | 111 | public IFixedValueParameter<IntValue> UpdateIntervalParameter
|
---|
| 112 | {
|
---|
[13645] | 113 | get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
| 114 | }
|
---|
[14558] | 115 | public IFixedValueParameter<BoolValue> CreateSolutionParameter
|
---|
| 116 | {
|
---|
[13645] | 117 | get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 118 | }
|
---|
| 119 | #endregion
|
---|
| 120 |
|
---|
| 121 | #region Properties
|
---|
[14558] | 122 | public int Iterations
|
---|
| 123 | {
|
---|
[13645] | 124 | get { return IterationsParameter.Value.Value; }
|
---|
| 125 | set { IterationsParameter.Value.Value = value; }
|
---|
| 126 | }
|
---|
[14558] | 127 | public int Seed
|
---|
| 128 | {
|
---|
[13645] | 129 | get { return SeedParameter.Value.Value; }
|
---|
| 130 | set { SeedParameter.Value.Value = value; }
|
---|
| 131 | }
|
---|
[14558] | 132 | public bool SetSeedRandomly
|
---|
| 133 | {
|
---|
[13645] | 134 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 135 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 136 | }
|
---|
[14558] | 137 | public int MaxVariableReferences
|
---|
| 138 | {
|
---|
[13652] | 139 | get { return MaxVariableReferencesParameter.Value.Value; }
|
---|
| 140 | set { MaxVariableReferencesParameter.Value.Value = value; }
|
---|
[13645] | 141 | }
|
---|
[14558] | 142 | public IPolicy Policy
|
---|
| 143 | {
|
---|
[13658] | 144 | get { return PolicyParameter.Value; }
|
---|
| 145 | set { PolicyParameter.Value = value; }
|
---|
[13645] | 146 | }
|
---|
[14558] | 147 | public double PunishmentFactor
|
---|
| 148 | {
|
---|
[13645] | 149 | get { return PunishmentFactorParameter.Value.Value; }
|
---|
| 150 | set { PunishmentFactorParameter.Value.Value = value; }
|
---|
| 151 | }
|
---|
[14558] | 152 | public ICheckedItemList<StringValue> AllowedFactors
|
---|
| 153 | {
|
---|
[13645] | 154 | get { return AllowedFactorsParameter.Value; }
|
---|
| 155 | }
|
---|
[14558] | 156 | public int ConstantOptimizationIterations
|
---|
| 157 | {
|
---|
[13645] | 158 | get { return ConstantOptimizationIterationsParameter.Value.Value; }
|
---|
| 159 | set { ConstantOptimizationIterationsParameter.Value.Value = value; }
|
---|
| 160 | }
|
---|
[14558] | 161 | public bool ScaleVariables
|
---|
| 162 | {
|
---|
[13645] | 163 | get { return ScaleVariablesParameter.Value.Value; }
|
---|
| 164 | set { ScaleVariablesParameter.Value.Value = value; }
|
---|
| 165 | }
|
---|
[14558] | 166 | public bool CreateSolution
|
---|
| 167 | {
|
---|
[13645] | 168 | get { return CreateSolutionParameter.Value.Value; }
|
---|
| 169 | set { CreateSolutionParameter.Value.Value = value; }
|
---|
| 170 | }
|
---|
| 171 | #endregion
|
---|
| 172 |
|
---|
| 173 | [StorableConstructor]
|
---|
| 174 | protected MctsSymbolicRegressionAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 175 |
|
---|
| 176 | protected MctsSymbolicRegressionAlgorithm(MctsSymbolicRegressionAlgorithm original, Cloner cloner)
|
---|
| 177 | : base(original, cloner) {
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 181 | return new MctsSymbolicRegressionAlgorithm(this, cloner);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | public MctsSymbolicRegressionAlgorithm() {
|
---|
| 185 | Problem = new RegressionProblem(); // default problem
|
---|
| 186 |
|
---|
| 187 | var defaultFactorsList = new CheckedItemList<StringValue>(
|
---|
| 188 | new string[] { VariableProductFactorName, ExpFactorName, LogFactorName, InvFactorName, FactorSumsName }
|
---|
| 189 | .Select(s => new StringValue(s).AsReadOnly())
|
---|
| 190 | ).AsReadOnly();
|
---|
| 191 | defaultFactorsList.SetItemCheckedState(defaultFactorsList.First(s => s.Value == FactorSumsName), false);
|
---|
| 192 |
|
---|
| 193 | Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName,
|
---|
| 194 | "Number of iterations", new IntValue(100000)));
|
---|
| 195 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName,
|
---|
| 196 | "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 197 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName,
|
---|
| 198 | "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 199 | Parameters.Add(new FixedValueParameter<IntValue>(MaxVariablesParameterName,
|
---|
| 200 | "Maximal number of variables references in the symbolic regression models (multiple usages of the same variable are counted)", new IntValue(5)));
|
---|
[13658] | 201 | // Parameters.Add(new FixedValueParameter<DoubleValue>(CParameterName,
|
---|
| 202 | // "Balancing parameter in UCT formula (0 < c < 1000). Small values: greedy search. Large values: enumeration. Default: 1.0", new DoubleValue(1.0)));
|
---|
| 203 | Parameters.Add(new ValueParameter<IPolicy>(PolicyParameterName,
|
---|
| 204 | "The policy to use for selecting nodes in MCTS (e.g. Ucb)", new Ucb()));
|
---|
| 205 | PolicyParameter.Hidden = true;
|
---|
[13645] | 206 | Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>(AllowedFactorsParameterName,
|
---|
| 207 | "Choose which expressions are allowed as factors in the model.", defaultFactorsList));
|
---|
| 208 |
|
---|
| 209 | Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName,
|
---|
| 210 | "Number of iterations for constant optimization. A small number of iterations should be sufficient for most models. " +
|
---|
| 211 | "Set to 0 to disable constants optimization.", new IntValue(10)));
|
---|
| 212 | Parameters.Add(new FixedValueParameter<BoolValue>(ScaleVariablesParameterName,
|
---|
| 213 | "Set to true to scale all input variables to the range [0..1]", new BoolValue(false)));
|
---|
| 214 | Parameters[ScaleVariablesParameterName].Hidden = true;
|
---|
| 215 | Parameters.Add(new FixedValueParameter<DoubleValue>(PunishmentFactorParameterName, "Estimations of models can be bounded. The estimation limits are calculated in the following way (lb = mean(y) - punishmentFactor*range(y), ub = mean(y) + punishmentFactor*range(y))", new DoubleValue(10)));
|
---|
| 216 | Parameters[PunishmentFactorParameterName].Hidden = true;
|
---|
| 217 | Parameters.Add(new FixedValueParameter<IntValue>(UpdateIntervalParameterName,
|
---|
| 218 | "Number of iterations until the results are updated", new IntValue(100)));
|
---|
| 219 | Parameters[UpdateIntervalParameterName].Hidden = true;
|
---|
| 220 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName,
|
---|
| 221 | "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 222 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 226 | private void AfterDeserialization() {
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 230 | // Set up the algorithm
|
---|
| 231 | if (SetSeedRandomly) Seed = new System.Random().Next();
|
---|
| 232 |
|
---|
| 233 | // Set up the results display
|
---|
| 234 | var iterations = new IntValue(0);
|
---|
| 235 | Results.Add(new Result("Iterations", iterations));
|
---|
| 236 |
|
---|
[13669] | 237 | var bestSolutionIteration = new IntValue(0);
|
---|
| 238 | Results.Add(new Result("Best solution iteration", bestSolutionIteration));
|
---|
| 239 |
|
---|
[13645] | 240 | var table = new DataTable("Qualities");
|
---|
| 241 | table.Rows.Add(new DataRow("Best quality"));
|
---|
| 242 | table.Rows.Add(new DataRow("Current best quality"));
|
---|
| 243 | table.Rows.Add(new DataRow("Average quality"));
|
---|
| 244 | Results.Add(new Result("Qualities", table));
|
---|
| 245 |
|
---|
| 246 | var bestQuality = new DoubleValue();
|
---|
| 247 | Results.Add(new Result("Best quality", bestQuality));
|
---|
| 248 |
|
---|
| 249 | var curQuality = new DoubleValue();
|
---|
| 250 | Results.Add(new Result("Current best quality", curQuality));
|
---|
| 251 |
|
---|
| 252 | var avgQuality = new DoubleValue();
|
---|
| 253 | Results.Add(new Result("Average quality", avgQuality));
|
---|
| 254 |
|
---|
[13651] | 255 | var totalRollouts = new IntValue();
|
---|
| 256 | Results.Add(new Result("Total rollouts", totalRollouts));
|
---|
| 257 | var effRollouts = new IntValue();
|
---|
| 258 | Results.Add(new Result("Effective rollouts", effRollouts));
|
---|
| 259 | var funcEvals = new IntValue();
|
---|
| 260 | Results.Add(new Result("Function evaluations", funcEvals));
|
---|
| 261 | var gradEvals = new IntValue();
|
---|
| 262 | Results.Add(new Result("Gradient evaluations", gradEvals));
|
---|
| 263 |
|
---|
| 264 |
|
---|
[13645] | 265 | // same as in SymbolicRegressionSingleObjectiveProblem
|
---|
| 266 | var y = Problem.ProblemData.Dataset.GetDoubleValues(Problem.ProblemData.TargetVariable,
|
---|
| 267 | Problem.ProblemData.TrainingIndices);
|
---|
| 268 | var avgY = y.Average();
|
---|
| 269 | var minY = y.Min();
|
---|
| 270 | var maxY = y.Max();
|
---|
| 271 | var range = maxY - minY;
|
---|
| 272 | var lowerLimit = avgY - PunishmentFactor * range;
|
---|
| 273 | var upperLimit = avgY + PunishmentFactor * range;
|
---|
| 274 |
|
---|
| 275 | // init
|
---|
| 276 | var problemData = (IRegressionProblemData)Problem.ProblemData.Clone();
|
---|
| 277 | if (!AllowedFactors.CheckedItems.Any()) throw new ArgumentException("At least on type of factor must be allowed");
|
---|
[13658] | 278 | var state = MctsSymbolicRegressionStatic.CreateState(problemData, (uint)Seed, MaxVariableReferences, ScaleVariables, ConstantOptimizationIterations,
|
---|
| 279 | Policy,
|
---|
[13645] | 280 | lowerLimit, upperLimit,
|
---|
| 281 | allowProdOfVars: AllowedFactors.CheckedItems.Any(s => s.Value.Value == VariableProductFactorName),
|
---|
| 282 | allowExp: AllowedFactors.CheckedItems.Any(s => s.Value.Value == ExpFactorName),
|
---|
| 283 | allowLog: AllowedFactors.CheckedItems.Any(s => s.Value.Value == LogFactorName),
|
---|
| 284 | allowInv: AllowedFactors.CheckedItems.Any(s => s.Value.Value == InvFactorName),
|
---|
| 285 | allowMultipleTerms: AllowedFactors.CheckedItems.Any(s => s.Value.Value == FactorSumsName)
|
---|
| 286 | );
|
---|
| 287 |
|
---|
| 288 | var updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
| 289 | double sumQ = 0.0;
|
---|
| 290 | double bestQ = 0.0;
|
---|
| 291 | double curBestQ = 0.0;
|
---|
| 292 | int n = 0;
|
---|
| 293 | // Loop until iteration limit reached or canceled.
|
---|
| 294 | for (int i = 0; i < Iterations && !state.Done; i++) {
|
---|
| 295 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 296 |
|
---|
[13669] | 297 | var q = MctsSymbolicRegressionStatic.MakeStep(state);
|
---|
[13645] | 298 | sumQ += q; // sum of qs in the last updateinterval iterations
|
---|
| 299 | curBestQ = Math.Max(q, curBestQ); // the best q in the last updateinterval iterations
|
---|
| 300 | bestQ = Math.Max(q, bestQ); // the best q overall
|
---|
| 301 | n++;
|
---|
| 302 | // iteration results
|
---|
| 303 | if (n == updateInterval) {
|
---|
[13669] | 304 | if (bestQ > bestQuality.Value) {
|
---|
| 305 | bestSolutionIteration.Value = i;
|
---|
| 306 | }
|
---|
[13645] | 307 | bestQuality.Value = bestQ;
|
---|
| 308 | curQuality.Value = curBestQ;
|
---|
| 309 | avgQuality.Value = sumQ / n;
|
---|
| 310 | sumQ = 0.0;
|
---|
| 311 | curBestQ = 0.0;
|
---|
| 312 |
|
---|
[13651] | 313 | funcEvals.Value = state.FuncEvaluations;
|
---|
| 314 | gradEvals.Value = state.GradEvaluations;
|
---|
| 315 | effRollouts.Value = state.EffectiveRollouts;
|
---|
| 316 | totalRollouts.Value = state.TotalRollouts;
|
---|
| 317 |
|
---|
[13645] | 318 | table.Rows["Best quality"].Values.Add(bestQuality.Value);
|
---|
| 319 | table.Rows["Current best quality"].Values.Add(curQuality.Value);
|
---|
| 320 | table.Rows["Average quality"].Values.Add(avgQuality.Value);
|
---|
| 321 | iterations.Value += n;
|
---|
| 322 | n = 0;
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | // final results
|
---|
| 327 | if (n > 0) {
|
---|
[13669] | 328 | if (bestQ > bestQuality.Value) {
|
---|
| 329 | bestSolutionIteration.Value = iterations.Value + n;
|
---|
| 330 | }
|
---|
[13645] | 331 | bestQuality.Value = bestQ;
|
---|
| 332 | curQuality.Value = curBestQ;
|
---|
| 333 | avgQuality.Value = sumQ / n;
|
---|
| 334 |
|
---|
[13651] | 335 | funcEvals.Value = state.FuncEvaluations;
|
---|
| 336 | gradEvals.Value = state.GradEvaluations;
|
---|
| 337 | effRollouts.Value = state.EffectiveRollouts;
|
---|
| 338 | totalRollouts.Value = state.TotalRollouts;
|
---|
| 339 |
|
---|
[13645] | 340 | table.Rows["Best quality"].Values.Add(bestQuality.Value);
|
---|
| 341 | table.Rows["Current best quality"].Values.Add(curQuality.Value);
|
---|
| 342 | table.Rows["Average quality"].Values.Add(avgQuality.Value);
|
---|
| 343 | iterations.Value = iterations.Value + n;
|
---|
[13651] | 344 |
|
---|
[13645] | 345 | }
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 | Results.Add(new Result("Best solution quality (train)", new DoubleValue(state.BestSolutionTrainingQuality)));
|
---|
| 349 | Results.Add(new Result("Best solution quality (test)", new DoubleValue(state.BestSolutionTestQuality)));
|
---|
| 350 |
|
---|
[13651] | 351 |
|
---|
[13645] | 352 | // produce solution
|
---|
| 353 | if (CreateSolution) {
|
---|
| 354 | var model = state.BestModel;
|
---|
| 355 |
|
---|
| 356 | // otherwise we produce a regression solution
|
---|
| 357 | Results.Add(new Result("Solution", model.CreateRegressionSolution(problemData)));
|
---|
| 358 | }
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | }
|
---|