Changeset 17972 for branches/3106_AnalyticContinuedFractionsRegression
- Timestamp:
- 04/30/21 18:01:39 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Algorithm.cs
r17971 r17972 24 24 var x = problemData.Dataset.ToArray(problemData.AllowedInputVariables.Concat(new[] { problemData.TargetVariable }), 25 25 problemData.TrainingIndices); 26 var nVars = x.GetLength(1) ;26 var nVars = x.GetLength(1) - 1; 27 27 var rand = new MersenneTwister(31415); 28 CFRAlgorithm(nVars, depth: 4, 0.10, x, out var best, out var bestObj, rand, numGen: 200, cancellationToken);28 CFRAlgorithm(nVars, depth: 6, 0.10, x, out var best, out var bestObj, rand, numGen: 200, stagnatingGens: 5, cancellationToken); 29 29 } 30 30 31 31 private void CFRAlgorithm(int nVars, int depth, double mutationRate, double[,] trainingData, 32 32 out ContinuedFraction best, out double bestObj, 33 IRandom rand, int numGen, 34 CancellationToken cancellation ) {33 IRandom rand, int numGen, int stagnatingGens, 34 CancellationToken cancellationToken) { 35 35 /* Algorithm 1 */ 36 36 /* Generate initial population by a randomized algorithm */ … … 38 38 best = pop.pocket; 39 39 bestObj = pop.pocketObjValue; 40 41 for (int gen = 1; gen <= numGen && !cancellation .IsCancellationRequested; gen++) {40 var bestObjGen = 0; 41 for (int gen = 1; gen <= numGen && !cancellationToken.IsCancellationRequested; gen++) { 42 42 /* mutate each current solution in the population */ 43 43 var pop_mu = Mutate(pop, mutationRate, rand); … … 47 47 /* local search optimization of current solutions */ 48 48 foreach (var agent in pop_r.IterateLevels()) { 49 LocalSearchSimplex(agent.current, trainingData, rand);49 LocalSearchSimplex(agent.current, ref agent.currentObjValue, trainingData, rand); 50 50 } 51 51 52 52 foreach (var agent in pop_r.IteratePostOrder()) agent.MaintainInvariant(); // Deviates from Alg1 in paper 53 54 /* TODO55 if (stagnating(curQuality, bestQuality, numStagnatingGensForReset)) {56 Reset(pop_r.root);57 }58 */59 53 60 54 /* replace old population with evolved population */ … … 65 59 best = pop.pocket; 66 60 bestObj = pop.pocketObjValue; 61 bestObjGen = gen; 67 62 Results.AddOrUpdateResult("MSE (best)", new DoubleValue(bestObj)); 63 } 64 65 66 if (gen > bestObjGen + stagnatingGens) { 67 bestObjGen = gen; // wait at least stagnatingGens until resetting again 68 // Reset(pop, nVars, depth, rand, trainingData); 69 InitialPopulation(nVars, depth, rand, trainingData); 68 70 } 69 71 } … … 95 97 return pop; 96 98 } 99 100 // TODO: reset is not described in the paper 101 private void Reset(Agent root, int nVars, int depth, IRandom rand, double[,] trainingData) { 102 root.pocket = new ContinuedFraction(nVars, depth, rand); 103 root.current = new ContinuedFraction(nVars, depth, rand); 104 105 root.currentObjValue = Evaluate(root.current, trainingData); 106 root.pocketObjValue = Evaluate(root.pocket, trainingData); 107 108 /* within each agent, the pocket solution always holds the better value of guiding 109 * function than its current solution 110 */ 111 root.MaintainInvariant(); 112 } 113 114 97 115 98 116 private Agent RecombinePopulation(Agent pop, IRandom rand, int nVars) { … … 182 200 agent.currentObjValue > 2 * agent.pocketObjValue) 183 201 ToggleVariables(agent.current, rand); // major mutation 184 else ModifyVariable(agent.current, rand); // soft mutation 202 else 203 ModifyVariable(agent.current, rand); // soft mutation 185 204 } 186 205 } … … 258 277 var hi = cfrac.h[i]; 259 278 var hi1 = cfrac.h[i - 1]; 260 var denom = hi.beta + dot( dataPoint, hi.coef) + res;261 var numerator = hi1.beta + dot( dataPoint, hi1.coef);279 var denom = hi.beta + dot(hi.vars, hi.coef, dataPoint) + res; 280 var numerator = hi1.beta + dot(hi1.vars, hi1.coef, dataPoint); 262 281 res = numerator / denom; 263 282 } … … 265 284 } 266 285 267 private static double dot( double[] x, double[] y) {286 private static double dot(bool[] filter, double[] x, double[] y) { 268 287 var s = 0.0; 269 288 for (int i = 0; i < x.Length; i++) 270 s += x[i] * y[i]; 289 if (filter[i]) 290 s += x[i] * y[i]; 271 291 return s; 272 292 } 273 293 274 294 275 private static ContinuedFraction LocalSearchSimplex(ContinuedFraction ch, double[,] trainingData, IRandom rand) {295 private static void LocalSearchSimplex(ContinuedFraction ch, ref double quality, double[,] trainingData, IRandom rand) { 276 296 double uniformPeturbation = 1.0; 277 297 double tolerance = 1e-3; … … 281 301 int numSelectedRows = numRows / 5; // 20% of the training samples 282 302 303 quality = Evaluate(ch, trainingData); // get quality with origial coefficients 304 283 305 double[] origCoeff = ExtractCoeff(ch); 284 if (origCoeff.Length == 0) return ch; // no parameters to optimize285 286 var bestQuality = Evaluate(ch, trainingData); // get quality with origial coefficients306 if (origCoeff.Length == 0) return; // no parameters to optimize 307 308 var bestQuality = quality; 287 309 var bestCoeff = origCoeff; 288 310 … … 317 339 318 340 SetCoeff(ch, bestCoeff); 319 return ch;341 quality = bestQuality; 320 342 } 321 343 … … 338 360 var coeff = new List<double>(); 339 361 foreach (var hi in ch.h) { 362 coeff.Add(hi.beta); 340 363 for (int vIdx = 0; vIdx < hi.vars.Length; vIdx++) { 341 364 if (hi.vars[vIdx]) coeff.Add(hi.coef[vIdx]); … … 348 371 int k = 0; 349 372 foreach (var hi in ch.h) { 373 hi.beta = curCoeff[k++]; 350 374 for (int vIdx = 0; vIdx < hi.vars.Length; vIdx++) { 351 375 if (hi.vars[vIdx]) hi.coef[vIdx] = curCoeff[k++];
Note: See TracChangeset
for help on using the changeset viewer.