1 | using System.Collections.Generic;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.PluginInfrastructure;
|
---|
7 | using HeuristicLab.Problems.MetaOptimization;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using System;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.MetaOptimization.Test {
|
---|
12 | class Program {
|
---|
13 | static void Main(string[] args) {
|
---|
14 | //TestIntSampling();
|
---|
15 | //TestDoubleSampling();
|
---|
16 |
|
---|
17 | GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
|
---|
18 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
19 | metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
|
---|
20 | IValueConfiguration algorithmVc = metaOptimizationProblem.AlgorithmParameterConfiguration;
|
---|
21 |
|
---|
22 | //ConfigurePopulationSize(algorithmVc);
|
---|
23 | ConfigureMutationRate(algorithmVc);
|
---|
24 |
|
---|
25 | GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
|
---|
26 | metaLevelAlgorithm.PopulationSize.Value = 10;
|
---|
27 |
|
---|
28 | // set random values
|
---|
29 | for (int i = 0; i < 10; i++) {
|
---|
30 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
31 | clonedVc.Randomize();
|
---|
32 | clonedVc.Parameterize();
|
---|
33 | GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
|
---|
34 | //Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
35 | Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
36 | }
|
---|
37 |
|
---|
38 | // mutate
|
---|
39 | for (int i = 0; i < 10; i++) {
|
---|
40 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
41 | clonedVc.Mutate();
|
---|
42 | clonedVc.Parameterize();
|
---|
43 | GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
|
---|
44 | //Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
45 | Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
46 | }
|
---|
47 |
|
---|
48 | // cross
|
---|
49 | for (int i = 0; i < 10; i++) {
|
---|
50 | IValueConfiguration clonedVc1 = (IValueConfiguration)algorithmVc.Clone();
|
---|
51 | clonedVc1.Randomize();
|
---|
52 | clonedVc1.Parameterize();
|
---|
53 |
|
---|
54 | IValueConfiguration clonedVc2 = (IValueConfiguration)algorithmVc.Clone();
|
---|
55 | GeneticAlgorithm first = (GeneticAlgorithm)clonedVc1.ActualValue.Value;
|
---|
56 | GeneticAlgorithm second = (GeneticAlgorithm)clonedVc2.ActualValue.Value;
|
---|
57 |
|
---|
58 | var popSizeBefore = first.PopulationSize.Value;
|
---|
59 | var mutRateBefore = first.MutationProbability.Value;
|
---|
60 | clonedVc1.Cross(clonedVc2);
|
---|
61 | clonedVc1.Parameterize();
|
---|
62 |
|
---|
63 | //Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
|
---|
64 | Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
|
---|
65 | }
|
---|
66 |
|
---|
67 | Debugger.Break();
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static void ConfigurePopulationSize(IValueConfiguration algorithmVc) {
|
---|
71 | var populationSizePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
|
---|
72 | populationSizePc.Optimize = true;
|
---|
73 | var populationSizeVc = populationSizePc.ValueConfigurations.First();
|
---|
74 | populationSizeVc.Optimize = true;
|
---|
75 | populationSizeVc.RangeConstraint.LowerBound = new IntValue(0);
|
---|
76 | populationSizeVc.RangeConstraint.UpperBound = new IntValue(100);
|
---|
77 | populationSizeVc.RangeConstraint.StepSize = new IntValue(1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static void ConfigureMutationRate(IValueConfiguration algorithmVc) {
|
---|
81 | var mutationRatePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
|
---|
82 | mutationRatePc.Optimize = true;
|
---|
83 | var mutationRateVc = mutationRatePc.ValueConfigurations.First();
|
---|
84 | mutationRateVc.Optimize = true;
|
---|
85 | mutationRateVc.RangeConstraint.LowerBound = new PercentValue(0.0);
|
---|
86 | mutationRateVc.RangeConstraint.UpperBound = new PercentValue(1.0);
|
---|
87 | mutationRateVc.RangeConstraint.StepSize = new PercentValue(0.01);
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static void TestIntSampling() {
|
---|
91 | System.Random rand = new System.Random();
|
---|
92 | int lower = 10;
|
---|
93 | int upper = 20;
|
---|
94 | int stepsize = 7;
|
---|
95 | for (int i = 0; i < 100; i++) {
|
---|
96 | int val;
|
---|
97 | do {
|
---|
98 | val = rand.Next(lower / stepsize, upper / stepsize + 1) * stepsize;
|
---|
99 | } while (val < lower || val > upper);
|
---|
100 | Console.WriteLine(val);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static void TestDoubleSampling() {
|
---|
105 | System.Random rand = new System.Random();
|
---|
106 | double lower = 2;
|
---|
107 | double upper = 3;
|
---|
108 | double stepsize = 0.6;
|
---|
109 | for (int i = 0; i < 100; i++) {
|
---|
110 | double val;
|
---|
111 | do {
|
---|
112 | val = Math.Round((rand.NextDouble() * (upper - lower) + lower) / stepsize, 0) * stepsize;
|
---|
113 | } while (val < lower || val > upper);
|
---|
114 | Console.WriteLine(val);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private static IEnumerable<IItem> GetValidValues(IValueParameter valueParameter) {
|
---|
119 | return ApplicationManager.Manager.GetInstances(valueParameter.DataType).Select(x => (IItem)x).OrderBy(x => x.ItemName);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|