In the bin packing problem, items of different weights (or sizes) must be packed into a finite number of bins each with the capacity C in a way that minimizes the number of bins used. The decision version of the bin packing problem (deciding if objects will fit into <= k bins) is NP-complete. There is no known polynomial time algorithm to solve the optimization version of the bin packing problem. In this practice problem you will be examining three greedy approximation algorithms to solve the bin packing problem.
- First-Fit: Put each item as you come to it into the first
(earliest opened) bin into which it fits. If there is no available
bin then open a new bin.
- First-Fit-Decreasing: First sort the items in decreasing order by
size, then use First-Fit on the resulting list.
- Best Fit: Place the items in the order in which they arrive.
Place the next item into the bin which will leave the least room
left over after the item is placed in the bin. If it does not fit
in any bin, start a new bin.
a) Give pseudo code and the running time for each of the
approximation algorithms.
b) Implement the algorithms in C++. Your program
named binpack should read in a text file named bin.txt with
multiple test cases as explained below and output to the terminal
the number of bins each algorithm calculated for each test case.
Submit a README file
Example bin.txt: The first line is the number of test cases,
followed by the capacity of bins for that test case, the number of
items and then the weight of each item. You can assume that the
weight of an item does not exceed the capacity of a bin for that
problem.
3
10
6
5 10 2 5 4 4
10
20
4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6
10
4
3 8 2 7
Sample output:
Test Case 1 First Fit: 4, First Fit Decreasing: 3, Best Fit:
4
Test Case 2 First Fit: 15, First Fit Decreasing: 10, Best Fit:
15
Test Case 2 First Fit: 3, First Fit Decreasing: 2, Best Fit: 2
c) Randomly generate at least 20 bin packing instances. Summarize
the results for each algorithm. Which algorithm performs better?
How often? Note: Submit a description of how the inputs were
generated not the code used to produce the random inputs.
ANSWER:
//main.cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "mergesort.h"
#define CAPACITY 100
#define MAX_ITEM_NUM 10000
#define MAX_WEIGHT 100
#define INTERVAL 1000
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
int firstFit(int capacity, vector<int> weights);
void timeRandom(int sort);
int main()
{
// open bin file
ifstream data;
string path = "bin.txt";
data.open(path.c_str());
vector<int> itemWeightVector;
int testCaseCount = 0;
int testCaseNum = 0;
int capacity = 0;
int itemNum = 0;
int itemWeight = 0;
// get number of test cases
while (data >> testCaseNum)
{
//cout << "testCaseNum: " << testCaseNum <<
endl;
// for each test case
for (int i = 0; i < testCaseNum; i++)
{
// get capacity of bins
data >> capacity;
//cout << "capacity: " << capacity << endl;
// get number of items
data >> itemNum;
//cout << "itemNum: " << itemNum << endl;
// for each item
for (int i = 0; i < itemNum; i++)
{
// get the weight of each item
data >> itemWeight;
itemWeightVector.push_back(itemWeight);
//cout << itemWeight << " ";
}
//cout << endl;
// run first-fit
int ff = firstFit(capacity, itemWeightVector);
// reverse sort weights of each item
mergesort(itemWeightVector, 0, itemWeightVector.size() - 1);
/*
for (int i = 0; i < itemWeightVector.size(); i++)
{
cout << "itemWeightVector: " << itemWeightVector[i]
<< endl;;
}
*/
// run first-fit-decreasing
int ffd = firstFit(capacity, itemWeightVector);
// output results
cout << "Test Case " << testCaseCount << " First
Fit: " << ff << " - First Fit Decreasing: " <<
ffd << endl;
// clear vectors for next test case
itemWeightVector.clear();
testCaseCount += 1;
}
}
timeRandom(0);
cout << endl;
timeRandom(1);
}
int firstFit(int capacity, vector<int> weights)
{
vector<int> bins;
bins.push_back(capacity);
// for all items in weights vector
for (int i = 0; i < weights.size(); i++)
{
// boolean switch
// 1 if item fit into existing bin, 0 if not
int fit = 0;
// compare with all bins
for (int j = 0; j < bins.size(); j++)
{
// if item weight is less than space available in bin j
if (weights[i] <= bins[j])
{
//cout << "item " << i << " fits into bin "
<< j << endl;
// add item to bin j
bins[j] -= weights[i];
fit = 1;
break;
}
}
//cout << "fit: " << fit << endl;
if (fit == 0)
{
//cout << "adding bin" << endl;
bins.push_back(capacity);
bins[bins.size() - 1] -= weights[i];
}
}
//cout << "bin size: " << bins.size() <<
endl;
return bins.size();
}
void timeRandom(int sort)
{
// seed random generator
srand(time(NULL));
vector<int> itemWeightVector;
int n = 0;
cout << "n" << '\t' << "runtime (s)" <<
'\t' << "bins" << endl;
while (n < MAX_ITEM_NUM)
{
for (int i = 0; i <= n; i++)
{
itemWeightVector.push_back(rand() % MAX_WEIGHT);
//cout << "itemWeightVector: " << itemWeightVector[i]
<< endl;
}
//return 1;
clock_t start;
clock_t end;
start = clock();
if (sort == 1)
{
// reverse sort weights of each item
mergesort(itemWeightVector, 0, itemWeightVector.size() - 1);
}
// run first-fit
int ff = firstFit(CAPACITY, itemWeightVector);
// calculate and output array size n and time in seconds
end = clock();
double diff = ((double)end - (double)start);
double runtime = diff / (double) CLOCKS_PER_SEC;
cout << n << '\t' << runtime << '\t' << ff << endl;
// increment n
n += INTERVAL;
// reset item weight vector
itemWeightVector.clear();
}
}
--------------------------------------------------------------------------------------------
//mergesort.cpp
#include "mergesort.h"
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::cout;
using std::endl;
// list of integers via the merge sort algorithm
void merge(vector<int>&list, int p, int q, int r)
{
int n = q - p + 1;
int m = r - q;
int L[n];
int R[m];
for (int i = 0; i < n; i++)
{
L[i] = list[p + i];
}
for (int j = 0; j < m; j++)
{
R[j] = list[q + 1 + j];
}
int i = 0;
int j = 0;
int k = p;
while (i < n && j < m)
{
if (L[i] >= R[j])
{
list[k] = L[i];
i = i + 1;
}
else
{
list[k] = R[j];
j = j + 1;
}
k += 1;
}
while (i < n)
{
list[k] = L[i];
i += 1;
k += 1;
}
while (j < m)
{
list[k] = R[j];
j += 1;
k += 1;
}
}
// recursively sorts a list of integers via the merge sort
algorithm
void mergesort(vector<int>& list, int p, int r)
{
if (p < r)
{
int q = (p + r) / 2;
mergesort(list, p, q);
mergesort(list, q + 1, r);
merge(list, p, q, r);
}
}
----------------------------------------------------------------------------------------
//mergesort.h
#ifndef MERGESORT
#define MERGESORT
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::cout;
using std::endl;
void merge(vector<int>&list, int p, int q, int
r);
void mergesort(vector<int>&list, int p, int r);
#endif
In the bin packing problem, items of different weights (or sizes) must be packed into a...
This is all I have
2. Consider the following approximation algorithm for the bin packing problem. Algorithm Last Fit(1,S) Input: Set I of items and set S of item sizes; item I; E I has size S; Output: A packing of I into unit size bins B {} for each item I; e I do { if I; fits in one of the bins of B then Put I; in the last bin where it fits else { Add a...
B.) First Fit
C.) First Fit with sorting
#1. Bin packing - Perform three bin packing algorithms. You can assume you have infinite supply of bins (minibuses if we think as of packing data) of size 7. Explain your steps. 0 A) Next Fit Groups Minibuses 3 7 10 7 6 4 7 5 20 7
Consider the following four problems: Bin Packing: Given n items with positive integer sizes s1,s2,...,sn, a capacity C for bins and a positive integer k, is it possible to pack the n items using at most k bins? Partition: Given a set S of n integers, is it possible to partition S into two subsets S1 and S2 so that the sum of the integers in S1 is equal to the sum of the integers in S2? Longest Path: Given...
Give an algorithm that minimizes the maximum load of bins for
the following description:
We are given a list of n items with sizes s1, 82,. . , Sn A sequential bin packing of these at in sad ins (That is, each bin has items si, si+1,. , s, for some indices i < j.) Bins have unbounded capacities. The load of a bin is the sum of the elements in it. Give an algorithm that determines a sequential packing...
Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...
"Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and each item has a weight W[1:n] and monetary value P[1:n]. You have to determine which items to take so that the total weight is C, and the total value (profit) is maximized. In this case we are considering an integer problem, so you can either take an item, or not take an item, you cannot take it fractionally. If you recall, the greedy algorithm...
Assume that an imaginary supermarket sells items that are identified by a unique item number which is an integer in range 100 to 499 inclusive. The items are classified in four different categories based on their numbers, and each category defines a different sales tax rate. These four categories along with the range of item numbers included in them, and their specific sales tax rates has been summarized in the following table: Category Item Numbers Sales Tax Rate A 100...
Python Problem, just need some guidance with the description, pseudocode and run time. I believe this is an 0-1 knapsack problem? Shopping Spree: (20 points) Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example,...
Shopping Spree: Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera and...
1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5