Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationSymbolicExpressionTreeBinaryConverter.cs @ 15517

Last change on this file since 15517 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using Google.ProtocolBuffers;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Problems.ExternalEvaluation.GP {
31  [Item("SymbolicExpressionTreeBinaryConverter", "Converts a symbolic expression tree into a binary representation by iterating over all nodes in a prefix way. The binary format is defined in HeuristicLab.Persistence.")]
32  [StorableType("940ef9f1-e002-4d9b-80d1-521f5b869570")]
33  public class SymbolicExpressionTreeBinaryConverter : SymbolicExpressionTreeConverter {
34    [StorableConstructor]
35    protected SymbolicExpressionTreeBinaryConverter(StorableConstructorFlag deserializing) : base(deserializing) { }
36    protected SymbolicExpressionTreeBinaryConverter(SymbolicExpressionTreeBinaryConverter original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new SymbolicExpressionTreeBinaryConverter(this, cloner);
41    }
42    public SymbolicExpressionTreeBinaryConverter() : base() { }
43
44    protected override void ConvertSymbolicExpressionTree(SymbolicExpressionTree tree, string name, SolutionMessage.Builder builder) {
45      using (MemoryStream memoryStream = new MemoryStream()) {
46        XmlGenerator.Serialize(tree, memoryStream);
47        byte[] byteRep = memoryStream.ToArray();
48        SolutionMessage.Types.RawVariable.Builder rawVariable = SolutionMessage.Types.RawVariable.CreateBuilder();
49        rawVariable.SetName(name).SetData(ByteString.CopyFrom(byteRep));
50        builder.AddRawVars(rawVariable.Build());
51      }
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.