A useful feature would, rather than fiddling with the salts, be to have the optimum addition to match a profile pre-calculated. I've done some linear optimisation before, and this is a classic case.
UI: User checks boxes for which salts are available for use. Could either start with all checked, or look at the user's inventory to determine which salts are available by default. User hits an "Optimize" button to kick off the process.
Implementation: You'd be best off using a linear constraint solver library or a minimal implementation of the Simplex algorithm.
Problem format: We have a series of salts (those checked as available), and a series of ions, and construct a linear constraint system as:
And have the following other pre-calculated values (from water volume, etc):
Our constraints are that:
For each ion y:
Ey >= Sum(For all salts x -> Rx,y * Qy) + Iy - Ty
Uy >= Ty - Iy - Sum(For all salts x -> Rx,y * Qy)
This gives us variables in the problem indicating the excess and/or under amount of each ion. A simple goal to optimise would be to:
Minimise Sum(For all ions y -> Ey + Uy)
You could add default weights to those as how bad 1 unit over of any given ion is, vs. how bad it is under, vs. each of those for another ion. UI to configure those would be complicated, so probably best to just fix sensible values.
I would be happy to write the optimisation routine in some free time if you're interested. I gather you use PHP?
Cheers,
Tim
UI: User checks boxes for which salts are available for use. Could either start with all checked, or look at the user's inventory to determine which salts are available by default. User hits an "Optimize" button to kick off the process.
Implementation: You'd be best off using a linear constraint solver library or a minimal implementation of the Simplex algorithm.
Problem format: We have a series of salts (those checked as available), and a series of ions, and construct a linear constraint system as:
- Qx: quantity of salt x to add in units (>= 0)
- Ey: excess of ion y (>= 0)
- Uy: under-amount of ion y (>= 0)
And have the following other pre-calculated values (from water volume, etc):
- Rx,y: rate at which adding one unit of salt x increases concentration of ion y
- Iy: initial concentration of ion y
- Ty: target concentration of ion y
Our constraints are that:
For each ion y:
Ey >= Sum(For all salts x -> Rx,y * Qy) + Iy - Ty
Uy >= Ty - Iy - Sum(For all salts x -> Rx,y * Qy)
This gives us variables in the problem indicating the excess and/or under amount of each ion. A simple goal to optimise would be to:
Minimise Sum(For all ions y -> Ey + Uy)
You could add default weights to those as how bad 1 unit over of any given ion is, vs. how bad it is under, vs. each of those for another ion. UI to configure those would be complicated, so probably best to just fix sensible values.
I would be happy to write the optimisation routine in some free time if you're interested. I gather you use PHP?
Cheers,
Tim