This problem is not interesting enough, because putting your money in the bank guarantees you zero volatility (and a zero return on investment). In practice, whatever set of assets you chose you would get a very extreme solution (e.g. 100% weight on one asset with very low volatility.)
With a minor tweak, you can get a very interesting problem. You can constraint your portfolio to have at least an expected return of $R$. Now you get a mean-variance optimization problem, with cardinality constraints:
$$ min \,\,\, w^TCw $$
subject to $$ r^Tw = R $$
$$ w \ge 0 $$
$$ \Sigma w_i = 1 $$
and subject to "no more than 5 weights are non-zero (positive)".
Where $C$ is the covariance matrix and $w$ your weights vector. There is a way to formally define the last constraint using indicator integer variables, but I am not doing this here for the sake of simplicity.
Now, to the second part of the question:
Here is why the knapsack problem is not a fitting approach to this problem. The knapsack problem is really hard because it does not allow fractional solutions. In portfolio optimization you usually assume that you can have a fractional amount of an asset.
Mean-variance optimization is a convex quadratic programming (QP) optimization problem, which can be solved extremely fast with many widely available solvers. Mean-variance optimization with cardinality constraints (e.g. you have to have exactly 5 assets in the portfolio) is a problem that is harder, a Mixed Integer Real optimization problem.
You can use heuristics to obtain a very satisfactory solutions. Here is a strategy:
- Solve the mean-variance problem without the cardinality constraints. The solution is usually extreme in that there is a small number of non-zero assets anyway.
- Then you can brute-force your way through the cardinality constraint or use heuristics (that could be for example based on a ranking of the sharpe ratio - which also is the value of the dual variables I believe) to obtain a satisfactory solution.
If you want to take a look into papers on mean-variance optimization with cardinality constraints, here is one of the most cited papers:
Heuristics for cardinality constrained portfolio optimisation (T.-J. Chang, N. Meade, Beasley)
Be mindful that metaheuristics, such as Genetic Algorithms are largely black box, and you would not get much educational value about the underlying securities by implementing them.