Hi commons-math First, thanks for a handy library.
Second, I'm looking for a little help with my linear programming problem. I'm a better coder than mathematician (and undergrad stats was a very long time ago), so probably I'm botching that bit in a way that is amusing to the experts :) The problem I'm solving is adding chemicals to a body of water so as to approach a target chemical composition. E.g. adding 1mg/l CaSO4 to the water adds 40.08/172.9 ppm Ca and 96.07/172.19 ppm SO4 to the end result (based on their molecular weights). Given 9 different types of chemical addition and 6 desired ion concentrations, I'd like to minimise the errors (error being target - actual concentration) subject to some typical (user selected) LP-type constraints (e.g. Ca >= 50ppm, mg/l of CaCO3 = 0). What I have managed to do with commons-math is formulate a working LP solution for this that can maximise or minimise the amounts of chemicals added. *That works great, but I'm stumped for how to tackle the minimise-errors scenario.* *Any suggestions?* (I'm suspecting that I'm using LP like a hammer here where I should be using something else instead because this problem isn't a nail... ) Thanks and regards! Alan PS: *More info:* The code for the working min/max additions LP is here: https://github.com/alanmclachlan/brewday/blob/master/src/main/java/mclachlan/brewday/math/WaterBuilder.java The LP is formulated as follows Let: Input Water ppm | Target ppm ------------------------------ Ca s1 | t1 HCO- s2 | t2 SO4 s3 | t3 Cl- s4 | t4 Mg s5 | t5 Na s6 | t6 Additions (mg/l): ------------------- CaCO3_undis x1 CaCO3_dis x2 CaSO4 x3 CaCl x4 MgSO4 x5 NaHCO3 x6 NaCl x7 Ca(HCO3) x8 MgCl x9 Let the molecular ratio used to get from addition mg/l to solution ppm be matrix "a": a11...a69 (e.g. a11 = 40.08/100.09 = Ca contributed by CaCO3_undis ) So then (where <=> can be user selected >, or <, user can also exclude a constraint): possible ppm constraints (user selected): - - - - - - - - - - - - - - - - - a11.x1 + ... + a19.x9 <=> t1 - s1 a21.x1 + ... + a29.x9 <=> t2 - s2 a31.x1 + ... + a39.x9 <=> t3 - s3 a41.x1 + ... + a49.x9 <=> t4 - s4 a51.x1 + ... + a59.x9 <=> t5 - s5 a61.x1 + ... + a69.x9 <=> t6 - s6 non=negative constraint, can't have <0 mg/l: - - - - - - - - - - - - - - - - - xn >= 0 for n = 1..8 allowed ingredients constraints: - - - - - - - - - - - - - - - - - xn = 0 where the user has deselected an ingredient objective function: - - - - - - - - - - - - - - - - - For MIN or MAX additions we sum the addition quantities y = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
