[or-tools](https://github.com/ankane/or-tools-ruby) allows to find the best opimized plan, see also [CP-SAT](https://github.com/google/or-tools/tree/stable/ortools/sat).