Changeset 11880
- Timestamp:
- 02/04/15 00:03:14 (8 years ago)
- Location:
- branches/ProgrammableProblem
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/ProblemDefinitionScriptView.Designer.cs
r11398 r11880 142 142 this.Controls.Add(this.splitContainer2); 143 143 this.Name = "ProblemDefinitionScriptView"; 144 this.Controls.SetChildIndex(this. compilationLabel, 0);144 this.Controls.SetChildIndex(this.infoTextLabel, 0); 145 145 this.Controls.SetChildIndex(this.compileButton, 0); 146 146 this.Controls.SetChildIndex(this.splitContainer2, 0); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11753 r11880 52 52 encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); 53 53 54 T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ;55 if (newSolutionCreator == null) newSolutionCreator =encodingOperators.OfType<T>().First();54 T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ?? 55 encodingOperators.OfType<T>().First(); 56 56 SolutionCreator = newSolutionCreator; 57 57 OnOperatorsChanged(); … … 83 83 [StorableConstructor] 84 84 protected Encoding(bool deserializing) : base(deserializing) { } 85 86 85 protected Encoding(Encoding<T> original, Cloner cloner) 87 86 : base(original, cloner) { 88 encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone) );87 encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone), new TypeEqualityComparer<IOperator>()); 89 88 solutionCreator = cloner.Clone(original.solutionCreator); 90 89 } … … 97 96 public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); } 98 97 public abstract void ConfigureOperators(IEnumerable<IOperator> operators); 99 100 98 101 99 public event EventHandler SolutionCreatorChanged; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiObjectiveProblemDefinition.cs
r11739 r11880 27 27 bool[] Maximization { get; } 28 28 double[] Evaluate(Individual individual, IRandom random); 29 void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results );29 void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random); 30 30 } 31 31 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/ISingleObjectiveProblemDefinition.cs
r11753 r11880 28 28 bool Maximization { get; } 29 29 double Evaluate(Individual individual, IRandom random); 30 void Analyze(Individual[] individuals, double[] qualities, ResultCollection results );30 void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random); 31 31 IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random); 32 32 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/internal/IMultiObjectiveAnalysisOperator.cs
r11739 r11880 21 21 22 22 using System; 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Optimization; 24 25 25 26 namespace HeuristicLab.Problems.Programmable { 26 27 internal interface IMultiObjectiveAnalysisOperator : IEncodingOperator, IAnalyzer { 27 Action<Individual[], double[][], ResultCollection > AnalyzeAction { get; set; }28 Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 28 29 } 29 30 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs
r11739 r11880 21 21 22 22 using System; 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Optimization; 24 25 25 26 namespace HeuristicLab.Problems.Programmable { 26 27 internal interface ISingleObjectiveAnalysisOperator : IEncodingOperator { 27 Action<Individual[], double[], ResultCollection > AnalyzeAction { get; set; }28 Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 28 29 } 29 30 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/BasicProblem.cs
r11814 r11880 139 139 140 140 var operators = oldOperators.Intersect(newOperators, comparer).Select(op => cloner.Clone(op)); 141 operators = operators.Union(newOperators, comparer) ;141 operators = operators.Union(newOperators, comparer).ToList(); 142 142 143 143 newEncoding.ConfigureOperators(operators); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveBasicProblem.cs
r11814 r11880 57 57 public abstract bool[] Maximization { get; } 58 58 public abstract double[] Evaluate(Individual individual, IRandom random); 59 public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results ) { }59 public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { } 60 60 61 61 protected override void OnEncodingChanged() { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveProgrammableProblem.cs
r11814 r11880 50 50 try { 51 51 ProblemScript.Compile(); 52 } 53 catch (InvalidOperationException) { 52 } catch (InvalidOperationException) { 54 53 //Compilation error 55 54 } … … 72 71 try { 73 72 ProblemScript.Compile(); 74 } 75 catch (InvalidOperationException) { 73 } catch (InvalidOperationException) { 76 74 //Compilation error 77 75 } … … 96 94 } 97 95 98 public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results ) {99 ProblemDefinition.Analyze(individuals, qualities, results );96 public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { 97 ProblemDefinition.Analyze(individuals, qualities, results, random); 100 98 } 101 99 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/OneMaxNew.cs
r11814 r11880 55 55 } 56 56 57 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results ) {58 base.Analyze(individuals, qualities, results );57 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 58 base.Analyze(individuals, qualities, results, random); 59 59 var best = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderByDescending(z => z.Quality).First(); 60 60 if (!results.ContainsKey("Best Solution")) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs
r11797 r11880 60 60 } 61 61 62 void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results ) {63 CompiledProblemDefinition.Analyze(individuals, qualities, results );62 void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { 63 CompiledProblemDefinition.Analyze(individuals, qualities, results, random); 64 64 } 65 65 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs
r11767 r11880 76 76 inst.Initialize(); 77 77 CompiledProblemDefinition = inst; 78 } 79 catch { 78 } catch (Exception e) { 80 79 compiledProblemDefinition = null; 80 throw; 81 81 } 82 82 return assembly; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.cs
r11797 r11880 60 60 } 61 61 62 void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results ) {63 CompiledProblemDefinition.Analyze(individuals, qualities, results );62 void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 63 CompiledProblemDefinition.Analyze(individuals, qualities, results, random); 64 64 } 65 65 IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/Templates/CompiledMultiObjectiveProblemDefinition.cs
r11786 r11880 14 14 15 15 public override void Initialize() { 16 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 16 17 // Define the solution encoding which can also consist of multiple vectors, examples below 17 18 //Encoding = new BinaryEncoding("b", length: 5); … … 19 20 //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0); 20 21 //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute); 21 22 // The encoding can also be a combination 22 23 //Encoding = new MultiEncoding() 23 24 //.Add(new BinaryEncoding("b", length: 5)) … … 26 27 //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute)) 27 28 ; 29 // Add additional initialization code e.g. private variables that you need for evaluating 28 30 } 29 31 30 32 public double[] Evaluate(Individual individual, IRandom random) { 33 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 31 34 var qualities = new[] { 0.0, 0.0 }; 32 // use vars.yourVariable to access variables in the variable store i.e. yourVariable 33 // qualities = new [] { individual.RealVector("r").Sum(x => x * x), individual.RealVector("r").Sum(x => x * x * x) }; 35 //qualities = new [] { individual.RealVector("r").Sum(x => x * x), individual.RealVector("r").Sum(x => x * x * x) }; 34 36 return qualities; 35 37 } 36 38 37 public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results ) {38 // write or update results given the range of vectors and resulting qualities39 // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable39 public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { 40 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 41 // Write or update results given the range of vectors and resulting qualities 40 42 } 41 // implement further classes and methods43 // Implement further classes and methods 42 44 } 43 45 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/Templates/CompiledSingleObjectiveProblemDefinition.cs
r11753 r11880 14 14 15 15 public override void Initialize() { 16 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 16 17 // Define the solution encoding which can also consist of multiple vectors, examples below 17 18 //Encoding = new BinaryEncoding("b", length: 5); 18 //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 4);19 //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 2); 19 20 //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0); 20 21 //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute); 21 22 // The encoding can also be a combination 22 23 //Encoding = new MultiEncoding() 23 24 //.Add(new BinaryEncoding("b", length: 5)) … … 25 26 //.Add(new RealEncoding("r", length: 5, min: -1.0, max: 1.0)) 26 27 //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute)) 27 //; 28 ; 29 // Add additional initialization code e.g. private variables that you need for evaluating 28 30 } 29 31 30 32 public double Evaluate(Individual individual, IRandom random) { 33 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 31 34 var quality = 0.0; 32 // use vars.yourVariable to access variables in the variable store i.e. yourVariable33 35 //quality = individual.RealVector("r").Sum(x => x * x); 34 36 return quality; 35 37 } 36 38 37 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { 38 // write or update results given the range of vectors and resulting qualities 39 // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable 39 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 40 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 41 // Write or update results given the range of vectors and resulting qualities 42 // Uncomment the following lines if you want to retrieve the best individual 43 //var bestIndex = Maximization ? 44 // qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1 45 // : qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1; 46 //var best = individuals[bestIndex]; 40 47 } 41 48 42 49 public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { 50 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 43 51 // Create new vectors, based on the given one that represent small changes 44 // This method is only called from move-based algorithms (Local Search, SimulatedAnnealing, etc.)52 // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.) 45 53 while (true) { 46 // this is not an infinite loop as only a finite amount of samples will be drawn47 // it is possible to return a concrete amount of neighbors also54 // Algorithm will draw only a finite amount of samples 55 // Change to a for-loop to return a concrete amount of neighbors 48 56 var neighbor = individual.Copy(); 49 // e.g. make a bitflip in a binary parameter57 // For instance, perform a single bit-flip in a binary parameter 50 58 //var bIndex = random.Next(neighbor.BinaryVector("b").Length); 51 59 //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex]; … … 54 62 } 55 63 56 // implement further classes and methods64 // Implement further classes and methods 57 65 } 58 66 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveBasicProblem.cs
r11814 r11880 66 66 public abstract bool Maximization { get; } 67 67 public abstract double Evaluate(Individual individual, IRandom random); 68 public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results ) { }68 public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { } 69 69 public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { 70 70 return Enumerable.Empty<Individual>(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs
r11814 r11880 51 51 try { 52 52 ProblemScript.Compile(); 53 } 54 catch (InvalidOperationException) { 53 } catch (InvalidOperationException) { 55 54 //Compilation error 56 55 } … … 75 74 try { 76 75 ProblemScript.Compile(); 77 } 78 catch (InvalidOperationException) { 76 } catch (InvalidOperationException) { 79 77 //Compilation error 80 78 } … … 97 95 } 98 96 99 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results ) {100 ProblemDefinition.Analyze(individuals, qualities, results );97 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 98 ProblemDefinition.Analyze(individuals, qualities, results, random); 101 99 } 102 100 public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs
r11739 r11880 13 13 [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")] 14 14 [StorableClass] 15 public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator {15 public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator, IStochasticOperator { 16 16 public bool EnabledByDefault { get { return true; } } 17 17 … … 28 28 } 29 29 30 public Action<Individual[], double[][], ResultCollection> AnalyzeAction { get; set; } 30 public ILookupParameter<IRandom> RandomParameter { 31 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 32 } 33 34 public Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 31 35 32 36 [StorableConstructor] … … 47 51 var encoding = EncodingParameter.ActualValue; 48 52 var results = ResultsParameter.ActualValue; 53 var random = RandomParameter.ActualValue; 49 54 50 55 IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope }; … … 53 58 54 59 var individuals = scopes.Select(encoding.GetIndividual).ToArray(); 55 AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results );60 AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results, random); 56 61 return base.Apply(); 57 62 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs
r11739 r11880 13 13 [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")] 14 14 [StorableClass] 15 public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer {15 public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer, IStochasticOperator { 16 16 public bool EnabledByDefault { get { return true; } } 17 17 … … 28 28 } 29 29 30 public Action<Individual[], double[], ResultCollection> AnalyzeAction { get; set; } 30 public ILookupParameter<IRandom> RandomParameter { 31 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 32 } 33 34 public Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 31 35 32 36 [StorableConstructor] … … 37 41 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 38 42 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to.")); 43 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 39 44 } 40 45 … … 46 51 var encoding = EncodingParameter.ActualValue; 47 52 var results = ResultsParameter.ActualValue; 53 var random = RandomParameter.ActualValue; 48 54 49 55 IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope }; … … 52 58 53 59 var individuals = scopes.Select(encoding.GetIndividual).ToArray(); 54 AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results );60 AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random); 55 61 return base.Apply(); 56 62 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs
r11739 r11880 34 34 [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")] 35 35 [StorableClass] 36 public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, I SingleObjectiveEvaluationOperator, IStochasticOperator {36 public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator { 37 37 public ILookupParameter<IRandom> RandomParameter { 38 38 get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
Note: See TracChangeset
for help on using the changeset viewer.