# Galapagos Optimization

## Introduction

Expected Time: 90 min
Animation of Galapagos (click on image if animation not visible)

An important part of parametric design is not only setting up a script, but also optimizing the model for different goals. For example, the position of a house can be optimized to receive as much daylight as possible, or the thickness of a construction can be reduced to the utmost.

In this tutorial, you will learn the basics of optimization in grasshopper, using Galapagos. Galapagos is a tool created by David Rutten. Most technical information is based on his blog, which can be found by opening the Grasshopper component.

## The Design Problem

The toy you designed, to be packaged in a box as small as possible

Imagine the following scenario: after years of experimenting and designing, you have found the ideal toy for architecture students. Now you want to send a package including your prototype to several friends. Unfortunately, the costs of sending the product, is based on the size of the box. In the following chapters we will discuss a method to optimize the volume of the box, based on the orientation of the toy. Furthermore, we will also discuss most settings of Galapagos.

## Setting up the script

Create a model in Rhino

First create a model in Rhino. Set this model to a geometry parameter in Grasshopper.

• Design a model in Rhino

Rotate the object in three directions

Decide which transformations of the model in the box are allowed. In the case of this example, we will assume it is allowed to rotate the model in three directions: X, Y and Z. By connecting a XY, XZ and YZ plane to a Move component, we can do these translations.

• Rotate the model in three directions Transform » Euclidian » Rotate

Edit the Gene Pool settings

The available values will be defined by a Gene Pool. By double-clicking on the Gene Pool, you can setup the sliders. This process works similar to a standard Number Slider. Set the Gene Count to 3 and the decimals to 0. Set the maximum to 359, since that is the maximum amount of degrees to rotate.

• Add a Gene Pool Params » Input » Gene Pool
• Change the settings of the Gene Pool RMB on Gene Pool » Edit

Extract the Gene Pool parameters

Extract the values using a List Item component. Zoom in on the List Item Component and click on the plus icon to extract item +1 and +2.

• Extract the Gene Pool parameters Sets » List » List Item
• Click on the + icon twice

Connect the parameters and set to degrees

Connect the outputs to the different Move components. Also set the Angle to degrees by right-clicking on the Angle input.

• Connect the outputs to the Angle inputs
• Set the Angle inputs to degrees RMB on Angle » Degrees

Finish with a geometry parameter

Finish the script by adding a Geometry Parameter. This node indicates that this is the final result.

• Add a Geometry parameter Params » Geometry » Geometry

Create the box around the object(s)

Now we need to create a box around the object. Use a Bounding box to find the smallest possible box in a certain orientation. If you are using multiple separated objects. Right-click on the Bounding Box component and click on Union Box.

• Create a bounding box around the model Surface » Primitive » Bounding Box

Calculate the volume

Finally we have to calculate the volume of the box. Add a volume component and connect a panel to the Volume output to see the details.

• Calculate the volume of the bounding box Surface » Analysis » Volume
• Connect a panel to the Volume output Params » Input » Panel

## Initiating Galapagos

Just like other components, add the Galapagos node to the canvas.

• Add Galapagos to the canvas Params » Util » Galapagos

Fitness input

Galapagos has two inputs: the Genome and the Fitness. A fitness value can be described as the value you want to optimize, in this case the volume. The Genome is a collection of parameters that influence the Fitness. For this tutorial: the rotation in the three different directions.

Since we have set our Gene Pool to 0 digits, each slider has 360 degrees of freeform. In total, this results in 360 * 360 * 360 = 46.656.000 possible outcomes to our script. It would be impossible to try all options ourselves. Therefore, we use Galapagos to find an approximation to the best solution fairly efficiently.

Connect the Galapagos wires

Connecting the wires to Galapagos works a bit different than normal components. You always have to connect the wire from the Galapagos input to the parameters. Connect the Genome to the Gene Pool and the Fitness to the Volume output.

• Connect the fitness to the Volume output
• Connect the Genome tot the Gene Pool

The Galapagos editor

Double-Click on the Galapagos component. The Galapagos Editor is now opened.

• Open the Galapagos editor Select Galapagos node » Double Click on icon

## Galapagos Initial Settings

In this chapter, we will discuss the different initial settings before you run the Galapagos optimization.

Fitness/threshold Defines if you want to maximize or minimize the value. By setting a threshold, you can define the fitness that should be found. If the algorithm finds a solution with this threshold, it will stop. If you leave the threshold empty, the algorithm will continue indefinitely, until it has tried all options, or reaches a time limit.

• Set the Fitness to minimize

Runtime Limit By setting a time limit, you can define the maximum time the model is allowed to run.

• Do not enable the Runtime Limit

Evolutionary solver versus Annealing solver

Galapagos has two types of optimization algorithms: evolutionary and annealing. Generally speaking, it is advised to use the evolutionary solver. The evolutionary solver searches for a good result and then optimizes it making small changes to the parameter. However, in some cases, a script is built with a great degree of freedom which may lead to unexpected bad results in the evolutionary solver. Therefore you can use the annealing solver. This tutorial would dive in too deep, to completely explain all the details and differences of the solvers. If you are interested in this topic, take a look at this paper of David Rutten.

The following settings only apply to the evolutionary solver:

Max. stagnant The maximum amount of generations that do not lead to a more optimized result, before the solver should stop.

Population How many options the algorithm should try before it progresses to the next optimized result.

Initial Boost The initial boost can lead to better results in cases where:

• The model only has specific local parameter value combination that lead to great results
• The model has high degrees of freedom; and lots of possible combinations
• Leave the Initial Boost to 2 for now. Feel free to experiment with different values.

Maintain The amount of results that should be combined every generation to find new better results.

Inbreeding The freedom factor of the algorithm to use very similar or very different genes to breed with. A high positive factor means that the algorithm will only use genes with very similar results. A high negative factor states that the algorithm is allowed to use very different genes to combine.

The following settings only apply to the annealing solver:

Temperature The chance that an iteration jumps to a completely different combination of parameter values to check if it works better.

Cooling The factor to lower the temperature with at each jump to another combination of parameter values.

Drift rate The chance that a parameter value is changed. If the drift rate is 0%, only one parameter will be changed every iteration.

Since we are using the evolutionary algorithm, you can leave the annealing settings as they are. Click on OK.

• Click on OK

Turn off the preview of irrelevant components

Before starting the algorithm, make sure you turned off all previews of nodes that are not relevant. In our case, only enable the Geometry parameter node and the Bounding Box component. After that, open the Galapagos editor again and click on the solvers.

• Open the Galapagos Editor Select Galapagos node » Doubleclick on icon
• Click on the solvers tab

Turn all genes display on

In the top shelf, you can specify several settings. The first two icons define the algorithm type you are using. Next to that, you can start the solver by clicking on Start Solver. At the end, there are some options to display intermediate results in the Rhino viewport. For fast and efficient calculations, it makes sense to disable the display. However, for this example, we will turn on the display of all genes, by clicking on the first clock icon. Finally click on OK.

• Turn on the display of all intermediate genes

Start the solver

Now click on Start Solver. You will see that Galapagos changes the parameter settings of your model and displays different results.

• Click on Start Solver

## Interpreting the results

Run the algorithm for a while and stop it

After running the algorithm for a while, you can Stop the Solver.

• Run the algorithm until the changes between iterations are minimized

The bottom right diagram shows some of the best solutions. Select the top result and click on reinstate. The result should now be visible in the viewport. If you are interested, you can also check other solutions.

• Select the top Gene
• Click on reinstate

If you are still not satisfied with the solution. Select one of the genes and click on the little triangle next to the Start Solver button. By clicking on Start from selected Genome, you can continue the algorithm using your preferred gene.

• Click on the triangle next to Start Solver
• Click on Start from selected Genome

For more information about the other diagrams, take a look at the blogs and papers of David Rutten.

## Multi-Fitness optimization

Unfortunately, Galapagos is not able to optimize multiple fitness values. Imagine that not only the volume of your box is increasing the costs, but also the amount of box material (the area of the bounding box). In that case, we will need to change our Grasshopper script.

Add the Area and Volume value together

One solution would be to add the Area of the Bounding box to the Volume value. However, this would mean that the fitness values are treated equally.

• Calculate the area of the object Surface » Analysis » Area
• Change the fitness to the new calculated sum

Multiply the area by factor

On the other hand, you could create a formula for the fitness value. For example, multiply the Area value with 0.25. Now the volume fitness is influencing the algorithm four times stronger than the area of the box.

• Multiply the area by a factor Maths » Operators » Multiplication

Use the weighted average

Finally, you could solve the problem by using a weighted average. Connect the fitness values to a weighted average input. Define the weight factors by connecting the same amount of number sliders, or use a gene pool. The arithmetic mean can be used as Fitness for Galapagos.

• Calculate a weighted product Maths » Util » Weighted Average
• Define the weights with a Gene Pool Params » Input » Gene Pool
• Change the fitness to the new calculated value

Wallacei multi-objective optimization

There are two great plugins available for multi-fitness optimization in Grasshopper to deal with more complex situations: Wallacei and Octopus. You can find them on Food4Rhino.

## General tips

Use the data recorder to save intermediate Gene combinations

Use these tips to make your Galapagos optimization run smoothly:

• Simplify (not related to data tree editing) your script as much as possible
• Minimize the degrees of freedom by setting limits for your gene sliders
• Do not display data in the Rhino viewport that is not relevant
• Disable parts of the script you are not using
• If you are running complex definitions, for example sun optimizations, take your time! It is not unusual that your script has to run for several hours.
• Connect a data recorder after your Genome sliders, to store results while running Galapagos
• Disable autosave in your Grasshopper settings temporarily