1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System.Collections;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
30 | [StorableClass]
|
---|
31 | public class SymbolList : DeepCloneable, IList<Symbol> {
|
---|
32 | [Storable]
|
---|
33 | private List<Symbol> symbols;
|
---|
34 |
|
---|
35 | public int Count { get { return symbols.Count; } }
|
---|
36 |
|
---|
37 | public int TerminalSymbolCount { get { return symbols.Count(s => s is TerminalSymbol); } }
|
---|
38 | public int NonTerminalSymbolCount { get { return symbols.Count(s => s is NonterminalSymbol); } }
|
---|
39 |
|
---|
40 | public bool IsReadOnly {
|
---|
41 | get { return false; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Return the complexity as the number of non-terminal or variable-terminal symbols
|
---|
46 | /// </summary>
|
---|
47 | public int Complexity {
|
---|
48 | get {
|
---|
49 | int c = 0;
|
---|
50 | foreach (var s in symbols) {
|
---|
51 | if (s is NonterminalSymbol || s is VariableTerminalSymbol) c++;
|
---|
52 | }
|
---|
53 | return c;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Symbol this[int index] {
|
---|
58 | get { return symbols[index]; }
|
---|
59 | set { symbols[index] = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public SymbolList(params Symbol[] symbols) {
|
---|
63 | this.symbols = symbols.ToList();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public SymbolList(IEnumerable<Symbol> symbols) {
|
---|
67 | this.symbols = symbols.ToList();
|
---|
68 | }
|
---|
69 |
|
---|
70 | public SymbolList(List<Symbol> symbols) {
|
---|
71 | this.symbols = symbols;
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | protected SymbolList(bool deserializing) { }
|
---|
76 |
|
---|
77 | protected SymbolList(SymbolList original, Cloner cloner) {
|
---|
78 | symbols = original.symbols.Select(cloner.Clone).ToList();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new SymbolList(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override string ToString() {
|
---|
86 | return string.Join(" ", this);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public bool IsSentence() {
|
---|
90 | return !this.Any(sym => sym is NonterminalSymbol);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public int[] GetNonterminalSymbolIndexes() {
|
---|
94 | return Enumerable.Range(0, symbols.Count)
|
---|
95 | .Where(i => symbols[i] is NonterminalSymbol)
|
---|
96 | .ToArray();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public int NextNonterminalIndex() {
|
---|
100 | Debug.Assert(!IsSentence());
|
---|
101 | int firstNonTerminalIndex = 0;
|
---|
102 | while (firstNonTerminalIndex < symbols.Count && symbols[firstNonTerminalIndex] is TerminalSymbol) {
|
---|
103 | firstNonTerminalIndex++;
|
---|
104 | }
|
---|
105 | return firstNonTerminalIndex;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public SymbolList DerivePhrase(int nonTerminalSymbolIndex, SymbolList production) {
|
---|
109 | // derive a new phrase by replacing the non-terminal at the given index with the production
|
---|
110 | var derived = new List<Symbol>(Count + production.Count - 1);
|
---|
111 | derived.AddRange(symbols.Take(nonTerminalSymbolIndex));
|
---|
112 | derived.AddRange(production);
|
---|
113 | derived.AddRange(symbols.Skip(nonTerminalSymbolIndex + 1));
|
---|
114 | return new SymbolList(derived);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #region IList<Symbol> methods
|
---|
118 | public IEnumerator<Symbol> GetEnumerator() {
|
---|
119 | return symbols.GetEnumerator();
|
---|
120 | }
|
---|
121 |
|
---|
122 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
123 | return GetEnumerator();
|
---|
124 | }
|
---|
125 |
|
---|
126 | public int IndexOf(Symbol item) {
|
---|
127 | return symbols.IndexOf(item);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void Insert(int index, Symbol item) {
|
---|
131 | symbols.Insert(index, item);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void RemoveAt(int index) {
|
---|
135 | symbols.RemoveAt(index);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void Add(Symbol item) {
|
---|
139 | symbols.Add(item);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void Clear() {
|
---|
143 | symbols.Clear();
|
---|
144 | }
|
---|
145 |
|
---|
146 | public bool Contains(Symbol item) {
|
---|
147 | return symbols.Contains(item);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void CopyTo(Symbol[] array, int arrayIndex) {
|
---|
151 | symbols.CopyTo(array, arrayIndex);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void CopyTo(int index, Symbol[] array, int arrayIndex, int count) {
|
---|
155 | symbols.CopyTo(index, array, arrayIndex, count);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public bool Remove(Symbol item) {
|
---|
159 | return symbols.Remove(item);
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 | }
|
---|
163 | }
|
---|