@1010011010 I playing with this since 2000 (and earlier) .. so ... have a look at
latex-project.org/papers/xo-pfloat.pdf basically in full generality (ie automatic placement as well as balancing etc) the problem is simply unsolved and by now that is starting to get recognized in the DocEngineering research community finally as well.