Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Agent.cs @ 17984

Last change on this file since 17984 was 17984, checked in by gkronber, 3 years ago

#3106 updated implementation based on the reply by Moscato

File size: 2.0 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
4  public class Agent {
5    public ContinuedFraction pocket;
6    public double pocketObjValue;
7    public ContinuedFraction current;
8    public double currentObjValue;
9
10    public IList<Agent> children = new List<Agent>();
11
12    public IEnumerable<Agent> IterateLevels() {
13      var agents = new List<Agent>() { this };
14      int i = 0;
15      while (i < agents.Count) {
16        var p = agents[i];
17        foreach (var child in p.children)
18          agents.Add(child);
19        i++;
20      }
21      return agents;
22    }
23    public IEnumerable<Agent> IteratePostOrder() {
24      var agents = new List<Agent>();
25      IteratePostOrderRec(this, agents);
26      return agents;
27    }
28    public IEnumerable<Agent> IteratePreOrder() {
29      var agents = new List<Agent>();
30      IteratePreOrderRec(this, agents);
31      return agents;
32    }
33    internal void MaintainInvariant() {
34      foreach (var child in children) {
35        MaintainInvariant(parent: this, child);
36      }
37      if (currentObjValue < pocketObjValue) {
38        Swap(ref pocket, ref current);
39        Swap(ref pocketObjValue, ref currentObjValue);
40      }
41    }
42
43
44    private static void MaintainInvariant(Agent parent, Agent child) {
45      if (child.pocketObjValue < parent.pocketObjValue) {
46        Swap(ref child.pocket, ref parent.pocket);
47        Swap(ref child.pocketObjValue, ref parent.pocketObjValue);
48      }
49    }
50
51    private void IteratePostOrderRec(Agent agent, List<Agent> agents) {
52      foreach (var child in agent.children) {
53        IteratePostOrderRec(child, agents);
54      }
55      agents.Add(agent);
56    }
57
58    private void IteratePreOrderRec(Agent agent, List<Agent> agents) {
59      agents.Add(agent);
60      foreach (var child in agent.children) {
61        IteratePreOrderRec(child, agents);
62      }
63    }
64
65
66    private static void Swap<T>(ref T a, ref T b) {
67      var temp = a;
68      a = b;
69      b = temp;
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.