Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/09 18:32:59 (15 years ago)
Author:
gkronber
Message:

Fixed a few issues in persistence of hard-coded GP algorithms. #224 (Simple frontend for GP for non-expert users (similar to HeurisicLab.SGA))

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP.StructureIdentification/AlgorithmBase.cs

    r1251 r1262  
    3737namespace HeuristicLab.GP.StructureIdentification {
    3838  public abstract class AlgorithmBase : ItemBase {
    39     private DoubleData mutationRate = new DoubleData();
    4039    public virtual double MutationRate {
    41       get { return mutationRate.Data; }
    42       set { mutationRate.Data = value; }
    43     }
    44     private IntData populationSize = new IntData();
     40      get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
     41      set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
     42    }
    4543    public virtual int PopulationSize {
    46       get { return populationSize.Data; }
    47       set {
    48         populationSize.Data = value;
    49       }
    50     }
    51 
    52     private BoolData setSeedRandomly = new BoolData();
     44      get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
     45      set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
     46    }
     47
    5348    public virtual bool SetSeedRandomly {
    54       get { return setSeedRandomly.Data; }
    55       set { setSeedRandomly.Data = value; }
    56     }
    57 
    58     private IntData seed = new IntData();
     49      get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
     50      set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
     51    }
     52
    5953    public virtual int Seed {
    60       get { return seed.Data; }
    61       set { seed.Data = value; }
     54      get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
     55      set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
    6256    }
    6357
     
    7165    }
    7266
    73     private IntData elites = new IntData();
    7467    public virtual int Elites {
    75       get { return elites.Data; }
    76       set { elites.Data = value; }
    77     }
    78 
    79     private int maxTreeSize = 50;
     68      get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
     69      set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
     70    }
     71
    8072    public virtual int MaxTreeSize {
    81       get { return maxTreeSize; }
    82       set { maxTreeSize = value; }
    83     }
    84 
    85     private int maxTreeHeight = 8;
     73      get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
     74      set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
     75    }
     76
    8677    public virtual int MaxTreeHeight {
    87       get { return maxTreeHeight; }
    88       set { maxTreeHeight = value; }
    89     }
    90 
    91     private IntData parents = new IntData();
     78      get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
     79      set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
     80    }
     81
    9282    public virtual int Parents {
    93       get { return parents.Data; }
    94       protected set { parents.Data = value; }
    95     }
    96 
    97     private double punishmentFactor = 10.0;
    98     private bool useEstimatedTargetValue = false;
     83      get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
     84      set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
     85    }
     86
     87    public virtual double PunishmentFactor {
     88      get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
     89      set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
     90    }
     91
     92    public virtual bool UseEstimatedTargetValue {
     93      get { return GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data; }
     94      set { GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data = value; }
     95    }
     96
    9997    private IOperator algorithm;
    10098
     
    119117
    120118      RandomInjector randomInjector = new RandomInjector();
    121       randomInjector.GetVariable("SetSeedRandomly").Value = setSeedRandomly;
    122       randomInjector.GetVariable("Seed").Value = seed;
    123119      randomInjector.Name = "Random Injector";
    124120
    125121      IOperator globalInjector = CreateGlobalInjector();
    126122      IOperator initialization = CreateInitialization();
    127       IOperator funLibInjector = CreateFunctionLibraryInjector(); 
     123      IOperator funLibInjector = CreateFunctionLibraryInjector();
    128124      IOperator mainLoop = CreateMainLoop();
    129125      mainLoop.Name = "Main loop";
    130126
    131127      IOperator treeCreator = CreateTreeCreator();
    132      
     128
    133129      MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
    134130      evaluator.GetVariableInfo("MSE").ActualName = "Quality";
     
    136132      evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
    137133      evaluator.Name = "Evaluator";
    138      
     134
    139135      IOperator crossover = CreateCrossover();
    140136      IOperator manipulator = CreateManipulator();
     
    180176      injector.Name = "Global Injector";
    181177      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
    182       injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", mutationRate));
    183       injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", populationSize));
    184       injector.AddVariable(new HeuristicLab.Core.Variable("Elites", elites));
     178      injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
     179      injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
     180      injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
    185181      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
    186       injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData(maxTreeHeight)));
    187       injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData(maxTreeSize)));
     182      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
     183      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
    188184      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
    189185      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
    190       injector.AddVariable(new HeuristicLab.Core.Variable("Parents", parents));
    191       injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(punishmentFactor)));
    192       injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(useEstimatedTargetValue)));
     186      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
     187      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
     188      injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
    193189      return injector;
    194190    }
     
    392388    }
    393389
    394     #region SetReferences Method
    395     private void SetReferences() {
    396       // SGA
     390    protected VariableInjector GetVariableInjector() {
    397391      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
    398       // SequentialProcessor in SGA
     392      // SequentialProcessor in GP
    399393      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
    400       // RandomInjector
    401       RandomInjector ri = (RandomInjector)algorithm.SubOperators[1];
    402       setSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
    403       seed = ri.GetVariable("Seed").GetValue<IntData>();
    404       // VariableInjector
    405       VariableInjector vi = (VariableInjector)algorithm.SubOperators[2];
    406       populationSize = vi.GetVariable("PopulationSize").GetValue<IntData>();
    407       mutationRate = vi.GetVariable("MutationRate").GetValue<DoubleData>();
    408       elites = vi.GetVariable("Elites").GetValue<IntData>();
    409       parents = vi.GetVariable("Parents").GetValue<IntData>();
    410     }
    411     #endregion
     394      return (VariableInjector)algorithm.SubOperators[2];
     395    }
     396
     397    protected RandomInjector GetRandomInjector() {
     398      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
     399      // SequentialProcessor in GP
     400      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
     401      return (RandomInjector)algorithm.SubOperators[1];
     402    }
    412403
    413404    #region Persistence Methods
     
    420411      base.Populate(node, restoredObjects);
    421412      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
    422       SetReferences();
    423413    }
    424414    #endregion
Note: See TracChangeset for help on using the changeset viewer.