You should use linear programming techniques using software like GAMS using the CPLEX solver. You can also use CVX in Matlab. The former is not free and the latter is.
It is very simple to develop yourself, like in this paper we discuss the formulas for use of numerical optimization to determine the best combination of resources to accomplish a least cost response.
Discrete cogeneration optimization with storage capacity decision support for dynamic hybrid solar combined heat and power systems in isolated rural villages