Using kangaroo2 or Galapagos in GhPython

Hi all,
I am looking for some examples of using Kangaroo2 or Galapagos within GhPython. I wonder if it is possible and if there is anyone has done this before?
Thanks for any hints,

@AndersDeleuran has some great examples on his GitHub.

I don’t know whether it is possible to integrate Galapagos in GHPython, however genetic algorithms are not that hard to implement once you understand the basic principals.

Daniel Shiffman aka The Coding Train has some great explanations on his YouTube channel:
https://www.youtube.com/results?search_query=coding+train+genetic+algorithm
You could do a Python translation from his P5.js or Processing code.

If you want to inspect a Pythonic example, you can check out my attempt here:

3 Likes

They are pretty old though, but should get you going. Note that I’ve been importing the KangarooSolver namespace like so :

import Grasshopper as gh
import clr
clr.AddReferenceToFileAndPath(gh.Folders.PluginFolder+"Components\KangarooSolver.dll")
import KangarooSolver as ks

Since Kangaroo2 became a first-party plugin. It’s pretty neat in that one does not need to add paths etc. And everything will “just work” when you issue definitions at work/workshops.

2 Likes

Cool, thanks, I wonder if using Galapagos in GhPython is also possible?

I’ve edited my reply above with some suggestions about that.

Probably not without substantial workarounds/hackery, as it was not designed to be implemented in scripts (i.e. unlike Kangaroo2). In addition to writing your own genetic algorithm, you might also consider implementing a .NET library such as Accord or AForge (I forget which one is preferable). I once implemented the latter in GHPython without much trouble (based on VB examples developed by @MateuszZwierzycki):

2 Likes

Thanks for the information, diff-arch

Thanks AndersDeleuran, if it is not possible to use both Kangeroo2 and Galapagos within GhPython. I may change my question to be ----- if it is possible to run both Kangaroo2 and Galapagos on GH canvas at the same time? Does anyone done this before?

Yes, the Kangaroo zombie solver is intended for such pipelines.

I’ve tested one of your Kangaroo Python files from GitHub and all I had to change to make it work in Rhino 7, were a couple of lines on top:

import sys
import clr
sys.path.append("/Applications/Rhino 7.app/Contents/Frameworks/RhCore.framework/Versions/A/Resources/ManagedPlugIns/GrasshopperPlugin.rhp/Components/")
clr.AddReferenceToFile("KangarooSolver.dll")

import KangarooSolver as ks

It could be that this is only necessary on macOS though. Your script has otherwise stood the test of time!

1 Like

Thanks AndersDeleuran, will try Kangaroo zombie.

1 Like

Got it, thanks diff_arch.

@diff-arch already posted a working solution but if you want components to work on win & osx as well as in Rhino 6 and 7, you can try this snippet I wrote some time ago:

Just copy paste on top of your GHPython components as with the other solution :slight_smile:

2 Likes

Thanks Max

I also managed to get this example to run by just adding the correct reference. However the other examples do not run. In example file “StepByStep” I get the following error message:

Runtime error (ArgumentTypeException): expected IGoal, got Spring

Traceback:
line 24, in script

This could be a trivial error since my experience with Python and Kangaroo scripting is limited, but I guess that this has to do something with how goals are written in Kangaroo and Kangaroo2.

@AndersDeleuran or @diff-arch do you have an idea of how to solve this?
Again, sorry if I am asking something super trivial here! But as I said, I am just getting into scripting with Kangaroo2.

Also - is there a documentation of the namespace somewhere?

Cheers,
David

@DanielPiker might be able to answer this for you. I’m not sure.

It seems to work okay for me, after replacing the import code with the snippet I posted above:

220508_StepByStep.gh (51.2 KB)

you are right, it does work. However now I even got it to work with the original code, strange…
Sorry for that!

Last thing - is there a documentation somewhere where I find all methods and functions?

No worries. I don’t think there is much documentation outside of the C# source code itself, which is pretty accessible through GHPython intellisense and the print() , type() , help() , dir() functions.

Also, I just uploaded some additional examples from our Smart Geometry 2016 and Advances in Architectural Geometry 2016 workshops. These were updated to work natively in Rhino 7 for a workshop at CITA in 2020 and are exported from this larger GHPython pipeline:

I’ll upload the whole thing to GitHub some time soon. But hopefully these additional examples should get you going. There’s also a lot of K2Engineering in there, which is probably relevant too I imagine :snake:

Edit: Also, a lot of the goals are open source and Daniel has uploaded this document.

1 Like

This is great, thank you so much! Now I should have enough material to stop bothering you for some weeks :wink:

EDIT: Clearly I stopped bother you an hour and not “some weeks”.

Cheers,
David

1 Like