Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/28/09 19:24:23 (15 years ago)
Author:
gkronber
Message:

Created a branch for #713

Location:
branches/GP-Refactoring-713
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BakedFunctionTree.cs

    r2174 r2202  
    3434    public byte arity = 0;
    3535    public IFunction functionType;
    36     public List<object> localData = new List<object>();
     36    public List<IVariable> localData = new List<IVariable>();
    3737
    3838    public LightWeightFunction Clone() {
     
    4040      clone.arity = arity;
    4141      clone.functionType = functionType;
    42       clone.localData.AddRange(localData);
     42      clone.localData = localData.Select(v=>(IVariable)v.Clone()).ToList();
    4343      return clone;
    4444    }
     
    5757    private List<IFunctionTree> subTrees;
    5858    private bool variablesExpanded = false;
    59     private List<IVariable> variables;
     59    private List<IVariable> variables = new List<IVariable>();
     60
     61    public IOperator Initializer {
     62      get { return Function.Initializer; }
     63    }
     64
     65    public IOperator Manipulator {
     66      get { return Function.Manipulator; }
     67    }
    6068
    6169    public BakedFunctionTree() {
     
    6371    }
    6472
    65     internal BakedFunctionTree(IFunction function)
     73    public BakedFunctionTree(IFunction function)
    6674      : this() {
    6775      LightWeightFunction fun = new LightWeightFunction();
     
    7280      variables = new List<IVariable>();
    7381      variablesExpanded = true;
    74       foreach (IVariableInfo variableInfo in function.VariableInfos) {
    75         if (variableInfo.Local) {
    76           variables.Add((IVariable)function.GetVariable(variableInfo.FormalName).Clone());
    77         }
    78       }
    79     }
    80 
    81     internal BakedFunctionTree(IFunctionTree tree)
    82       : this() {
    83       LightWeightFunction fun = new LightWeightFunction();
    84       fun.functionType = tree.Function;
    85       linearRepresentation.Add(fun);
    86       foreach (IVariable variable in tree.LocalVariables) {
    87         IObjectData value = (IObjectData)variable.Value;
    88         fun.localData.Add(value.Data);
    89       }
    90       foreach (IFunctionTree subTree in tree.SubTrees) {
    91         AddSubTree(new BakedFunctionTree(subTree));
    92       }
    93     }
     82    }
     83
     84    //internal BakedFunctionTree(IFunctionTree tree)
     85    //  : this() {
     86    //  LightWeightFunction fun = new LightWeightFunction();
     87    //  fun.functionType = tree.Function;
     88    //  linearRepresentation.Add(fun);
     89    //  foreach (IVariable variable in tree.LocalVariables) {
     90    //    IObjectData value = (IObjectData)variable.Value;
     91    //    fun.localData.Add(value.Data);
     92    //  }
     93    //  foreach (IFunctionTree subTree in tree.SubTrees) {
     94    //    AddSubTree(new BakedFunctionTree(subTree));
     95    //  }
     96    //}
    9497
    9598    private int BranchLength(int branchRoot) {
     
    119122        linearRepresentation[0].localData.Clear();
    120123        foreach (IVariable variable in variables) {
    121           object objData = variable.Value;
    122           while (objData is IObjectData) objData = ((IObjectData)objData).Data;
    123           linearRepresentation[0].localData.Add(objData);
     124          //object objData = variable.Value;
     125          //while (objData is IObjectData) objData = ((IObjectData)objData).Data;
     126          //linearRepresentation[0].localData[variable.Name] = objData;
     127          linearRepresentation[0].localData.Add(variable);
    124128        }
    125129        variablesExpanded = false;
     
    197201        if (!variablesExpanded) {
    198202          variables = new List<IVariable>();
    199           IFunction function = Function;
    200           int localVariableIndex = 0;
    201           foreach (IVariableInfo variableInfo in function.VariableInfos) {
    202             if (variableInfo.Local) {
    203               IVariable clone = (IVariable)function.GetVariable(variableInfo.FormalName).Clone();
    204               IObjectData objData = (IObjectData)clone.Value;
    205               if (objData is ConstrainedDoubleData) {
    206                 ((ConstrainedDoubleData)objData).Data = (double)linearRepresentation[0].localData[localVariableIndex];
    207               } else if (objData is ConstrainedIntData) {
    208                 ((ConstrainedIntData)objData).Data = (int)linearRepresentation[0].localData[localVariableIndex];
    209               } else {
    210                 objData.Data = linearRepresentation[0].localData[localVariableIndex];
    211               }
    212               variables.Add(clone);
    213               localVariableIndex++;
    214             }
     203          IFunction function = Function;         
     204          //int localVariableIndex = 0;
     205          foreach (IVariable v in linearRepresentation[0].localData) {
     206            //IObjectData objData = (IObjectData)clone.Value;
     207            //if (objData is ConstrainedDoubleData) {
     208            //  ((ConstrainedDoubleData)objData).Data = (double)linearRepresentation[0].localData[localVariableIndex];
     209            //} else if (objData is ConstrainedIntData) {
     210            //  ((ConstrainedIntData)objData).Data = (int)linearRepresentation[0].localData[localVariableIndex];
     211            //} else {
     212            //  objData.Data = linearRepresentation[0].localData[localVariableIndex];
     213            //}
     214            variables.Add(v);
     215            //localVariableIndex++;
    215216          }
    216217          variablesExpanded = true;
     
    233234
    234235    public void AddVariable(IVariable variable) {
    235       throw new NotSupportedException();
     236      if (!variablesExpanded) throw new InvalidOperationException();
     237      if (!variables.Exists(v => v.Name == variable.Name))
     238        variables.Add(variable);
    236239    }
    237240
    238241    public void RemoveVariable(string name) {
    239       throw new NotSupportedException();
     242      if (!variablesExpanded) throw new InvalidOperationException();
     243      variables.RemoveAll(v => v.Name == name);
    240244    }
    241245
     
    256260    }
    257261
    258     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    259       FlattenVariables();
    260       FlattenTrees();
    261       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    262       XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation");
    263       foreach (LightWeightFunction f in linearRepresentation) {
    264         XmlNode entryNode = PersistenceManager.Persist("Function", f.functionType, document, persistedObjects);
    265         XmlAttribute arityAttribute = document.CreateAttribute("Arity");
    266         arityAttribute.Value = XmlConvert.ToString(f.arity);
    267         entryNode.Attributes.Append(arityAttribute);
    268         foreach (object o in f.localData) {
    269           if (f.localData.Count > 0) {
    270             entryNode.AppendChild(CreateDataNode(document, o));
    271           }
    272         }
    273         linearRepresentationNode.AppendChild(entryNode);
    274       }
    275 
    276       node.AppendChild(linearRepresentationNode);
    277       return node;
    278     }
     262    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
     263    //  FlattenVariables();
     264    //  FlattenTrees();
     265    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     266    //  XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation");
     267    //  foreach (LightWeightFunction f in linearRepresentation) {
     268    //    XmlNode entryNode = PersistenceManager.Persist("Function", f.functionType, document, persistedObjects);
     269    //    XmlAttribute arityAttribute = document.CreateAttribute("Arity");
     270    //    arityAttribute.Value = XmlConvert.ToString(f.arity);
     271    //    entryNode.Attributes.Append(arityAttribute);
     272    //    foreach (object o in f.localData) {
     273    //      if (f.localData.Count > 0) {
     274    //        entryNode.AppendChild(CreateDataNode(document, o));
     275    //      }
     276    //    }
     277    //    linearRepresentationNode.AppendChild(entryNode);
     278    //  }
     279
     280    //  node.AppendChild(linearRepresentationNode);
     281    //  return node;
     282    //}
    279283
    280284    private XmlNode CreateDataNode(XmlDocument doc, object o) {
     
    295299    }
    296300
    297     public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    298       base.Populate(node, restoredObjects);
    299       XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation");
    300       foreach (XmlNode entryNode in linearRepresentationNode.ChildNodes) {
    301         LightWeightFunction f = new LightWeightFunction();
    302         f.arity = XmlConvert.ToByte(entryNode.Attributes["Arity"].Value);
    303         foreach (XmlNode dataNode in entryNode.ChildNodes) {
    304           f.localData.Add(ParseDataNode(dataNode));
    305         }
    306         f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects);
    307         linearRepresentation.Add(f);
    308       }
    309       treesExpanded = false;
    310       variablesExpanded = false;
    311     }
     301    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     302    //  base.Populate(node, restoredObjects);
     303    //  XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation");
     304    //  foreach (XmlNode entryNode in linearRepresentationNode.ChildNodes) {
     305    //    LightWeightFunction f = new LightWeightFunction();
     306    //    f.arity = XmlConvert.ToByte(entryNode.Attributes["Arity"].Value);
     307    //    foreach (XmlNode dataNode in entryNode.ChildNodes) {
     308    //      f.localData.Add(ParseDataNode(dataNode));
     309    //    }
     310    //    f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects);
     311    //    linearRepresentation.Add(f);
     312    //  }
     313    //  treesExpanded = false;
     314    //  variablesExpanded = false;
     315    //}
    312316
    313317    private object ParseDataNode(XmlNode dataNode) {
Note: See TracChangeset for help on using the changeset viewer.