48 return (t0.
c/t0.
w > t1.
c/t1.
w ?
49 true : (t0.
c/t0.
w < t1.
c/t1.
w ?
false : (t0.
i > t1.
i)));
57 double* ww =
new double[
n_+1];
58 double* cc =
new double[
n_+1];
61 for (
int i =
n_ - 1; i >= 0; --i) {
67 for (
int i =
n_ - 1; i >= 0; --i) {
101 Knapsack(
int n,
double cap,
const double* c,
const double* w) :
119 void optimize(
double lb = 0.0,
double minimp = 1e-6);
132 int * xhat =
new int[
n_+1];
133 fill(xhat, xhat+(
n_+1), 0);
138 double * p =
new double[
n_+2];
139 double * w =
new double[
n_+2];
141 for (ii = 1; ii <
n_+1; ii++){
149 double caphat =
cap_;
158 double wSemiSum = w[j];
159 double pSemiSum = p[j];
160 while (wSemiSum <= caphat && ii < n_+2){
166 printf(
"Exceeded iterator limit. Aborting...\n");
174 double u = pSemiSum + (caphat - wSemiSum)*p[ii]/w[ii];
177 if (
z + minimp < zhat + u) {
180 while (w[j] <= caphat){
181 caphat = caphat - w[j];
200 for (k = 0; k <
n_; ++k){
206 caphat = caphat + w[
n_];
214 while (!(xhat[i]==1) && i>0) {
226 caphat = caphat + w[i];
const int * getBestSol() const
const double * weight_
The weight of the items (the order is after doing the cost/weight ordering)
Knapsack(int n, double cap, const double *c, const double *w)
const double getBestVal() const
void sortItems(const double *c, const double *w)
double cap_
The capacity of the knapsack.
double z
The optimal solution value.
void setCapacity(double cap)
bool ratioComp(const triplet &t0, const triplet &t1)
const double * cost_
The cost of each item (the order is after doing the cost/weight ordering)
void optimize(double lb=0.0, double minimp=1e-6)
Return true/false depending on whether the problem is feasible.
void setCosts(const double *c)
int * x
The optimal solution in terms of the input order.
int * perm_
The permutation corresponding to the ordering (element perm[i] is the i-th in the original order...
int n_
The number of items.