I also doubt that it's optimal. An implementation using Hierarchical Risk Parity for instance would be more interesting. I assume you can model risk/uncertainty here.
By the way, Google has an OR tools framework that implements these kind of solvers for you: