Dynamic Programming Knapsack K-exact items - python

I found this very handy example code which implements a DP solution to the knapsack problem (kudos to the person who posted it).
https://codereview.stackexchange.com/questions/20569/dynamic-programming-solution-to-knapsack-problem
I am trying to modify it to include a constraint on the number of items k in the knapsack.
I added a third argument
def knapsack(items, maxweight, maxitems):
and modified the reconstruction as follows:
while i > 0:
if bestvalues[i][j] != bestvalues[i - 1][j] and len(reconstruction) < maxitems:
reconstruction.append(items[i - 1])
j -= items[i - 1][1]
i -= 1
Provided I input enough items to choose from this will always converge to the desired k number of items. However, I am fairly certain that this is not finding the closest approximation of the global optimum. The discussions I have read after some searching refer to adding a third dimension k and accounting for the constraint before the reconstruction (I *think this would be during the best value assessment).
Can someone provide an example of how to do this? Ideally a working python example would be fantastic but I'll settle for pseudocode. I have read a few instructions using notation but I am still not sure how to constrain with k (outside of what I have done here).
Thanks!

As i stated in the comment above a third dimension is required, i have written a recursive dynamic programming solution :
#include<bits/stdc++.h>
using namespace std;
int noOfItems, items[100], maxWeight, maxItems, value[100];
int dp[100][1000][100];
int solve(int idx, int currentWeight, int itemsLeft){
if(idx == noOfItems || itemsLeft == 0) return 0;
if(dp[idx][currentWeight][itemsLeft] != -1) return dp[idx][currentWeight][itemsLeft];
int v1 = 0, v2 = 0;
//try to included the current item
if(currentWeight >= items[idx]) v1 = solve(idx+1, currentWeight-items[idx], itemsLeft-1) + value[idx];
//exclude current item
v2 = solve(idx+1, currentWeight, itemsLeft);
return dp[idx][currentWeight][itemsLeft] = max(v1, v2);
}
//print the contents of the knapsack
void print(int idx, int currentWeight, int itemsLeft){
if(idx == noOfItems || itemsLeft == 0) return;
int v1 = 0, v2 = 0;
if(currentWeight >= items[idx]) v1 = solve(idx+1, currentWeight-items[idx], itemsLeft-1) + value[idx];
v2 = solve(idx+1, currentWeight, itemsLeft);
if(v1 >= v2){
cout << idx << " " << items[idx] << " " << value[idx] << endl;
print(idx+1, currentWeight-items[idx], itemsLeft-1);
return;
}else{
print(idx+1, currentWeight, itemsLeft);
return;
}
}
int main(){
cin >> noOfItems >> maxWeight >> maxItems;
for(int i = 0;i < noOfItems;i++) cin >> items[i] >> value[i];
memset(dp, -1, sizeof dp);
cout << solve(0, maxWeight, maxItems) << endl; //prints the maximum value that we can get from the constraints
cout << "Printing the elements in the knapsack" << endl;
print(0, maxWeight, maxItems);
return 0;
}
Link to solution on ideone : https://ideone.com/wKzqXk

Related

Unusual behaviour of Ant Colony Optimization for Closest String Problem in Python and C++

This is probably going to be a long question, I apologize in advance.
I'm working on a project with the goal of researching different solutions for the closest string problem.
Let s_1, ... s_n be strings of length m. Find a string s of length m such that it minimizes max{d(s, s_i) | i = 1, ..., n}, where d is the hamming distance.
One solution that has been tried is one using ant colony optimization, as decribed here.
The paper itself does not go into implementation details, so I've done my best on efficiency. However, efficiency is not the only unusual behaviour.
I'm not sure whether it's common pratice to do so, but I will present my code through pastebin since I believe it would overwhelm the thread if I should put it directly here. If that turns out to be a problem, I won't mind editing the thread to put it here directly. As all the previous algorithms I've experimented with, I've written this one in python initially. Here's the code:
def solve_(self, problem: CSProblem) -> CSSolution:
m, n, alphabet, strings = problem.m, problem.n, problem.alphabet, problem.strings
A = len(alphabet)
rho = self.config['RHO']
colony_size = self.config['COLONY_SIZE']
global_best_ant = None
global_best_metric = m
ants = np.full((colony_size, m), '')
world_trails = np.full((m, A), 1 / A)
for iteration in range(self.config['MAX_ITERS']):
local_best_ant = None
local_best_metric = m
for ant_idx in range(colony_size):
for next_character_index in range(m):
ants[ant_idx][next_character_index] = random.choices(alphabet, weights=world_trails[next_character_index], k=1)[0]
ant_metric = utils.problem_metric(ants[ant_idx], strings)
if ant_metric < local_best_metric:
local_best_metric = ant_metric
local_best_ant = ants[ant_idx]
# First we perform pheromone evaporation
for i in range(m):
for j in range(A):
world_trails[i][j] = world_trails[i][j] * (1 - rho)
# Now, using the elitist strategy, only the best ant is allowed to update his pheromone trails
best_ant_ys = (alphabet.index(a) for a in local_best_ant)
best_ant_xs = range(m)
for x, y in zip(best_ant_xs, best_ant_ys):
world_trails[x][y] = world_trails[x][y] + (1 - local_best_metric / m)
if local_best_metric < global_best_metric:
global_best_metric = local_best_metric
global_best_ant = local_best_ant
return CSSolution(''.join(global_best_ant), global_best_metric)
The utils.problem_metric function looks like this:
def hamming_distance(s1, s2):
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
def problem_metric(string, references):
return max(hamming_distance(string, r) for r in references)
I've seen that there are a lot more tweaks and other parameters you can add to ACO, but I've kept it simple for now. The configuration I'm using is is 250 iterations, colony size od 10 ants and rho=0.1. The problem that I'm testing it on is from here: http://tcs.informatik.uos.de/research/csp_cssp , the one called 2-10-250-1-0.csp (the first one). The alphabet consists only of '0' and '1', the strings are of length 250, and there are 10 strings in total.
For the ACO configuration that I've mentioned, this problem, using the python solver, gets solved on average in 5 seconds, and the average target function value is 108.55 (simulated 20 times). The correct target function value is 96. Ironically, the 5-second average is good compared to what it used to be in my first attempt of implementing this solution. However, it's still surprisingly slow.
After doing all kinds of optimizations, I've decided to try and implement the exact same solution in C++ so see whether there will be a significant difference between the running times. Here's the C++ solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <random>
#include <chrono>
#include <map>
class CSPProblem{
public:
int m;
int n;
std::vector<char> alphabet;
std::vector<std::string> strings;
CSPProblem(int m, int n, std::vector<char> alphabet, std::vector<std::string> strings)
: m(m), n(n), alphabet(alphabet), strings(strings)
{
}
static CSPProblem from_csp(std::string filepath){
std::ifstream file(filepath);
std::string line;
std::vector<std::string> input_lines;
while (std::getline(file, line)){
input_lines.push_back(line);
}
int alphabet_size = std::stoi(input_lines[0]);
int n = std::stoi(input_lines[1]);
int m = std::stoi(input_lines[2]);
std::vector<char> alphabet;
for (int i = 3; i < 3 + alphabet_size; i++){
alphabet.push_back(input_lines[i][0]);
}
std::vector<std::string> strings;
for (int i = 3 + alphabet_size; i < input_lines.size(); i++){
strings.push_back(input_lines[i]);
}
return CSPProblem(m, n, alphabet, strings);
}
int hamm(const std::string& s1, const std::string& s2) const{
int h = 0;
for (int i = 0; i < s1.size(); i++){
if (s1[i] != s2[i])
h++;
}
return h;
}
int measure(const std::string& sol) const{
int mm = 0;
for (const auto& s: strings){
int h = hamm(sol, s);
if (h > mm){
mm = h;
}
}
return mm;
}
friend std::ostream& operator<<(std::ostream& out, CSPProblem problem){
out << "m: " << problem.m << std::endl;
out << "n: " << problem.n << std::endl;
out << "alphabet_size: " << problem.alphabet.size() << std::endl;
out << "alphabet: ";
for (const auto& a: problem.alphabet){
out << a << " ";
}
out << std::endl;
out << "strings:" << std::endl;
for (const auto& s: problem.strings){
out << "\t" << s << std::endl;
}
return out;
}
};
std::random_device rd;
std::mt19937 gen(rd());
int get_from_distrib(const std::vector<float>& weights){
std::discrete_distribution<> d(std::begin(weights), std::end(weights));
return d(gen);
}
int max_iter = 250;
float rho = 0.1f;
int colony_size = 10;
int ant_colony_solver(const CSPProblem& problem){
srand(time(NULL));
int m = problem.m;
int n = problem.n;
auto alphabet = problem.alphabet;
auto strings = problem.strings;
int A = alphabet.size();
float init_pher = 1.0 / A;
std::string global_best_ant;
int global_best_matric = m;
std::vector<std::vector<float>> world_trails(m, std::vector<float>(A, 0.0f));
for (int i = 0; i < m; i++){
for (int j = 0; j < A; j++){
world_trails[i][j] = init_pher;
}
}
std::vector<std::string> ants(colony_size, std::string(m, ' '));
for (int iteration = 0; iteration < max_iter; iteration++){
std::string local_best_ant;
int local_best_metric = m;
for (int ant_idx = 0; ant_idx < colony_size; ant_idx++){
for (int next_character_idx = 0; next_character_idx < m; next_character_idx++){
char next_char = alphabet[get_from_distrib(world_trails[next_character_idx])];
ants[ant_idx][next_character_idx] = next_char;
}
int ant_metric = problem.measure(ants[ant_idx]);
if (ant_metric < local_best_metric){
local_best_metric = ant_metric;
local_best_ant = ants[ant_idx];
}
}
// Evaporation
for (int i = 0; i < m; i++){
for (int j = 0; j < A; j++){
world_trails[i][j] = world_trails[i][j] + (1.0 - rho);
}
}
std::vector<int> best_ant_xs;
for (int i = 0; i < m; i++){
best_ant_xs.push_back(i);
}
std::vector<int> best_ant_ys;
for (const auto& c: local_best_ant){
auto loc = std::find(std::begin(alphabet), std::end(alphabet), c);
int idx = loc- std::begin(alphabet);
best_ant_ys.push_back(idx);
}
for (int i = 0; i < m; i++){
int x = best_ant_xs[i];
int y = best_ant_ys[i];
world_trails[x][y] = world_trails[x][y] + (1.0 - static_cast<float>(local_best_metric) / m);
}
if (local_best_metric < global_best_matric){
global_best_matric = local_best_metric;
global_best_ant = local_best_ant;
}
}
return global_best_matric;
}
int main(){
auto problem = CSPProblem::from_csp("in.csp");
int TRIES = 20;
std::vector<int> times;
std::vector<int> measures;
for (int i = 0; i < TRIES; i++){
auto start = std::chrono::high_resolution_clock::now();
int m = ant_colony_solver(problem);
auto stop = std::chrono::high_resolution_clock::now();
int duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
times.push_back(duration);
measures.push_back(m);
}
float average_time = static_cast<float>(std::accumulate(std::begin(times), std::end(times), 0)) / TRIES;
float average_measure = static_cast<float>(std::accumulate(std::begin(measures), std::end(measures), 0)) / TRIES;
std::cout << "Average running time: " << average_time << std::endl;
std::cout << "Average solution: " << average_measure << std::endl;
std::cout << "all solutions: ";
for (const auto& m: measures) std::cout << m << " ";
std::cout << std::endl;
return 0;
}
The average running time now is only 530.4 miliseconds. However, the average target function value is 122.75, which is significantly higher than that of the python solution.
If the average function values were the same, and the times were as they are, I would simply write this off as 'C++ is faster than python' (even though the difference in speed is also very suspiscious). But, since C++ yields worse solutions, it leads me to believe that I've done something wrong in C++. What I'm suspiscious of is the way I'm generating an alphabet index using weights. In python I've done it using random.choices as follows:
ants[ant_idx][next_character_index] = random.choices(alphabet, weights=world_trails[next_character_index], k=1)[0]
As for C++, I haven't done it in a while so I'm a bit rusty on reading cppreference (which is a skill of its own), and the std::discrete_distribution solution is something I've plain copied from the reference:
std::random_device rd;
std::mt19937 gen(rd());
int get_from_distrib(const std::vector<float>& weights){
std::discrete_distribution<> d(std::begin(weights), std::end(weights));
return d(gen);
}
The suspiscious thing here is the fact that I'm declaring the std::random_device and std::mt19937 objects globally and using the same ones every time. I have not been able to find an answer to whether this is the way they're meant to be used. However, if I put them in the function:
int get_from_distrib(const std::vector<float>& weights){
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> d(std::begin(weights), std::end(weights));
return d(gen);
}
the average running time gets significantly worse, clocking in at 8.84 seconds. However, even more surprisingly, the average function value gets worse as well, at 130.
Again, if only one of the two things changed (say if only the time went up) I would have been able to draw some conclusions. This way it only gets more confusing.
So, does anybody have an idea of why this is happening?
Thanks in advance.
MAJOR EDIT: I feel embarrased having asked such a huge question when in fact the problem lies in a simple typo. Namely in the evaporation step in the C++ version I put a + instead of a *.
Now the algorithms behave identically in terms of average solution quality.
However, I could still use some tips on how to optimize the python version.
Apart form the dumb mistake I've mentioned in the question edit, it seems I've finally found a way to optimize the python solution decently. First of all, keeping world_trails and ants as numpy arrays instead of lists of lists actually slowed things down. Furthermore, I actually stopped keeping a list of ants altogether since I only ever need the best one per iteration.
Lastly, running cProfile indicated that a lot of the time was spent on random.choices, therefore I've decided to implement my own version of it suited specifically for this case. I've done this by pre-computing total weight sum per character for each next iteration (in the trail_row_wise_sums array), and using the following function:
def fast_pick(arr, weights, ws):
r = random.random()*ws
for i in range(len(arr)):
if r < weights[i]:
return arr[i]
r -= weights[i]
return 0
The new version now looks like this:
def solve_(self, problem: CSProblem) -> CSSolution:
m, n, alphabet, strings = problem.m, problem.n, problem.alphabet, problem.strings
A = len(alphabet)
rho = self.config['RHO']
colony_size = self.config['COLONY_SIZE']
miters = self.config['MAX_ITERS']
global_best_ant = None
global_best_metric = m
init_pher = 1.0 / A
world_trails = [[init_pher for _ in range(A)] for _ in range(m)]
trail_row_wise_sums = [1.0 for _ in range(m)]
for iteration in tqdm(range(miters)):
local_best_ant = None
local_best_metric = m
for _ in range(colony_size):
ant = ''.join(fast_pick(alphabet, world_trails[next_character_index], trail_row_wise_sums[next_character_index]) for next_character_index in range(m))
ant_metric = utils.problem_metric(ant, strings)
if ant_metric <= local_best_metric:
local_best_metric = ant_metric
local_best_ant = ant
# First we perform pheromone evaporation
for i in range(m):
for j in range(A):
world_trails[i][j] = world_trails[i][j] * (1 - rho)
# Now, using the elitist strategy, only the best ant is allowed to update his pheromone trails
best_ant_ys = (alphabet.index(a) for a in local_best_ant)
best_ant_xs = range(m)
for x, y in zip(best_ant_xs, best_ant_ys):
world_trails[x][y] = world_trails[x][y] + (1 - 1.0*local_best_metric / m)
if local_best_metric < global_best_metric:
global_best_metric = local_best_metric
global_best_ant = local_best_ant
trail_row_wise_sums = [sum(world_trails[i]) for i in range(m)]
return CSSolution(global_best_ant, global_best_metric)
The average running time is now down to 800 miliseconds (compared to 5 seconds that it was before). Granted, applying the same fast_pick optimization to the C++ solution did also speed up the C++ version (around 150 ms) but I guess now I can write it off as C++ being faster than python.
Profiler also showed that a lot of the time was spent on calculating Hamming distances, but that's to be expected, and apart from that I see no other way of computing the Hamming distance between arbitrary strings more efficiently.

Discrepancy between a program in Python and C++

I wrote a code in C++ to solve a programming challenge on Project Euler (problem 65: https://projecteuler.net/problem=65), but have not been able to get the correct answer. To check my algorithm, I wrote the "same" code in Python (same as far as I can tell, but there's obviously some difference), but I got a different answer (the correct answer).
The basic idea behind the program is to find the sum of digits in the numerator of the 100th convergent of the continued fraction for e. Representing the continuing fraction as "e = [2;1,2,1,1,4,1,1,6,1,...,1,2k,1,...]"--this is the vector e_terms that I fill in my code.
My question is why does my C++ code give me a different answer than Python? (Again, I already know the answer to the challenge, I just want to understand the difference between my codes.) Both codes give me the same first 10 approximations of the continued fraction, and up to the 45th approximation; but from 46 and on, my codes give me different values. Is it because a long double doesn't store integers like Python does? Or is there some kind of flaw in my C++ code?
Thanks in advance for the help! And let me know if there is something I should clarify about my code.
C++ code
#include <iostream>
#include <vector>
#include "math.h"
using namespace std;
void PrintDouble(long double num)
{
int val = 0;
for (int i = floor(log(num)/log(10)); i >= 0; i--)
{
val = floor(num/(pow(10,i)));
num -= val*pow(10,i);
cout << val;
}
}
void Convergents_e(int max_val)
{
long double numer = 0, denom = 1, temp;
vector<int> e_terms;
if (max_val > 1) e_terms.push_back(1);
if (max_val > 2) e_terms.push_back(2);
if (max_val > 3)
{
for (int i = 1; i <= max_val-3; i++)
{
if (i%3 == 0) e_terms.push_back((int)((i/3+1)*2));
else e_terms.push_back(1);
cout << e_terms.back() << endl;
}
}
for (vector<int>::reverse_iterator it = e_terms.rbegin(); it != e_terms.rend(); it++)
{
numer += ((double)(*it) * denom);
cout << "\tnumer = ";
PrintDouble(numer);
cout << endl;
swap(numer, denom);
}
numer += 2*denom; // +1 term
cout << "e = " << numer << "/" << denom << " = " << numer/denom << endl;
int val = 0, sum = 0;
for (int i = floor(log(numer)/log(10)); i >= 0; i--)
{
val = floor(numer/(pow(10,i)));
numer -= val*pow(10,i);
sum += val;
cout << val;
}
cout << endl << "The total of the digits is " << sum << endl << endl;
}
int main()
{
Convergents_e(100);
}
Python code
#!/usr/bin/env python3
import math as m
max_val = 100
numer = 0
denom = 1
e_terms = []
digits = []
if (max_val > 1): e_terms.append(1)
if (max_val > 2): e_terms.append(2)
if (max_val > 3):
for i in range(1,max_val-3 + 1):
if (i%3 == 0): e_terms.append(int((i/3+1)*2))
else: e_terms.append(1)
print(e_terms[-1])
for e_t in reversed(e_terms):
numer += e_t * denom
print("\tnumer =", numer)
numer, denom = denom, numer
numer += 2*denom # +1 term
print("e =", numer, "/", denom, "=", numer/denom)
val = 0
sum = 0
for i in reversed(range(0,m.ceil(m.log(numer)/m.log(10)))):
val = m.floor(numer/(10**i))
numer -= (val*(10**i))
digits.append(val)
sum += val
#print(val)
print( "The total of the digits is",sum)

Knapsack Problem: Why do I need a 2 dimensional DP Matrix

I came across some classical Knapsack solutions and they always build a 2-dimensional DP array.
In my opinion, my code below solves the classical knapsack problem but with only a 1-dim DP array.
Can someone tell me where my solution does not work or why it is computationally inefficient compared to the 2D-DP version?
A 2D-DP version can be found here
https://www.geeksforgeeks.org/python-program-for-dynamic-programming-set-10-0-1-knapsack-problem/
example input:
weights = [(3,30),(2,20),(1,50),(4,30)]
constraint = 5
And my solution:
def knapsack(weights,constraint):
n = len(weights)
#define dp array
dp = [0]*(constraint+1)
#start filling in the array
for k in weights:
for i in range(constraint,k[0]-1,-1):
dp[i] = max(dp[i],dp[i-k[0]]+k[1])
return dp[constraint]
The version using O(nW) memory is more intuitive and makes it possible to easily retrieve the subset of items that produce the optimal answer value.
But, using O(n + W) of memory, we cannot retrieve this subset directly. While it is possible to do this, using the divide-and-conquer technique as explained in https://codeforces.com/blog/entry/47247?#comment-316200.
Sample code
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define FOR(i, b) for(int i = 0; i < (b); i++)
template<class T>
struct Knapsack{
int n, W;
vector<T> dp, vl;
vi ans, opt, wg;
Knapsack(int n_, int W): n(0), W(W),
dp(W + 1), vl(n_), opt(W + 1), wg(n_){}
void Add(T v, int w){
vl[n] = v;
wg[n++] = w;
}
T conquer(int l, int r, int W){
if(l == r){
if(W >= wg[l])
return ans.push_back(l), vl[l];
return 0;
}
FOR(i, W + 1)
opt[i] = dp[i] = 0;
int m = (l + r) >> 1;
for(int i = l; i <= r; i++)
for(int sz = W; sz >= wg[i]; sz--){
T dpCur = dp[sz - wg[i]] + vl[i];
if(dpCur > dp[sz]){
dp[sz] = dpCur;
opt[sz] = i <= m ? sz : opt[sz - wg[i]];
}
}
T ret = dp[W];
int K = opt[W];
T ret2 = conquer(l, m, K) + conquer(m + 1, r, W - K);
assert(ret2 == ret);
return ret;
}
T Solve(){
return conquer(0, n - 1, W);
}
};
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, W, vl, wg;
cin >> n >> W;
Knapsack<int> ks(n, W);
FOR(i, n){
cin >> vl >> wg;
ks.Add(vl, wg);
}
cout << ks.Solve() << endl;
}

Python to C++: Algorithm that list all combinations of Knapsack using recursion

I'm trying to implement a code that lists all possible combinations of a Knapsack problem using recursion. I have difficulty with recursion. I tried to solve it and got nothing, so I did some research and I found a code in Java Python, but I'm having a hard time trying to rewrite that code in C++.
Here is the solution code, in Java Python:
items = [1,1,3,4,5]
knapsack = []
limit = 7
def print_solutions(current_item, knapsack, current_sum):
#if all items have been processed print the solution and return:
if current_item == len(items):
print knapsack
return
#don't take the current item and go check others
print_solutions(current_item + 1, list(knapsack), current_sum)
#take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit):
knapsack.append(items[current_item])
current_sum += items[current_item]
#current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum )
print_solutions(0,knapsack,0)
I found that code in this link
Here is what I tried to do..
#include <iostream>
using namespace std;
void AddItem(int item, int *knapsack) {
int i = 0;
while (knapsack[i] != -1)
i++;
knapsack[i] = item;
};
void printKnapsack(int *knapsack, int n) {
cout << "[";
for (int i = 0; i < n; i++)
if (knapsack[i] != -1)
cout << knapsack[i] << ",";
}
void print_solutions(int current_item, int *knapsack, int current_sum, int *items, int n, int limit) {
//if all items have been processed print the solution and return
if (current_item == n - 1) {
printKnapsack(knapsack, n);
return;
};
//don't take the current item and go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
//take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit) {
AddItem(items[current_item], knapsack);
current_sum += items[current_item];
};
//current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
};
int main() {
int current_item = 0;
int current_sum = 0;
int limit, n;
cout << "Type the maximum weight ";
cin >> limit;
cout << "How many items? ";
cin >> n;
int* knapsack;
knapsack = new int[10];
for (int i = 0; i < 10; i++)
knapsack[i] = -1;
int * items;
items = new int[n];
cout << "Type weights.";
for (int i = 0; i < n; i++) {
cin >> items[i];
};
print_solutions(0, knapsack, 0, items, n, limit);
return 0;
}
With the input:
7 // limit
5 // number of items
1 1 3 4 5 // items
I expect to get the following final result:
[]
[5]
[4]
[3]
[3, 4]
[1]
[1, 5]
[1, 4]
[1, 3]
[1]
[1, 5]
[1, 4]
[1, 3]
[1, 1]
[1, 1, 5]
[1, 1, 4]
[1, 1, 3]
But all I get is arrays filled with 3 and 4 instead of getting all actual solutions.
In short
There is a major issue in your transcription of the algorithm from python to C++ related to the language semantics related to parameter passing.
In full details
When in python you write the following:
print_solutions(current_item + 1, list(knapsack), current_sum)
Then list(knapsack) is a copy from the knapsack list. So the recursive call in the middle leaves the original knapsack unchanged, whereas the second recursive call changes the original knapsack:
print_solutions(current_item + 1, knapsack, current_sum)
In your C++ code however, in both case you work on the original knapsack list (the arrays parameters are passed by references), so that knapsack gets completely messed up:
//don't take the current item and go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
How to make it work ?
Either you create a temporary array and copy knapsack in it, or, much better, you start to use vector , which will make your C++ life much easier (making attention to pass by value or by reference).
The following version uses a vectors. The & in the parameter means that it's an argument passed by reference (i.e. the original vector can be changed). Note that we do no longer need to pass n, as the vector knows its length, as list do in python:
void print_solutions(int current_item, vector<int>& knapsack, int current_sum, const vector<int>& items, int limit) {
//if all items have been processed print the solution and return
if (current_item == items.size() ) {
printKnapsack(knapsack);
return;
};
//don't take the current item and go check others
vector<int> knapcopy = knapsack;
print_solutions(current_item + 1, knapcopy, current_sum, items, limit);
//take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit) {
knapsack.push_back(items[current_item]);
current_sum += items[current_item];
//current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum, items, limit);
};
};
Here an online demo.

How to convert this extensive Python loop to C++?

I know very little on Python, but I'm quite experienced in C++.
I was looking for an algorithm that would loop through the points in a hexagon pattern and found one written in Python that seems to be exactly what I need. The problem is that I have no idea how to interpret it.
Here is the Python code:
for x in [(n-abs(x-int(n/2))) for x in range(n)]:
for y in range(n-x):
print ' ',
for y in range(x):
print ' * ',
print
I'd show you my attempts but there are like 30 different ones that all failed (which I'm sure are just my bad interpretation).
Hope this helps you.Tested both python and c++ code .Got same results.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
int n = 10;
int a[n];
for(int j = 0; j < n; j++) {
a[j] = n - abs(j - (n / 2));
}
for(int x = 0; x < n; x++) {
for(int y = 0; y < (n - a[x]); y++) {
std::cout << " " << std::endl;
}
}
for(int k = 0; k < a[n-1]; k++) {
std::cout << " * " << std::endl;
}
std::cout << "i" << std::endl;
return 0;
}
auto out = ostream_iterator<const char*>(cout," ");
for(int i = 0; i != n; ++i) {
auto width = n - abs(i - n/2);
fill_n(out, n-width, " ");
fill_n(out, width, " * ");
cout << "\n";
}
Live demo.
Python live demo for reference.

Categories