Changeset 11587 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
- Timestamp:
- 11/26/14 13:24:20 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11582 r11587 27 27 using HeuristicLab.Common; 28 28 using HeuristicLab.Core; 29 using HeuristicLab.Data;30 using HeuristicLab.Encodings.BinaryVectorEncoding;31 using HeuristicLab.Encodings.IntegerVectorEncoding;32 using HeuristicLab.Encodings.PermutationEncoding;33 29 using HeuristicLab.Encodings.RealVectorEncoding; 34 30 using HeuristicLab.Optimization; … … 93 89 Operators.Add(new SingleObjectiveAnalyzer()); 94 90 Operators.Add(Evaluator); 95 Operators.Add(SolutionCreator);96 91 97 92 RegisterEventHandlers(); … … 148 143 149 144 IEncoding encoding = definition.Encoding; 145 SolutionCreator = encoding.SolutionCreator; 150 146 151 147 EncodingParameter.Value = encoding; … … 168 164 DynamicEncodingParameters.Clear(); 169 165 170 var creator = ConfigureCreator(encoding); 171 166 DynamicEncodingParameters.AddRange(encoding.Parameters); 172 167 foreach (var param in DynamicEncodingParameters) { 173 168 param.Hidden = true; … … 175 170 } 176 171 177 var multiEncoding = encoding as MultiEncoding; 178 if (multiEncoding != null) { 179 ConfigureMultiVectorEncodingOperators((MultiEncodingCreator)creator, multiEncoding); 180 } else { 181 ConfigureSingleEncodingOperators(creator, encoding); 182 } 172 ConfigureSingleEncodingOperators(); 183 173 UpdateImprovementOperators(); 184 174 UpdateMoveOperators(); 185 175 } 186 176 187 protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) { 188 #region Configure MultiEncoding Creator 189 var multiEncoding = encoding as MultiEncoding; 190 if (multiEncoding != null) { 191 var creator = new MultiEncodingCreator(); 192 foreach (var enc in multiEncoding.Encodings) { 193 if (enc is MultiEncoding) throw new InvalidOperationException("MultiEncoding within a MultiEncoding is not supported."); 194 creator.Operators.Add(ConfigureCreator(enc)); 195 } 196 return creator; 197 } 198 #endregion 199 200 #region Configure RealVector Creator 201 var realEnc = encoding as RealEncoding; 202 if (realEnc != null) { 203 DynamicEncodingParameters.AddRange(realEnc.Parameters); 204 return realEnc.SolutionCreator; 205 } 206 #endregion 207 #region Configure BinaryVector Creator 208 var binEnc = encoding as BinaryEncoding; 209 if (binEnc != null) { 210 DynamicEncodingParameters.AddRange(binEnc.Parameters); 211 return binEnc.SolutionCreator; 212 } 213 #endregion 214 #region Configure IntegerVector Creator 215 var intEnc = encoding as IntegerEncoding; 216 if (intEnc != null) { 217 DynamicEncodingParameters.AddRange(intEnc.Parameters); 218 return intEnc.SolutionCreator; 219 } 220 #endregion 221 #region Configure Permutation Creator 222 var permEnc = encoding as PermutationEncoding; 223 if (permEnc != null) { 224 DynamicEncodingParameters.AddRange(permEnc.Parameters); 225 return permEnc.SolutionCreator; 226 } 227 #endregion 228 throw new ArgumentException(string.Format("Encoding {0} is unknown.", encoding != null ? encoding.GetType().FullName : "(null)")); 229 } 230 231 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) { 232 // remove all multiencoding operators 233 Operators.RemoveAll(x => x is MultiEncodingCrossover 234 || x is MultiEncodingManipulator 235 || x is MultiEncodingCreator); 236 237 #region Configure Operators for RealVectorEncoding 238 var realEncoding = encoding as RealEncoding; 239 if (realEncoding != null) { 240 // do not replace a real vector creator that was manually set 241 if (!(SolutionCreator is IRealVectorCreator) 242 || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realEncoding.Name) { 243 Operators.Remove(SolutionCreator); 244 SolutionCreator = newCreator; 245 } 246 247 foreach (var su in realEncoding.Operators.OfType<IRealVectorSwarmUpdater>()) { 248 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 249 } 250 } 251 #endregion 252 #region Configure Operators for BinaryVectorEncoding 253 var binEncoding = encoding as BinaryEncoding; 254 if (binEncoding != null) { 255 // do not replace a binary vector creator that was manually set 256 if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != binEncoding.Name) { 257 Operators.Remove(SolutionCreator); 258 SolutionCreator = newCreator; 259 } 260 } 261 #endregion 262 #region Configure Operators for IntegerVectorEncoding 263 var intEncoding = encoding as IntegerEncoding; 264 if (intEncoding != null) { 265 // do not replace an integer vector creator that was manually set 266 if (!(SolutionCreator is IIntegerVectorCreator) 267 || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intEncoding.Name) { 268 Operators.Remove(SolutionCreator); 269 SolutionCreator = newCreator; 270 } 271 } 272 #endregion 273 #region Configure Operators for PermutationEncoding 274 var permEncoding = encoding as PermutationEncoding; 275 if (permEncoding != null) { 276 // do not replace a permutation creator that was manually set 277 if (!(SolutionCreator is IPermutationCreator) 278 || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permEncoding.Name) { 279 Operators.Remove(SolutionCreator); 280 SolutionCreator = newCreator; 281 } 282 } else { 283 Operators.RemoveAll(x => x is IPermutationCrossover 284 || x is IPermutationManipulator 285 || x is IPermutationMultiNeighborhoodShakingOperator); 286 } 287 #endregion 288 } 289 290 protected virtual void ConfigureMultiVectorEncodingOperators(MultiEncodingCreator newCreator, MultiEncoding encoding) { 291 var newBinParams = new HashSet<string>(newCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName)); 292 var newIntParams = new HashSet<string>(newCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName)); 293 var newRealParams = new HashSet<string>(newCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName)); 294 var newPermParams = new HashSet<string>(newCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName)); 295 296 var oldCreator = SolutionCreator as MultiEncodingCreator; 297 if (oldCreator == null) SolutionCreator = newCreator; 298 else { 299 #region Configure BinaryVector Creator 300 var oldBinParams = new HashSet<string>(oldCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName)); 301 foreach (var toAdd in newBinParams.Except(oldBinParams)) { 302 oldCreator.Operators.Add(newCreator.Operators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == toAdd)); 303 } 304 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 305 var op = oldCreator.Operators.OfType<IBinaryVectorCreator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove); 306 if (op != null) oldCreator.Operators.Remove(op); 307 } 308 #endregion 309 310 #region Configure IntegerVector Creator 311 var oldIntParams = new HashSet<string>(oldCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName)); 312 foreach (var toAdd in newIntParams.Except(oldIntParams)) { 313 oldCreator.Operators.Add(newCreator.Operators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == toAdd)); 314 } 315 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 316 var op = oldCreator.Operators.OfType<IIntegerVectorCreator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove); 317 if (op != null) oldCreator.Operators.Remove(op); 318 } 319 #endregion 320 321 #region Configure RealVector Creator 322 var oldRealParams = new HashSet<string>(oldCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName)); 323 foreach (var toAdd in newRealParams.Except(oldRealParams)) { 324 oldCreator.Operators.Add(newCreator.Operators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == toAdd)); 325 } 326 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 327 var op = oldCreator.Operators.OfType<IRealVectorCreator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove); 328 if (op != null) oldCreator.Operators.Remove(op); 329 } 330 #endregion 331 332 #region Configure Permutation Creator 333 var oldPermParams = new HashSet<string>(oldCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName)); 334 foreach (var toAdd in newPermParams.Except(oldPermParams)) { 335 oldCreator.Operators.Add(newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == toAdd)); 336 } 337 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 338 var op = oldCreator.Operators.OfType<IPermutationCreator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove); 339 if (op != null) oldCreator.Operators.Remove(op); 340 } 341 // we also have to sync the permutation type (in case this changes, as it is a value parameter) 342 foreach (var intersect in newPermParams.Intersect(oldPermParams)) { 343 var oldPermCreator = oldCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect); 344 var newPermCreator = newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect); 345 oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value; 346 } 347 #endregion 348 349 } 350 351 // crossover and manipulator for multi-vector encoding 352 // the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator) 353 if (Operators.OfType<MultiEncodingCrossover>().Any() && Operators.OfType<MultiEncodingManipulator>().Any()) { 354 #region Update existing multi-vector encoding 355 #region Update ParameterVector Crossover ... 356 foreach (var oldXo in Operators.OfType<MultiEncodingCrossover>()) { 357 #region ... for binary parameters 358 var oldBinParams = new HashSet<string>(oldXo.Operators.OfType<IBinaryVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 359 foreach (var toAdd in newBinParams.Except(oldBinParams)) 360 oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, encoding)); 361 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 362 var op = oldXo.Operators.OfType<IBinaryVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 363 if (op != null) oldXo.Operators.Remove(op); 364 } 365 #endregion 366 #region ... for integer parameters 367 var oldIntParams = new HashSet<string>(oldXo.Operators.OfType<IIntegerVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 368 foreach (var toAdd in newIntParams.Except(oldIntParams)) 369 oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, encoding)); 370 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 371 var op = oldXo.Operators.OfType<IIntegerVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 372 if (op != null) oldXo.Operators.Remove(op); 373 } 374 #endregion 375 #region ... for real parameters 376 var oldRealParams = new HashSet<string>(oldXo.Operators.OfType<IRealVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 377 foreach (var toAdd in newRealParams.Except(oldRealParams)) 378 oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, encoding)); 379 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 380 var op = oldXo.Operators.OfType<IRealVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 381 if (op != null) oldXo.Operators.Remove(op); 382 } 383 #endregion 384 #region ... for permutation parameters 385 var oldPermParams = new HashSet<string>(oldXo.Operators.OfType<IPermutationCrossover>().Select(x => x.ChildParameter.ActualName)); 386 foreach (var toAdd in newPermParams.Except(oldPermParams)) 387 oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, encoding)); 388 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 389 var op = oldXo.Operators.OfType<IPermutationCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 390 if (op != null) oldXo.Operators.Remove(op); 391 } 392 #endregion 393 } 394 #endregion 395 #region Update ParameterVector Manipulator ... 396 foreach (var oldM in Operators.OfType<MultiEncodingManipulator>()) { 397 #region ... for binary parameters 398 var oldBinParams = new HashSet<string>(oldM.Operators.OfType<IBinaryVectorManipulator>().Select(x => x.BinaryVectorParameter.ActualName)); 399 foreach (var toAdd in newBinParams.Except(oldBinParams)) 400 oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, encoding)); 401 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 402 var op = oldM.Operators.OfType<IBinaryVectorManipulator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove); 403 if (op != null) oldM.Operators.Remove(op); 404 } 405 #endregion 406 #region ... for integer parameters 407 var oldIntParams = new HashSet<string>(oldM.Operators.OfType<IIntegerVectorManipulator>().Select(x => x.IntegerVectorParameter.ActualName)); 408 foreach (var toAdd in newIntParams.Except(oldIntParams)) 409 oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, encoding)); 410 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 411 var op = oldM.Operators.OfType<IIntegerVectorManipulator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove); 412 if (op != null) oldM.Operators.Remove(op); 413 } 414 #endregion 415 #region ... for real parameters 416 var oldRealParams = new HashSet<string>(oldM.Operators.OfType<IRealVectorManipulator>().Select(x => x.RealVectorParameter.ActualName)); 417 foreach (var toAdd in newRealParams.Except(oldRealParams)) 418 oldM.Operators.Add(GetDefaultRealManipulator(toAdd, encoding)); 419 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 420 var op = oldM.Operators.OfType<IRealVectorManipulator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove); 421 if (op != null) oldM.Operators.Remove(op); 422 } 423 #endregion 424 #region ... for permutation parameters 425 var oldPermParams = new HashSet<string>(oldM.Operators.OfType<IPermutationManipulator>().Select(x => x.PermutationParameter.ActualName)); 426 foreach (var toAdd in newPermParams.Except(oldPermParams)) 427 oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, encoding)); 428 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 429 var op = oldM.Operators.OfType<IPermutationManipulator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove); 430 if (op != null) oldM.Operators.Remove(op); 431 } 432 #endregion 433 } 434 #endregion 435 #endregion 436 } else { 437 #region Handle transition from single-vector to multi-vector encoding 438 Operators.RemoveAll(x => x is ICrossover); 439 Operators.RemoveAll(x => x is IManipulator); 440 Operators.RemoveAll(x => x is IStrategyParameterCreator || x is IStrategyParameterManipulator || x is IStrategyParameterCrossover); 441 Operators.RemoveAll(x => x is IParticleCreator); 442 Operators.RemoveAll(x => x is IParticleUpdater); 443 Operators.RemoveAll(x => x is ISwarmUpdater); 444 Operators.RemoveAll(x => x is IMultiNeighborhoodShakingOperator); 445 446 var crossover = new MultiEncodingCrossover(); 447 var manipulator = new MultiEncodingManipulator(); 448 foreach (var enc in encoding.Encodings) { 449 if (enc is BinaryEncoding) { 450 crossover.Operators.Add(GetDefaultBinaryCrossover(enc.Name, encoding)); 451 manipulator.Operators.Add(GetDefaultBinaryManipulator(enc.Name, encoding)); 452 continue; 453 } 454 var intConfig = enc as IntegerEncoding; 455 if (intConfig != null) { 456 crossover.Operators.Add(GetDefaultIntegerCrossover(enc.Name, encoding)); 457 manipulator.Operators.Add(GetDefaultIntegerManipulator(enc.Name, encoding)); 458 continue; 459 } 460 var realConfig = enc as RealEncoding; 461 if (realConfig != null) { 462 crossover.Operators.Add(GetDefaultRealCrossover(enc.Name, encoding)); 463 manipulator.Operators.Add(GetDefaultRealManipulator(enc.Name, encoding)); 464 continue; 465 } 466 var permConfig = enc as PermutationEncoding; 467 if (permConfig != null) { 468 crossover.Operators.Add(GetDefaultPermutationCrossover(enc.Name, encoding)); 469 manipulator.Operators.Add(GetDefaultPermutationManipulator(enc.Name, encoding)); 470 continue; 471 } 472 throw new InvalidOperationException("Unknown type for parameter " + enc.Name); 473 } 474 Operators.Add(crossover); 475 Operators.Add(manipulator); 476 #endregion 177 178 protected virtual void ConfigureSingleEncodingOperators() { 179 foreach (var su in GetOperators().OfType<IRealVectorSwarmUpdater>()) { 180 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 477 181 } 478 182 } … … 512 216 } 513 217 514 #region GetDefaultOperators for Crossovers and Manipulators 515 // ReSharper disable RedundantNameQualifier 516 protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, MultiEncoding config) { 517 var binConfig = (BinaryEncoding)config.Encodings[paramName]; 518 IBinaryVectorCrossover binXo; 519 if (binConfig.Length > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover(); 520 else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover(); 521 binXo.ChildParameter.ActualName = paramName; 522 binXo.ParentsParameter.ActualName = paramName; 523 return binXo; 524 } 525 526 protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, MultiEncoding config) { 527 var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator(); 528 binM.BinaryVectorParameter.ActualName = paramName; 529 binM.MutationProbabilityParameter.Value = new DoubleValue(0.1); 530 return binM; 531 } 532 533 protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, MultiEncoding config) { 534 var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaBetaCrossover(); 535 intXo.ChildParameter.ActualName = paramName; 536 intXo.ParentsParameter.ActualName = paramName; 537 intXo.BoundsParameter.ActualName = paramName + "Bounds"; 538 intXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 539 intXo.MaximizationParameter.ActualName = MaximizationParameter.Name; 540 return intXo; 541 } 542 543 protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, MultiEncoding configuration) { 544 var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator(); 545 intM.IntegerVectorParameter.ActualName = paramName; 546 intM.BoundsParameter.ActualName = paramName + "Bounds"; 547 intM.ProbabilityParameter.Value = new DoubleValue(0.1); 548 return intM; 549 } 550 551 protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, MultiEncoding configuration) { 552 var realXo = new Encodings.RealVectorEncoding.BlendAlphaBetaCrossover(); 553 realXo.ChildParameter.ActualName = paramName; 554 realXo.ParentsParameter.ActualName = paramName; 555 realXo.BoundsParameter.ActualName = paramName + "Bounds"; 556 realXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 557 realXo.MaximizationParameter.ActualName = MaximizationParameter.Name; 558 return realXo; 559 } 560 561 protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, MultiEncoding configuration) { 562 var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator(); 563 realM.RealVectorParameter.ActualName = paramName; 564 realM.BoundsParameter.ActualName = paramName + "Bounds"; 565 return realM; 566 } 567 568 protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, MultiEncoding configuration) { 569 var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover(); 570 permXo.ChildParameter.ActualName = paramName; 571 permXo.ParentsParameter.ActualName = paramName; 572 return permXo; 573 } 574 575 protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, MultiEncoding configuration) { 576 var permM = new Encodings.PermutationEncoding.Swap2Manipulator(); 577 permM.PermutationParameter.ActualName = paramName; 578 return permM; 579 } 580 // ReSharper restore RedundantNameQualifier 581 #endregion 218 582 219 } 583 220 }
Note: See TracChangeset
for help on using the changeset viewer.