Changes between Version 1 and Version 2 of Documentation/Howto/DefineCustomProblems
- Timestamp:
- 05/29/11 21:34:08 (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Documentation/Howto/DefineCustomProblems
v1 v2 9 9 Let's name this problem "My Knapsack Problem" and it should look like in the following screenshot. 10 10 11 [[Image(2-Initial-Problem-View.png)]] 11 12 12 13 Next, we'll need to define the parameters that are important to this problem. The knapsack problem is defined by: … … 16 17 We can represent the items as two vectors of double values that denote the items' weights and values and the size as a double value. So we add these as `ValueParameters` to our problem by clicking on the yellow plus icon as shown in the following screenshot. 17 18 18 The "Create Parameter" dialog pops up and asks for the kind of parameters to create. We'll choose `ValueParameter<T>`, name it "Weights", add a description and when we click on the '''T''' we choose `DoubleArray` in the Type Selector dialog that pops up. The dialog window should look as follows before we click "OK". 19 [[Image(3-Add-Parameters.png)]] 19 20 21 The "Create Parameter" dialog pops up and asks for the kind of parameters to create. We'll choose `ValueParameter<T>` (1), name it "Weights" (4), add a description and when we click on the '''T''' (2) and on the button to select a type (3). We choose `DoubleArray` in the Type Selector dialog that pops up. The dialog window should look as follows before we click "OK". 22 23 [[Image(4-Create-Parameter.png)]] 20 24 21 25 Now we can see that the parameter has been added to the problem's parameter list. Any operator with a `LookupParameter<DoubleArray>` that also has Weights as its !ActualName is now able to find this parameter and obtain its value. In a similar fashion we add another parameter called "Values", and another one called "Size" which however is just a single real value so this time create a `ValueParameter<DoubleValue>`. Our problem now looks as in the following screenshot. 22 26 27 [[Image(5-Problem-View-With-Parameters.png)]] 23 28 24 29 Now it's time to fill the parameters, so to say to define an instance of our problem. You can choose your own values, I chose to make a knapsack problem with 5 items, so I put 5 as the length of my !DoubleArrays and then choose [ 1; 1; 3; 2; 2 ] for the weights and [ 3; 1; 5; 4; 1 ] for the values. For the size of the knapsack I chose 4. The problem is small and usually not worth to optimize, but it is suited in a tutorial like this. As you may have already calculated, there are two best solutions and the quality is 8. We could enter this information in the problem's !BestKnownQuality and !BestKnownSolution parameters, but let's have the algorithm find that out. 25 30 31 The problem definition is done, but we still need to define the encoding and finally write the evaluation function before we can start the optimization. 32 26 33 == Encoding == 34 35 The knapsack problem can be represented as a 0-1 decision problem, which means for every item we can either choose not to take it (0) or to include it (1). Such kind of problems can be represented by using a Boolean vector that is as large as the number of items. Our solution thus consists of a !BoolArray. 36 37 Select the !SolutionCreator parameter in the problem and set it to a "`RandomBinaryVectorCreator`" which can be found under !HeuristicLab.Encodings.!BinaryVectorEncoding. You'll see that it has a parameter called "Length" which we set to a new !IntValue that has the value 5 or whatever you choose to be the length of the Weights and Values parameters. There are some other parameters, probably hidden, which you can see by clicking on the "Show Hidden Parameters" button. We'll notice that it has a parameter called "!BinaryVector" which is the name of the solution. Now we're able to create random solutions to our problem. The `RandomBinaryVectorCreator` will do that for us in that it creates Boolean vectors of our specified length. 38 39 To optimize problems with a genetic algorithm however we also need a crossover operator and often also a mutation operator. We add them to the !ItemList of Operators. Click the "Operators" parameter and add the `SinglePointCrossover` and the `SinglePositionBitflipManipulator` from the !HeuristicLab.Encodings.!BinaryVectorEncoding namespace. Finally also add an analyzer so that we can track the best found solution, click the yellow plus icon on the Operators list once again and add the `BestScopeSolutionAnalyzer` from the !HeuristicLab.Analysis namespace. Our Operators list looks as follows. 40 41 [[Image(6-Operators-ItemList.png)]] 42 43 Now we have defined the encoding in that we chose a !SolutionCreator and operators for manipulating it. 44 45 == Evaluation Function == 46