This is the knapsack problem if you can either pick a product or not. If you can pick fractional values of products then you could solve this with the simplex method, but the fractional knapsack problem has a simple solution.
Order the items by energy/price ratio, picking 100% of the highest ones until you run out of money, then pick a fractional value of the highest remaining one.
For example if prices are 4,3,5,4 and energies are 3,5,2,7 the ordering is
7/4, 5/3, 3/4, 2/5
So you would pick items 4 and 2 which would cost 7$, with the 3$ remaining you would buy 75% of the first item for a price of $3 and an energy of 3*.75 = 2.25
This would give a total energy of 14.25
Note that allowing fractional values will give you a higher objective value than only allowing 0% or 100%, so no integer solution will do any better than 14.25 (or 14 for that matter since the objective value has to be an integer).
To solve the original knapsack problem, you can use a branch-and-bound which should work just fine in practice. Assume that you have a current candidate solution with an objective value of z*
- Solve the relaxed problem, where you allow the fractional weights. If the value is less than z*, discard this branch.
- Compute a new value z which is the solution found without the last fractional weight, if this value is greater than z* , replace z* with your new value
- Pick one item (say the first in the list, the most profitable one) and form two subproblems, one where you must include it in the solution and one where you cannot include it in the solution (this is the branching step).
- As long as you have subproblems left to solve, pick one and go back to step 1.
Note that when you create the subproblem where you must pick an item, just subtract its price from the budget and add the value to the profit, now you have a smaller problem to solve.
For a more detailed description look at Branch and Bound on Wikipedia.