Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2365


Ignore:
Timestamp:
09/16/09 17:14:34 (15 years ago)
Author:
gkronber
Message:

Fixed #747 (GP engines create model with non-zero time-offsets).

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Variable.cs

    r2222 r2365  
    2323using HeuristicLab.Operators;
    2424using HeuristicLab.Random;
     25using HeuristicLab.Data;
    2526
    2627namespace HeuristicLab.GP.StructureIdentification {
    27   public class Variable : Terminal {   
     28  public class Variable : Terminal {
    2829    public const string WEIGHT = "Weight";
    2930    public const string OFFSET = "SampleOffset";
     
    99100      offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
    100101      offsetRandomAdder.Name = "Offset Adder";
     102      offsetRandomAdder.GetVariableInfo("MinValue").Local = true;
     103      offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MinValue", new DoubleData(minOffset)));
     104      offsetRandomAdder.GetVariableInfo("MaxValue").Local = true;
     105      offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MaxValue", new DoubleData(maxOffset + 1)));
    101106
    102107      combinedOp.OperatorGraph.AddOperator(seq);
  • trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs

    r2235 r2365  
    9494    }
    9595
    96     public virtual string ToString() {
     96    public override string ToString() {
    9797      return Function.Name;
    9898    }
  • trunk/sources/HeuristicLab.Random/3.2/NormalRandomAdder.cs

    r1530 r2365  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
    27 using HeuristicLab.Constraints;
    2827
    2928namespace HeuristicLab.Random {
     
    6968    /// </summary>
    7069    public NormalRandomAdder() {
    71       AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
     70      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.In));
    7271      GetVariableInfo("Mu").Local = true;
    7372      AddVariable(new Variable("Mu", new DoubleData(0.0)));
    7473
    75       AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
     74      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.In));
    7675      GetVariableInfo("Sigma").Local = true;
    7776      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
    7877
    79       AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
     78      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData", typeof(IObjectData), VariableKind.In | VariableKind.Out));
     79      AddVariableInfo(new VariableInfo("MinValue", "(optional) The minimal value", typeof(DoubleData), VariableKind.In));
     80      AddVariableInfo(new VariableInfo("MaxValue", "(optional) The maximal value", typeof(DoubleData), VariableKind.In));
    8081      AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor (effective sigma = sigma * shakingFactor)", typeof(DoubleData), VariableKind.In));
    8182      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
     
    9495      double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data;
    9596      double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data;
     97      DoubleData minValueData = GetVariableValue<DoubleData>("MinValue", scope, true, false);
     98      double minValue = minValueData == null ? double.MinValue : minValueData.Data;
     99      DoubleData maxValueData = GetVariableValue<DoubleData>("MaxValue", scope, true, false);
     100      double maxValue = maxValueData == null ? double.MaxValue : maxValueData.Data;
     101
    96102      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor);
    97103
    98       AddNormal(value, normal);
     104      AddNormal(value, normal, minValue, maxValue);
    99105      return null;
    100106    }
    101107
    102     private void AddNormal(IObjectData value, NormalDistributedRandom normal) {
     108    private void AddNormal(IObjectData value, NormalDistributedRandom normal, double minValue, double maxValue) {
    103109      // dispatch manually based on dynamic type
    104110      if (value is IntData)
    105         AddNormal((IntData)value, normal);
    106       else if (value is ConstrainedIntData)
    107         AddNormal((ConstrainedIntData)value, normal);
    108       else if (value is ConstrainedDoubleData)
    109         AddNormal((ConstrainedDoubleData)value, normal);
     111        AddNormal((IntData)value, normal, minValue, maxValue);
    110112      else if (value is DoubleData)
    111         AddNormal((DoubleData)value, normal);
     113        AddNormal((DoubleData)value, normal, minValue, maxValue);
    112114      else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);
    113     }
    114     /// <summary>
    115     /// Generates a new double random number and adds it to the value of
    116     /// the given <paramref name="data"/>.
    117     /// </summary>
    118     /// <param name="data">The double object where to add the random number.</param>
    119     /// <param name="normal">The continuous, normally distributed random variable.</param>
    120     public void AddNormal(DoubleData data, NormalDistributedRandom normal) {
    121       data.Data += normal.NextDouble();
    122115    }
    123116
     
    128121    /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value
    129122    /// could be found.</exception>
    130     /// <param name="data">The double object where to add the random number and whose constraints
    131     /// to fulfill.</param>
     123    /// <param name="data">The double object where to add the random number</param>
    132124    /// <param name="normal">The continuous, normally distributed random variable.</param>
    133     public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {
     125    /// <param name="minValue">The minimal value allowed for the double object.</param>
     126    /// <param name="maxValue">The maximal value allowed for the double object.</param>
     127    public void AddNormal(DoubleData data, NormalDistributedRandom normal, double minValue, double maxValue) {
    134128      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    135129        double newValue = data.Data + normal.NextDouble();
    136         if (IsIntegerConstrained(data)) {
    137           newValue = Math.Round(newValue);
    138         }
    139         if (data.TrySetData(newValue)) {
     130        if (newValue >= minValue && newValue < maxValue) {
     131          data.Data = newValue;
    140132          return;
    141133        }
    142134      }
    143135      throw new InvalidProgramException("Coudn't find a valid value");
    144     }
    145 
    146     /// <summary>
    147     /// Generates a new int random number and adds it to value of the given <paramref name="data"/>.
    148     /// </summary>
    149     /// <param name="data">The int object where to add the random number.</param>
    150     /// <param name="normal">The continuous, normally distributed random variable.</param>
    151     public void AddNormal(IntData data, NormalDistributedRandom normal) {
    152       data.Data = (int)Math.Round(data.Data + normal.NextDouble());
    153136    }
    154137
     
    159142    /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value
    160143    /// could be found.</exception>
    161     /// <param name="data">The int object where to add the generated value and whose contraints to check.</param>
     144    /// <param name="data">The int object where to add the generated value.</param>
    162145    /// <param name="normal">The continuous, normally distributed random variable.</param>
    163     public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) {
     146    /// <param name="minValue">The minimal value allowed for the double object.</param>
     147    /// <param name="maxValue">The maximal value allowed for the double object.</param>
     148    public void AddNormal(IntData data, NormalDistributedRandom normal, double minValue, double maxValue) {
    164149      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    165         if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
     150        int newValue = (int)Math.Round(data.Data + normal.NextDouble());
     151        if (newValue >= minValue && newValue < maxValue) {
     152          data.Data = newValue;
    166153          return;
     154        }
    167155      }
    168156      throw new InvalidProgramException("Couldn't find a valid value.");
    169157    }
    170 
    171     private bool IsIntegerConstrained(ConstrainedDoubleData data) {
    172       foreach (IConstraint constraint in data.Constraints) {
    173         if (constraint is IsIntegerConstraint) {
    174           return true;
    175         }
    176       }
    177       return false;
    178     }
    179158  }
    180159}
Note: See TracChangeset for help on using the changeset viewer.