1 | namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 |
|
---|
6 | using HeuristicLab.Collections;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Boolean;
|
---|
13 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Char;
|
---|
14 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Float;
|
---|
15 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.FloatVector;
|
---|
16 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Integer;
|
---|
17 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.IntegerVector;
|
---|
18 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.String;
|
---|
19 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.StringVector;
|
---|
20 |
|
---|
21 | [StorableClass]
|
---|
22 | public class ErcOptions : ParameterizedNamedItem, IReadOnlyErcOptions {
|
---|
23 | private const string ProbabilityParameterName = "Probability";
|
---|
24 | private const string IntegerParameterName = "Integer";
|
---|
25 | private const string FloatParameterName = "Float";
|
---|
26 | private const string CharParameterName = "Char";
|
---|
27 | private const string BooleanParameterName = "Boolean";
|
---|
28 | private const string StringParameterName = "String";
|
---|
29 | private const string NameParameterName = "Name";
|
---|
30 | private const string IntegerVectorParameterName = "IntegerVector";
|
---|
31 | private const string FloatVectorParameterName = "FloatVector";
|
---|
32 | private const string StringVectorParameterName = "StringVector";
|
---|
33 |
|
---|
34 | [Storable]
|
---|
35 | private readonly NamedItemCollection<IValueParameter> ercOptionParameters;
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private readonly ReadOnlyCheckedItemCollection<IValueParameter> ercOptions;
|
---|
39 |
|
---|
40 | private IDictionary<IValueParameter, EventHandler<bool>> ercOptionEnabledChangedHandler;
|
---|
41 |
|
---|
42 | public ErcOptions() {
|
---|
43 | Name = "ERC Options";
|
---|
44 |
|
---|
45 | Parameters.Add(new FixedValueParameter<PercentValue>(ProbabilityParameterName));
|
---|
46 |
|
---|
47 | ercOptionParameters = new NamedItemCollection<IValueParameter>
|
---|
48 | {
|
---|
49 | new ValueParameter<IntegerErcOptions>(IntegerParameterName),
|
---|
50 | new ValueParameter<FloatErcOptions>(FloatParameterName),
|
---|
51 | new ValueParameter<BooleanErcOptions>(BooleanParameterName),
|
---|
52 | new ValueParameter<StringErcOptions>(StringParameterName),
|
---|
53 | new ValueParameter<CharErcOptions>(CharParameterName),
|
---|
54 | new ValueParameter<StringErcOptions>(NameParameterName),
|
---|
55 | new ValueParameter<IntegerVectorErcOptions>(IntegerVectorParameterName),
|
---|
56 | new ValueParameter<FloatVectorErcOptions>(FloatVectorParameterName),
|
---|
57 | new ValueParameter<StringVectorErcOptions>(StringVectorParameterName)
|
---|
58 | };
|
---|
59 |
|
---|
60 | InitEnabledChangedHandlerDict(ercOptionParameters);
|
---|
61 |
|
---|
62 | var optionsCollection = new CheckedItemCollection<IValueParameter>(ercOptionParameters);
|
---|
63 | ercOptions = new ReadOnlyCheckedItemCollection<IValueParameter>(optionsCollection);
|
---|
64 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IValueParameter>>("Options", ercOptions));
|
---|
65 |
|
---|
66 | ercOptions.CheckedItemsChanged += ErcOptionsCheckedItemsChanged;
|
---|
67 | DisableAllOptions();
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | protected ErcOptions(bool deserializing)
|
---|
72 | : base(deserializing) {
|
---|
73 | }
|
---|
74 |
|
---|
75 | public ErcOptions(ErcOptions origin, Cloner cloner)
|
---|
76 | : base(origin, cloner) {
|
---|
77 | ercOptions = cloner.Clone(origin.ercOptions);
|
---|
78 | ercOptionParameters = cloner.Clone(origin.ercOptionParameters);
|
---|
79 |
|
---|
80 | InitEnabledChangedHandlerDict(ercOptionParameters);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IValueParameter<PercentValue> ErcProbabilityParameter
|
---|
84 | {
|
---|
85 | get
|
---|
86 | {
|
---|
87 | return (IValueParameter<PercentValue>)Parameters[ProbabilityParameterName];
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public IValueParameter<IntegerErcOptions> IntegerErcOptionsParameter
|
---|
92 | {
|
---|
93 | get
|
---|
94 | {
|
---|
95 | return (IValueParameter<IntegerErcOptions>)ercOptionParameters[IntegerParameterName];
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public IntegerErcOptions IntegerErcOptions
|
---|
100 | {
|
---|
101 | get
|
---|
102 | {
|
---|
103 | return IntegerErcOptionsParameter.Value;
|
---|
104 | }
|
---|
105 | set
|
---|
106 | {
|
---|
107 | SetErcOptions(IntegerErcOptionsParameter, value);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public IValueParameter<FloatErcOptions> FloatErcOptionsParameter
|
---|
112 | {
|
---|
113 | get
|
---|
114 | {
|
---|
115 | return (IValueParameter<FloatErcOptions>)ercOptionParameters[FloatParameterName];
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public FloatErcOptions FloatErcOptions
|
---|
120 | {
|
---|
121 | get
|
---|
122 | {
|
---|
123 | return FloatErcOptionsParameter.Value;
|
---|
124 | }
|
---|
125 | set
|
---|
126 | {
|
---|
127 | SetErcOptions(FloatErcOptionsParameter, value);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /// <summary>
|
---|
132 | /// Integer represents ASCII value
|
---|
133 | /// </summary>
|
---|
134 | public IValueParameter<CharErcOptions> CharErcOptionsParameter
|
---|
135 | {
|
---|
136 | get
|
---|
137 | {
|
---|
138 | return (IValueParameter<CharErcOptions>)ercOptionParameters[CharParameterName];
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public CharErcOptions CharErcOptions
|
---|
143 | {
|
---|
144 | get
|
---|
145 | {
|
---|
146 | return CharErcOptionsParameter.Value;
|
---|
147 | }
|
---|
148 | set
|
---|
149 | {
|
---|
150 | SetErcOptions(CharErcOptionsParameter, value);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public IValueParameter<BooleanErcOptions> BooleanErcOptionsParameter
|
---|
155 | {
|
---|
156 | get
|
---|
157 | {
|
---|
158 | return (IValueParameter<BooleanErcOptions>)ercOptionParameters[BooleanParameterName];
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public BooleanErcOptions BooleanErcOptions
|
---|
163 | {
|
---|
164 | get
|
---|
165 | {
|
---|
166 | return BooleanErcOptionsParameter.Value;
|
---|
167 | }
|
---|
168 | set
|
---|
169 | {
|
---|
170 | SetErcOptions(BooleanErcOptionsParameter, value);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public IValueParameter<StringErcOptions> StringErcOptionsParameter
|
---|
175 | {
|
---|
176 | get
|
---|
177 | {
|
---|
178 | return (IValueParameter<StringErcOptions>)ercOptionParameters[StringParameterName];
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public StringErcOptions StringErcOptions
|
---|
183 | {
|
---|
184 | get
|
---|
185 | {
|
---|
186 | return StringErcOptionsParameter.Value;
|
---|
187 | }
|
---|
188 | set
|
---|
189 | {
|
---|
190 | SetErcOptions(StringErcOptionsParameter, value);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | public IValueParameter<StringErcOptions> NameParameter
|
---|
195 | {
|
---|
196 | get
|
---|
197 | {
|
---|
198 | return (IValueParameter<StringErcOptions>)ercOptionParameters[NameParameterName];
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | public StringErcOptions NameErcOptions
|
---|
203 | {
|
---|
204 | get
|
---|
205 | {
|
---|
206 | return NameParameter.Value;
|
---|
207 | }
|
---|
208 | set
|
---|
209 | {
|
---|
210 | SetErcOptions(NameParameter, value);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | public IValueParameter<IntegerVectorErcOptions> IntegerVectorErcOptionsParameter
|
---|
215 | {
|
---|
216 | get
|
---|
217 | {
|
---|
218 | return (IValueParameter<IntegerVectorErcOptions>)ercOptionParameters[IntegerVectorParameterName];
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public IntegerVectorErcOptions IntegerVectorErcOptions
|
---|
223 | {
|
---|
224 | get
|
---|
225 | {
|
---|
226 | return IntegerVectorErcOptionsParameter.Value;
|
---|
227 | }
|
---|
228 | set
|
---|
229 | {
|
---|
230 | SetErcOptions(IntegerVectorErcOptionsParameter, value);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public IValueParameter<StringVectorErcOptions> StringVectorErcOptionsValueParameter
|
---|
235 | {
|
---|
236 | get
|
---|
237 | {
|
---|
238 | return (IValueParameter<StringVectorErcOptions>)ercOptionParameters[StringVectorParameterName];
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | public StringVectorErcOptions StringVectorErcOptions
|
---|
243 | {
|
---|
244 | get
|
---|
245 | {
|
---|
246 | return StringVectorErcOptionsValueParameter.Value;
|
---|
247 | }
|
---|
248 | set
|
---|
249 | {
|
---|
250 | SetErcOptions(StringVectorErcOptionsValueParameter, value);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | public IValueParameter<FloatVectorErcOptions> FloatVectorErcOptionsParameter
|
---|
255 | {
|
---|
256 | get
|
---|
257 | {
|
---|
258 | return (IValueParameter<FloatVectorErcOptions>)ercOptionParameters[FloatVectorParameterName];
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public FloatVectorErcOptions FloatVectorErcOptions
|
---|
263 | {
|
---|
264 | get
|
---|
265 | {
|
---|
266 | return FloatVectorErcOptionsParameter.Value;
|
---|
267 | }
|
---|
268 | set
|
---|
269 | {
|
---|
270 | SetErcOptions(FloatVectorErcOptionsParameter, value);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | public double ErcProbability
|
---|
275 | {
|
---|
276 | get
|
---|
277 | {
|
---|
278 | return ErcProbabilityParameter.Value.Value;
|
---|
279 | }
|
---|
280 | set
|
---|
281 | {
|
---|
282 | ErcProbabilityParameter.Value.Value = value;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | IErcValueItem<int> IReadOnlyErcOptions.IntegerErcOptions
|
---|
287 | {
|
---|
288 | get
|
---|
289 | {
|
---|
290 | return IntegerErcOptions;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | IErcValueItem<double> IReadOnlyErcOptions.FloatErcOptions
|
---|
295 | {
|
---|
296 | get
|
---|
297 | {
|
---|
298 | return FloatErcOptions;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | IErcValueItem<bool> IReadOnlyErcOptions.BooleanErcOptions
|
---|
303 | {
|
---|
304 | get
|
---|
305 | {
|
---|
306 | return BooleanErcOptions;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | IErcValueItem<char> IReadOnlyErcOptions.CharErcOptions
|
---|
311 | {
|
---|
312 | get
|
---|
313 | {
|
---|
314 | return CharErcOptions;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | IErcValueItem<string> IReadOnlyErcOptions.StringErcOptions
|
---|
319 | {
|
---|
320 | get
|
---|
321 | {
|
---|
322 | return StringErcOptions;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | IErcValueItem<string> IReadOnlyErcOptions.NameErcOptions
|
---|
327 | {
|
---|
328 | get
|
---|
329 | {
|
---|
330 | return NameErcOptions;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | IErcValueItem<IReadOnlyList<int>> IReadOnlyErcOptions.IntegerVectorErcOptions
|
---|
335 | {
|
---|
336 | get
|
---|
337 | {
|
---|
338 | return IntegerVectorErcOptions;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | IErcValueItem<IReadOnlyList<double>> IReadOnlyErcOptions.FloatVectorErcOptions
|
---|
343 | {
|
---|
344 | get
|
---|
345 | {
|
---|
346 | return FloatVectorErcOptions;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | IErcValueItem<IReadOnlyList<string>> IReadOnlyErcOptions.StringVectorErcOptions
|
---|
351 | {
|
---|
352 | get
|
---|
353 | {
|
---|
354 | return StringVectorErcOptions;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | [StorableHook(HookType.AfterDeserialization)]
|
---|
359 | // ReSharper disable once UnusedMember.Local
|
---|
360 | private void AfterDeserialization() {
|
---|
361 | InitEnabledChangedHandlerDict(ercOptionParameters);
|
---|
362 | }
|
---|
363 |
|
---|
364 | private void InitEnabledChangedHandlerDict(NamedItemCollection<IValueParameter> parameters) {
|
---|
365 | ercOptionEnabledChangedHandler =
|
---|
366 | parameters.ToDictionary<IValueParameter, IValueParameter, EventHandler<bool>>(
|
---|
367 | param => param,
|
---|
368 | param => (sender, state) => ercOptions.SetItemCheckedState(param, state));
|
---|
369 | }
|
---|
370 |
|
---|
371 | private void ErcOptionsCheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IValueParameter> e) {
|
---|
372 | foreach (var item in e.Items) {
|
---|
373 | var ercItem = (IErcItem)item.Value;
|
---|
374 | ercItem.IsEnabled = ercOptions.ItemChecked(item);
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
379 | return new ErcOptions(this, cloner);
|
---|
380 | }
|
---|
381 |
|
---|
382 | public void DisableAllOptions() {
|
---|
383 | SetOptionsState(false);
|
---|
384 | }
|
---|
385 |
|
---|
386 | public void EnableAllOptions() {
|
---|
387 | SetOptionsState(true);
|
---|
388 | }
|
---|
389 |
|
---|
390 | public void SetOptionsState(bool state) {
|
---|
391 | foreach (var param in ercOptions) ercOptions.SetItemCheckedState(param, state);
|
---|
392 | }
|
---|
393 |
|
---|
394 | private void SetErcOptions(IValueParameter param, IErcItem value) {
|
---|
395 | if ((ercOptionEnabledChangedHandler[param] != null) && (param.Value != null)) ((IErcItem)param.Value).EnabledChanged -= ercOptionEnabledChangedHandler[param];
|
---|
396 |
|
---|
397 | param.Value = value;
|
---|
398 | ercOptions.SetItemCheckedState(param, value != null ? value.IsEnabled : false);
|
---|
399 |
|
---|
400 | if (value != null) value.EnabledChanged += ercOptionEnabledChangedHandler[param];
|
---|
401 | }
|
---|
402 | }
|
---|
403 | } |
---|