[5112] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[5653] | 4 | using System.Drawing;
|
---|
[5112] | 5 | using System.Linq;
|
---|
[5653] | 6 | using System.Text;
|
---|
| 7 | using HeuristicLab.Collections;
|
---|
[5112] | 8 | using HeuristicLab.Common;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
[5927] | 11 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5112] | 12 | using HeuristicLab.Parameters;
|
---|
| 13 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 14 | using HeuristicLab.PluginInfrastructure;
|
---|
| 15 |
|
---|
| 16 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 17 | [StorableClass]
|
---|
| 18 | public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent {
|
---|
[5207] | 19 | public bool IsOptimizable {
|
---|
| 20 | get { return true; }
|
---|
[5112] | 21 | }
|
---|
| 22 |
|
---|
| 23 | [Storable]
|
---|
| 24 | public string Filename { get; set; }
|
---|
| 25 |
|
---|
| 26 | [Storable]
|
---|
| 27 | protected bool optimize;
|
---|
[5927] | 28 | public virtual bool Optimize {
|
---|
[5112] | 29 | get { return optimize; }
|
---|
| 30 | set {
|
---|
| 31 | if (optimize != value) {
|
---|
| 32 | optimize = value;
|
---|
| 33 | if (optimize) {
|
---|
| 34 | PopulateValueConfigurations();
|
---|
| 35 | } else {
|
---|
[5927] | 36 | ClearValueConfigurations();
|
---|
[5112] | 37 | }
|
---|
| 38 | OnOptimizeChanged();
|
---|
| 39 | OnToStringChanged();
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | [Storable]
|
---|
[5927] | 45 | protected Image itemImage;
|
---|
| 46 | public override Image ItemImage {
|
---|
[5313] | 47 | get { return itemImage ?? base.ItemImage; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | [Storable]
|
---|
[5112] | 51 | protected string parameterName;
|
---|
| 52 | public string ParameterName {
|
---|
| 53 | get { return parameterName; }
|
---|
| 54 | set {
|
---|
| 55 | if (parameterName != value) {
|
---|
| 56 | parameterName = value;
|
---|
| 57 | OnToStringChanged();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | [Storable]
|
---|
| 63 | protected Type parameterDataType;
|
---|
| 64 | public Type ParameterDataType {
|
---|
| 65 | get { return this.parameterDataType; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | [Storable]
|
---|
[5207] | 69 | protected Type[] validTypes;
|
---|
| 70 | public Type[] ValidTypes {
|
---|
| 71 | get { return validTypes; }
|
---|
[5112] | 72 | protected set {
|
---|
[5207] | 73 | if (this.validTypes != value) {
|
---|
| 74 | this.validTypes = value;
|
---|
[5112] | 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | [Storable]
|
---|
| 80 | protected Type valueDataType;
|
---|
| 81 | public Type ValueDataType {
|
---|
| 82 | get { return valueDataType; }
|
---|
| 83 | protected set { this.valueDataType = value; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | [Storable]
|
---|
[5277] | 87 | protected ICheckedValueConfigurationList valueConfigurations;
|
---|
| 88 | public ICheckedValueConfigurationList ValueConfigurations {
|
---|
[5112] | 89 | get { return this.valueConfigurations; }
|
---|
| 90 | protected set {
|
---|
| 91 | if (this.valueConfigurations != value) {
|
---|
| 92 | if (this.valueConfigurations != null) DeregisterValueConfigurationEvents();
|
---|
| 93 | this.valueConfigurations = value;
|
---|
[6018] | 94 | KeepActualValueConfigurationIndexConsistent();
|
---|
[5112] | 95 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | [Storable]
|
---|
| 101 | private int actualValueConfigurationIndex = 0;
|
---|
| 102 | public int ActualValueConfigurationIndex {
|
---|
| 103 | get { return actualValueConfigurationIndex; }
|
---|
| 104 | set { actualValueConfigurationIndex = value; }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | [Storable]
|
---|
| 108 | protected ConstrainedValue actualValue;
|
---|
| 109 | public ConstrainedValue ActualValue {
|
---|
| 110 | get { return actualValue; }
|
---|
| 111 | set {
|
---|
| 112 | if (actualValue != value) {
|
---|
| 113 | DeregisterActualValueEvents();
|
---|
| 114 | actualValue = value;
|
---|
| 115 | RegisterActualValueEvents();
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | [Storable]
|
---|
| 121 | protected bool isNullable;
|
---|
| 122 | public bool IsNullable {
|
---|
| 123 | get { return isNullable; }
|
---|
| 124 | protected set {
|
---|
| 125 | if (this.isNullable != value) {
|
---|
| 126 | this.isNullable = value;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[5267] | 131 | [Storable]
|
---|
[5665] | 132 | protected bool discoverValidValues;
|
---|
| 133 | public bool DiscoverValidValues {
|
---|
| 134 | get { return discoverValidValues; }
|
---|
| 135 | set { discoverValidValues = value; }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | [Storable]
|
---|
[5231] | 139 | protected IItemSet<IItem> validValues;
|
---|
| 140 |
|
---|
[6017] | 141 | [Storable]
|
---|
| 142 | private bool autoPopulateValueConfigurations = true;
|
---|
| 143 | public bool AutoPopulateValueConfigurations {
|
---|
| 144 | get { return autoPopulateValueConfigurations; }
|
---|
| 145 | set { autoPopulateValueConfigurations = value; }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[6018] | 148 |
|
---|
[5112] | 149 | #region Constructors and Cloning
|
---|
[5665] | 150 | public ParameterConfiguration(string parameterName, IValueParameter valueParameter, bool discoverValidValues) {
|
---|
[6017] | 151 | this.AutoPopulateValueConfigurations = true;
|
---|
[5112] | 152 | this.ParameterName = parameterName;
|
---|
| 153 | this.parameterDataType = valueParameter.GetType();
|
---|
| 154 | this.valueDataType = valueParameter.DataType;
|
---|
[5665] | 155 | this.discoverValidValues = discoverValidValues;
|
---|
| 156 | this.validValues = discoverValidValues ? GetValidValues(valueParameter) : new ItemSet<IItem> { valueParameter.Value };
|
---|
[5207] | 157 | this.validTypes = GetValidTypes(valueParameter).ToArray();
|
---|
[5112] | 158 | this.IsNullable = valueParameter.ItemName.StartsWith("Optional");
|
---|
[5313] | 159 | this.itemImage = valueParameter.ItemImage;
|
---|
[5112] | 160 | if (IsNullable) {
|
---|
[5207] | 161 | validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray();
|
---|
[5112] | 162 | }
|
---|
[5313] | 163 | this.ValueConfigurations = new CheckedValueConfigurationList(this.validValues ?? CreateValidValues());
|
---|
[5267] | 164 | this.ActualValue = new ConstrainedValue(
|
---|
[5361] | 165 | valueParameter.Value != null ? valueParameter.Value : null, // don't clone here; otherwise settings of a non-optimized ValueParameter will not be synchronized with the ConstrainedValue
|
---|
[5653] | 166 | valueParameter.DataType,
|
---|
| 167 | this.validValues != null ? new ItemSet<IItem>(this.validValues) : CreateValidValues(),
|
---|
[5267] | 168 | this.IsNullable);
|
---|
[5112] | 169 | if (Optimize) {
|
---|
| 170 | PopulateValueConfigurations();
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
[5927] | 173 | public ParameterConfiguration(string parameterName, Type type, IItem actualValue, IEnumerable<IValueConfiguration> valueConfigurations) {
|
---|
[6017] | 174 | this.AutoPopulateValueConfigurations = false;
|
---|
[5927] | 175 | this.ParameterName = parameterName;
|
---|
| 176 | this.parameterDataType = type;
|
---|
| 177 | this.valueDataType = type;
|
---|
| 178 | this.discoverValidValues = false;
|
---|
| 179 | this.validValues = null; // maybe use values from valueConfigurations
|
---|
| 180 | this.validTypes = new Type[] { type };
|
---|
| 181 | this.IsNullable = false;
|
---|
| 182 | this.itemImage = valueConfigurations.Count() > 0 ? valueConfigurations.FirstOrDefault().ItemImage : null;
|
---|
| 183 | this.ValueConfigurations = new CheckedValueConfigurationList(valueConfigurations);
|
---|
| 184 | this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable);
|
---|
| 185 | }
|
---|
| 186 | public ParameterConfiguration(string parameterName, Type type, IItem actualValue) {
|
---|
[6017] | 187 | this.AutoPopulateValueConfigurations = true;
|
---|
[5927] | 188 | this.ParameterName = parameterName;
|
---|
| 189 | this.parameterDataType = type;
|
---|
| 190 | this.valueDataType = type;
|
---|
| 191 | this.discoverValidValues = false;
|
---|
| 192 | this.validValues = null;
|
---|
| 193 | this.validTypes = new Type[] { type };
|
---|
| 194 | this.IsNullable = false;
|
---|
| 195 | this.itemImage = actualValue.ItemImage;
|
---|
| 196 | this.ValueConfigurations = new CheckedValueConfigurationList();
|
---|
| 197 | this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable);
|
---|
| 198 | if (Optimize) {
|
---|
| 199 | PopulateValueConfigurations();
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
[5112] | 202 | public ParameterConfiguration() { }
|
---|
| 203 | [StorableConstructor]
|
---|
| 204 | protected ParameterConfiguration(bool deserializing) { }
|
---|
[5207] | 205 | protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
|
---|
| 206 | : base(original, cloner) {
|
---|
[5112] | 207 | this.parameterName = original.ParameterName;
|
---|
| 208 | this.parameterDataType = original.parameterDataType;
|
---|
| 209 | this.valueDataType = original.ValueDataType;
|
---|
[5231] | 210 | this.validValues = cloner.Clone(original.validValues);
|
---|
[5207] | 211 | this.validTypes = original.validTypes.ToArray();
|
---|
[5112] | 212 | this.valueConfigurations = cloner.Clone(original.ValueConfigurations);
|
---|
[6197] | 213 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
| 214 | this.actualValue = cloner.Clone(original.actualValue);
|
---|
| 215 | if (this.actualValue != null) RegisterActualValueEvents();
|
---|
[5112] | 216 | this.optimize = original.optimize;
|
---|
| 217 | this.actualValueConfigurationIndex = original.actualValueConfigurationIndex;
|
---|
| 218 | this.isNullable = original.isNullable;
|
---|
[5313] | 219 | this.itemImage = original.itemImage;
|
---|
[5665] | 220 | this.discoverValidValues = original.discoverValidValues;
|
---|
[6017] | 221 | this.AutoPopulateValueConfigurations = original.AutoPopulateValueConfigurations;
|
---|
[5112] | 222 | }
|
---|
[5927] | 223 |
|
---|
| 224 |
|
---|
[5112] | 225 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 226 | return new ParameterConfiguration(this, cloner);
|
---|
| 227 | }
|
---|
| 228 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[6197] | 229 | protected virtual void AfterDeserialization() {
|
---|
[5112] | 230 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
[6197] | 231 | if (this.actualValue != null) RegisterActualValueEvents();
|
---|
[5112] | 232 | }
|
---|
| 233 | #endregion
|
---|
| 234 |
|
---|
| 235 | private void RegisterValueConfigurationEvents() {
|
---|
[5277] | 236 | this.ValueConfigurations.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_CheckedItemsChanged);
|
---|
| 237 | this.ValueConfigurations.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsAdded);
|
---|
| 238 | this.ValueConfigurations.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsRemoved);
|
---|
[5112] | 239 | }
|
---|
| 240 |
|
---|
| 241 | private void DeregisterValueConfigurationEvents() {
|
---|
[5277] | 242 | this.ValueConfigurations.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_CheckedItemsChanged);
|
---|
| 243 | this.ValueConfigurations.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsAdded);
|
---|
| 244 | this.ValueConfigurations.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsRemoved);
|
---|
[5112] | 245 | }
|
---|
| 246 |
|
---|
| 247 | private void RegisterActualValueEvents() {
|
---|
| 248 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ActualValue_ToStringChanged);
|
---|
| 249 | }
|
---|
| 250 | private void DeregisterActualValueEvents() {
|
---|
| 251 | if (this.ActualValue != null) this.ActualValue.ToStringChanged -= new EventHandler(ActualValue_ToStringChanged);
|
---|
| 252 | }
|
---|
[5653] | 253 |
|
---|
[5927] | 254 | protected virtual void PopulateValueConfigurations() {
|
---|
[6017] | 255 | if (!this.AutoPopulateValueConfigurations)
|
---|
| 256 | return;
|
---|
| 257 |
|
---|
[5207] | 258 | foreach (Type t in this.validTypes) {
|
---|
| 259 | if (t == typeof(NullValue)) {
|
---|
[5112] | 260 | this.ValueConfigurations.Add(new NullValueConfiguration());
|
---|
| 261 | } else {
|
---|
[5267] | 262 | IItem val;
|
---|
[6018] | 263 | if (ActualValue.Value != null && ActualValue.Value.GetType() == t) {
|
---|
[5267] | 264 | val = (IItem)ActualValue.Value.Clone(); // use existing value for that type (if available)
|
---|
| 265 | } else {
|
---|
| 266 | val = CreateItem(t);
|
---|
| 267 | }
|
---|
[5665] | 268 | if (val != null) { // val can be null when ValidValues does not contain the type (this can happen when discoverValidValues=false)
|
---|
[5927] | 269 | IValueConfiguration valueConfiguration;
|
---|
[5665] | 270 | if (val is IParameterizedItem) {
|
---|
[5927] | 271 | valueConfiguration = new ParameterizedValueConfiguration(val, val.GetType(), true);
|
---|
[5665] | 272 | } else {
|
---|
[5927] | 273 | if (val is ISymbolicExpressionGrammar) {
|
---|
| 274 | valueConfiguration = new SymbolicExpressionGrammarValueConfiguration((ISymbolicExpressionGrammar)val);
|
---|
| 275 | } else {
|
---|
| 276 | valueConfiguration = new RangeValueConfiguration(val, val.GetType());
|
---|
| 277 | }
|
---|
[5665] | 278 | }
|
---|
[5927] | 279 | this.ValueConfigurations.Add(valueConfiguration, true);
|
---|
[5653] | 280 | }
|
---|
[5112] | 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[5927] | 285 | protected virtual void ClearValueConfigurations() {
|
---|
[6017] | 286 | if (!this.AutoPopulateValueConfigurations)
|
---|
| 287 | return;
|
---|
| 288 |
|
---|
[5927] | 289 | this.ValueConfigurations.Clear();
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[5337] | 292 | private static IEnumerable<Type> GetValidTypes(IValueParameter parameter) {
|
---|
[5267] | 293 | // in case of IOperator return empty list, otherwise hundreds of types would be returned. this mostly occurs for Successor which will never be optimized
|
---|
| 294 | if (parameter.DataType == typeof(IOperator))
|
---|
| 295 | return new List<Type>();
|
---|
| 296 |
|
---|
[5337] | 297 | // return only one type for ValueTypeValues<> (Int, Double, Percent, Bool). Otherwise 2 types would be returned for DoubleValue (PercentValue which is derived)
|
---|
| 298 | if (IsSubclassOfRawGeneric(typeof(ValueTypeValue<>), parameter.DataType))
|
---|
[5653] | 299 | return new List<Type> { parameter.DataType };
|
---|
| 300 |
|
---|
[5231] | 301 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
[5337] | 302 | // use existing validvalues if known
|
---|
[5231] | 303 | var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
| 304 | return parameterValidValues.Cast<object>().Select(x => x.GetType());
|
---|
| 305 | } else {
|
---|
[5337] | 306 | // otherwise use all assignable types
|
---|
| 307 | return ApplicationManager.Manager.GetTypes(parameter.DataType, true);
|
---|
[5231] | 308 | }
|
---|
[5112] | 309 | }
|
---|
| 310 |
|
---|
[5231] | 311 | private IItemSet<IItem> GetValidValues(IValueParameter parameter) {
|
---|
| 312 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
| 313 | var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
[5267] | 314 | return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone())); // cloning actually saves memory, because references to event-subscribers are removed
|
---|
[5231] | 315 | } else {
|
---|
| 316 | return null;
|
---|
| 317 | }
|
---|
[6445] | 318 |
|
---|
[5231] | 319 | }
|
---|
| 320 |
|
---|
| 321 | public IItem CreateItem(Type type) {
|
---|
| 322 | // no valid values; just instantiate
|
---|
[5927] | 323 | try {
|
---|
| 324 | if (validValues == null)
|
---|
| 325 | return (IItem)Activator.CreateInstance(type);
|
---|
| 326 | }
|
---|
| 327 | catch (MissingMemberException) {
|
---|
| 328 | return null; // can happen, when ApplicationManager.Manager.GetTypes(type, OnlyInstantiable=true) returns objects which have no default constructor
|
---|
| 329 | }
|
---|
[5231] | 330 |
|
---|
| 331 | if (type == typeof(NullValue))
|
---|
| 332 | return new NullValue();
|
---|
[5653] | 333 |
|
---|
[5231] | 334 | // copy value from ValidValues; this ensures that previously set ActualNames for a type are kept
|
---|
| 335 | IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault();
|
---|
[5653] | 336 | if (value != null)
|
---|
[5267] | 337 | return value;
|
---|
[5231] | 338 |
|
---|
| 339 | return null;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[5313] | 342 | private IItemSet<IItem> CreateValidValues() {
|
---|
[5231] | 343 | var validValues = new ItemSet<IItem>();
|
---|
| 344 | foreach (Type t in this.validTypes) {
|
---|
[5267] | 345 | try {
|
---|
| 346 | var val = CreateItem(t);
|
---|
[5927] | 347 | if (val != null) validValues.Add(val);
|
---|
[5267] | 348 | }
|
---|
| 349 | catch (MissingMethodException) { /* Constructor is missing, don't use those types */ }
|
---|
[5231] | 350 | }
|
---|
| 351 | return validValues;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[6445] | 354 | public static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
|
---|
[5112] | 355 | while (toCheck != typeof(object)) {
|
---|
| 356 | var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
|
---|
| 357 | if (generic == cur) {
|
---|
| 358 | return true;
|
---|
| 359 | }
|
---|
[5337] | 360 | toCheck = toCheck.BaseType; // baseType is null when toCheck is an Interface
|
---|
| 361 | if (toCheck == null)
|
---|
| 362 | return false;
|
---|
[5112] | 363 | }
|
---|
| 364 | return false;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[6421] | 367 | private void ValueConfigurations_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 368 | OnToStringChanged();
|
---|
[6018] | 369 | KeepActualValueConfigurationIndexConsistent();
|
---|
[5112] | 370 | }
|
---|
[6018] | 371 |
|
---|
[6421] | 372 | private void ValueConfigurations_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 373 | OnToStringChanged();
|
---|
[6018] | 374 | KeepActualValueConfigurationIndexConsistent();
|
---|
[5112] | 375 | }
|
---|
[6421] | 376 | private void ValueConfigurations_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 377 | OnToStringChanged();
|
---|
[6018] | 378 | KeepActualValueConfigurationIndexConsistent();
|
---|
[5112] | 379 | }
|
---|
| 380 |
|
---|
| 381 | #region INamedItem Properties
|
---|
| 382 | public virtual string Name {
|
---|
| 383 | get { return ParameterName; }
|
---|
| 384 | set { throw new NotSupportedException(); }
|
---|
| 385 | }
|
---|
| 386 | public virtual string Description {
|
---|
| 387 | get { return base.ItemDescription; }
|
---|
| 388 | set { throw new NotSupportedException(); }
|
---|
| 389 | }
|
---|
| 390 | public virtual bool CanChangeDescription {
|
---|
| 391 | get { return false; }
|
---|
| 392 | }
|
---|
| 393 | public virtual bool CanChangeName {
|
---|
| 394 | get { return false; }
|
---|
| 395 | }
|
---|
| 396 | public override string ItemDescription {
|
---|
| 397 | get { return base.ItemDescription; }
|
---|
| 398 | }
|
---|
| 399 | public override string ItemName {
|
---|
| 400 | get { return base.ItemName; }
|
---|
| 401 | }
|
---|
| 402 | #endregion
|
---|
| 403 |
|
---|
| 404 | #region Events
|
---|
[6197] | 405 | protected virtual void ActualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
[5112] | 406 | OnToStringChanged();
|
---|
| 407 | }
|
---|
| 408 | #endregion
|
---|
| 409 |
|
---|
| 410 | #region Event Handler
|
---|
| 411 | public virtual event EventHandler NameChanged;
|
---|
| 412 | protected virtual void OnNameChanged(object sender, EventArgs e) {
|
---|
| 413 | var handler = NameChanged;
|
---|
| 414 | if (handler != null) handler(sender, e);
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 418 | protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
|
---|
| 419 | var handler = NameChanging;
|
---|
| 420 | if (handler != null) handler(sender, e);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | public virtual event EventHandler DescriptionChanged;
|
---|
| 424 | protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
|
---|
| 425 | var handler = DescriptionChanged;
|
---|
| 426 | if (handler != null) handler(sender, e);
|
---|
| 427 | }
|
---|
[5207] | 428 |
|
---|
[5112] | 429 | public virtual event EventHandler IsOptimizableChanged;
|
---|
| 430 | private void OnIsOptimizableChanged() {
|
---|
| 431 | var handler = IsOptimizableChanged;
|
---|
| 432 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | public virtual event EventHandler OptimizeChanged;
|
---|
| 436 | protected virtual void OnOptimizeChanged() {
|
---|
| 437 | var handler = OptimizeChanged;
|
---|
| 438 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 439 | }
|
---|
| 440 | #endregion
|
---|
| 441 |
|
---|
| 442 | public override string ToString() {
|
---|
| 443 | if (Optimize) {
|
---|
| 444 | return string.Format("{0}: (Optimize: {1})", ParameterName, ValueConfigurations.CheckedItems.Count());
|
---|
| 445 | } else {
|
---|
| 446 | return string.Format("{0}: {1}", ParameterName, ActualValue.Value);
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[6018] | 450 | public virtual string ParameterInfoString {
|
---|
[5184] | 451 | get {
|
---|
| 452 | StringBuilder sb = new StringBuilder();
|
---|
| 453 | if (this.Optimize) {
|
---|
[5359] | 454 | var vc = this.ValueConfigurations[actualValueConfigurationIndex];
|
---|
[5361] | 455 | if (vc.ActualValue == null || vc.ActualValue.Value == null) {
|
---|
| 456 | sb.Append(string.Format("{0}: null", parameterName));
|
---|
| 457 | } else if (IsSubclassOfRawGeneric(typeof(ValueTypeValue<>), vc.ActualValue.Value.GetType())) {
|
---|
[5359] | 458 | // for int, double, bool use the value directly
|
---|
[5361] | 459 | sb.Append(string.Format("{0}: {1}", parameterName, this.ActualValue.Value.ToString()));
|
---|
[5359] | 460 | } else {
|
---|
| 461 | // for other types use NumberedName (this also uses the Number-Property for otherwise ambiguous ValueConfigurations)
|
---|
| 462 | sb.Append(string.Format("{0}: {1}", parameterName, vc.NumberedName));
|
---|
| 463 | }
|
---|
[5184] | 464 |
|
---|
[6018] | 465 | if (this.ValueConfigurations[actualValueConfigurationIndex] is ParameterizedValueConfiguration) {
|
---|
[5277] | 466 | string subParams = this.ValueConfigurations[actualValueConfigurationIndex].ParameterInfoString;
|
---|
[5184] | 467 | if (!string.IsNullOrEmpty(subParams)) {
|
---|
| 468 | sb.Append(" (");
|
---|
| 469 | sb.Append(subParams);
|
---|
| 470 | sb.Append(")");
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | return sb.ToString();
|
---|
[5144] | 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[5665] | 478 | public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter, bool discoverValidValues) {
|
---|
[5112] | 479 | if (parameter is IValueParameter) {
|
---|
| 480 | IValueParameter valueParameter = parameter as IValueParameter;
|
---|
[5665] | 481 | return new ParameterConfiguration(parameter.Name, valueParameter, discoverValidValues);
|
---|
[5112] | 482 | }
|
---|
| 483 | return null;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | public void Parameterize(IValueParameter parameter) {
|
---|
| 487 | if (Optimize) {
|
---|
| 488 | if (this.ActualValue.Value is IParameterizedItem) {
|
---|
[5653] | 489 | ((ParameterizedValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((IParameterizedItem)this.ActualValue.Value);
|
---|
[5112] | 490 | }
|
---|
[5927] | 491 | if (this.ActualValue.Value is ISymbolicExpressionGrammar) {
|
---|
| 492 | ((SymbolicExpressionGrammarValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((ISymbolicExpressionGrammar)this.ActualValue.Value);
|
---|
| 493 | }
|
---|
[5112] | 494 | }
|
---|
[5207] | 495 | var clonedValue = this.ActualValue.Value != null ? (IItem)this.ActualValue.Value.Clone() : null;
|
---|
[5665] | 496 | if (clonedValue != null) AdaptValidValues(parameter, clonedValue);
|
---|
[6421] | 497 | parameter.Value = clonedValue;
|
---|
[5112] | 498 | }
|
---|
| 499 |
|
---|
[5207] | 500 | /// <summary>
|
---|
| 501 | /// Adds value to the ValidValues of the parameter if they don't contain the value
|
---|
| 502 | /// </summary>
|
---|
| 503 | private void AdaptValidValues(IValueParameter parameter, IItem value) {
|
---|
| 504 | // this requires some tricky reflection, because the type is unknown at runtime so parameter.ValidValues cannot be casted to Collection<?>
|
---|
| 505 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
| 506 | var validValues = parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
| 507 | Type validValuesType = validValues.GetType();
|
---|
| 508 |
|
---|
| 509 | var containsMethod = validValuesType.GetMethod("Contains");
|
---|
| 510 | if (!(bool)containsMethod.Invoke(validValues, new object[] { value })) {
|
---|
| 511 | var addMethod = validValuesType.GetMethod("Add");
|
---|
| 512 | addMethod.Invoke(validValues, new object[] { value });
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[5112] | 517 | public void Randomize(IRandom random) {
|
---|
| 518 | if (Optimize) {
|
---|
[5277] | 519 | foreach (var vc in this.ValueConfigurations) {
|
---|
| 520 | if (this.ValueConfigurations.ItemChecked(vc)) {
|
---|
[5653] | 521 | if (vc.Optimize) vc.Randomize(random);
|
---|
[5277] | 522 | }
|
---|
[5112] | 523 | }
|
---|
[5277] | 524 | do {
|
---|
| 525 | actualValueConfigurationIndex = random.Next(ValueConfigurations.Count());
|
---|
| 526 | } while (!this.ValueConfigurations.ItemChecked(this.ValueConfigurations[actualValueConfigurationIndex]));
|
---|
| 527 | this.ActualValue = this.ValueConfigurations[actualValueConfigurationIndex].ActualValue;
|
---|
[5112] | 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[5277] | 531 | public void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
|
---|
[5112] | 532 | if (Optimize) {
|
---|
[5277] | 533 | foreach (var vc in this.ValueConfigurations) {
|
---|
| 534 | if (this.ValueConfigurations.ItemChecked(vc)) {
|
---|
[5653] | 535 | if (vc.Optimize) vc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
|
---|
[5277] | 536 | }
|
---|
[5112] | 537 | }
|
---|
[5277] | 538 | mutate(random, this, intValueManipulator, doubleValueManipulator);
|
---|
[5112] | 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[5277] | 542 | public void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
[5112] | 543 | if (Optimize) {
|
---|
| 544 | IParameterConfiguration otherPc = (IParameterConfiguration)other;
|
---|
| 545 | for (int i = 0; i < this.ValueConfigurations.Count; i++) {
|
---|
[5277] | 546 | if (this.ValueConfigurations.ItemChecked(this.ValueConfigurations[i])) {
|
---|
[5653] | 547 | if (this.ValueConfigurations[i].Optimize) this.ValueConfigurations[i].Cross(random, otherPc.ValueConfigurations[i], cross, intValueCrossover, doubleValueCrossover);
|
---|
[5277] | 548 | }
|
---|
[5112] | 549 | }
|
---|
[5277] | 550 | cross(random, this, other, intValueCrossover, doubleValueCrossover);
|
---|
[5112] | 551 | }
|
---|
| 552 | }
|
---|
[5144] | 553 |
|
---|
| 554 | public void UpdateActualValueIndexToItem(IValueConfiguration vc) {
|
---|
[5277] | 555 | for (int i = 0; i < this.ValueConfigurations.Count(); i++) {
|
---|
| 556 | if (this.ValueConfigurations.ElementAt(i) == vc) {
|
---|
[5144] | 557 | this.actualValueConfigurationIndex = i;
|
---|
| 558 | return;
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[5303] | 563 | public List<IOptimizable> GetAllOptimizables() {
|
---|
| 564 | var list = new List<IOptimizable>();
|
---|
| 565 | foreach (var vc in ValueConfigurations) {
|
---|
| 566 | if (vc.Optimize) {
|
---|
| 567 | list.Add(vc);
|
---|
| 568 | list.AddRange(vc.GetAllOptimizables());
|
---|
| 569 | }
|
---|
| 570 | }
|
---|
| 571 | return list;
|
---|
| 572 | }
|
---|
[5340] | 573 |
|
---|
| 574 | public void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
|
---|
| 575 | foreach (var vc in ValueConfigurations) {
|
---|
| 576 | if (vc.Optimize) {
|
---|
| 577 | vc.CollectOptimizedParameterNames(parameterNames, prefix);
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
[5522] | 581 |
|
---|
| 582 | public double CalculateSimilarity(IOptimizable optimizable) {
|
---|
| 583 | var other = (IParameterConfiguration)optimizable;
|
---|
| 584 | if (this.ActualValueConfigurationIndex == other.ActualValueConfigurationIndex) {
|
---|
| 585 | return this.ValueConfigurations[this.ActualValueConfigurationIndex].CalculateSimilarity(other.ValueConfigurations[other.ActualValueConfigurationIndex]);
|
---|
| 586 | } else {
|
---|
| 587 | return 0.0;
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
[6018] | 590 |
|
---|
| 591 | /// <summary>
|
---|
| 592 | /// Ensures that the ActualValueConfigurationIndex has a valid value
|
---|
| 593 | /// Only indices of items which are checked are allowed
|
---|
| 594 | /// </summary>
|
---|
| 595 | private void KeepActualValueConfigurationIndexConsistent() {
|
---|
| 596 | if (this.valueConfigurations != null && this.valueConfigurations.CheckedItems.Count() > 0) {
|
---|
| 597 | if(this.valueConfigurations.Count <= this.actualValueConfigurationIndex &&
|
---|
| 598 | this.valueConfigurations.CheckedItems.Count(x => x.Index == this.actualValueConfigurationIndex) == 1) {
|
---|
| 599 | // everything is ok; do nothing
|
---|
| 600 | } else {
|
---|
| 601 | // current index is invalid, set new one
|
---|
| 602 | this.ActualValueConfigurationIndex = this.valueConfigurations.CheckedItems.First().Index;
|
---|
| 603 | }
|
---|
| 604 | } else {
|
---|
| 605 | // no checked valueConfiguration is available; cannot be used
|
---|
| 606 | this.ActualValueConfigurationIndex = -1;
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
[5112] | 609 | }
|
---|
| 610 | }
|
---|