Discrete Variable for Optimo:
In order to show how to set discrete variables for Optimo we will create a simple example that tries to find the string with minimum length from a list of strings. The list of strings for this case is shown below:
We need to update our decision variables to address the index of items in this list. As it can be seen from the image above, the index of strings in the list is between 0 and 9. Therefore, we set the lower limit variable to 0 and the upper limit variable to 10 (highest index + 1). This way, the generated numbers from NSGA-II.InitialSolutionList node will be 0 < x < 10 as shown below.
Then, if we round these numbers down to integers (using the Math.Floor node in dynamo), the numbers will indicate item indexes in the list of strings that we have. We will floor the numbers in the fitness function custom node, but just for visualization I added the same node after the NSGA-II. InitialSolutionList node to show you what the result will look like.
Let’s create our fitness function custom node now. The fitness function will look like the image below from outside the node. The node will have two inputs:
1)
List of numbers that is used for the result of NSGA-II.InitialSolutionList node results and then mapped by Function.Apply node as a function
2)
Discrete VarList - list of initial strings.
Now, our code will look like the image below in Dynamo.
Inside the fitness function, we need to get the first item from the initial population list using List.FirstItem. Then, we floor the values to change them to integers. These integers can be used as indexes to get the items from our list of strings (from Discrete VarList) using the List.GetItemATIndex node. When we have the items from the list, we can measure the length of the strings using the String.Length node and then report the values. In this example, the string lengths are reported as our fitness values. The image below shows the inside of the fitness function node.
As we can see in our list of strings, two items (“A” and “Z”) have the minimum length (length = 1). Therefore, we expect that the result of the optimization merges toward these two items at the end. I added a few nodes to visualize the result of optimization. The image blow shows the final graph.
As you can see, after 10 generations, the list of final results only includes “A” and “Z” as the shortest strings from the list. You can try the same process with more complex examples.