Question
Need help with my binpacking c++ code I have firstFit () and First Fit Decreasing () functions working but my bestFit() is still off. Any
Need help with my binpacking c++ code
I have firstFit () and First Fit Decreasing () functions working but my bestFit() is still off. Any help is appreciated.
input file:
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
my output for Best Fit is 6, 14, 4 for each test case
output should be Best Fit 4,15, 2 for each test case
here is the C++ code, thank you
//bin.cpp
#include
#include
#include
#include
#include
#include
#include "sort.h"
#define CAPACITY 100
#define maximum_item 10000
#define maximum_weight 100
#define intervalvalue 1000
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
int firstFit(int capacityval, vector
int bestFit(int capacityval, vector
// void timeRandom(int sort);
int main()
{
// open bin file
ifstream dataval;
string pathval = "bin.txt";
dataval.open(pathval.c_str());
vector
int test_case_count = 1;
int test_case_num = 0;
int capacityval = 0;
int item_num = 0;
int item_weight = 0;
while (dataval >> test_case_num)
{
for (int i = 0; i < test_case_num; i++)
{
dataval >> capacityval;
dataval >> item_num;
for (int i = 0; i < item_num; i++)
{
dataval >> item_weight;
item_Weight_Vector.push_back(item_weight);
}
int ff = firstFit(capacityval, item_Weight_Vector);
sort(item_Weight_Vector, 0, item_Weight_Vector.size() - 1);
// run first-fit-decreasing
int ffd = firstFit(capacityval, item_Weight_Vector);
//run best fit
int bf = bestFit(capacityval, item_Weight_Vector);
cout << "Test Case " << test_case_count << " First Fit: " << ff << " - First Fit Decreasing: " << ffd << " - Best Fit: " << bf << endl;
item_Weight_Vector.clear();
test_case_count += 1;
}
}
cout << endl;
}
int firstFit(int capacityval, vector
{
vector
binsval.push_back(capacityval);
for (int i = 0; i < weights.size(); i++)
{
int fit = 0;
for (int j = 0; j < binsval.size(); j++)
{
if (weights[i] <= binsval[j])
{
binsval[j] -= weights[i];
fit = 1;
break;
}
}
if (fit == 0)
{
binsval.push_back(capacityval);
binsval[binsval.size() - 1] -= weights[i];
}
}
return binsval.size();
}
int bestFit(int capacityval, vector
{
// Initialize result (Count of bins)
int fit = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int binsval[capacityval];
// Place items one by one
for (int i=0; i { // Find the best bin that ca accomodate // weight[i] int j; // Initialize minimum space left and index // of best bin int min = fit+1, bi = 0; for (j=0; j { if (binsval[j] >= weights[i] && binsval[j] - weights[i] < min) { bi = j; min = binsval[j] - weights[i]; } } // If no bin could accommodate weight[i], // create a new bin if (min==fit+1) { binsval[fit] = fit - weights[i]; fit++; } else // Assign the item to best bin binsval[bi] -= weights[i]; } return fit; } //sort.h #ifndef SORT #define SORT #include #include #include #include #include using std::ifstream; using std::ofstream; using std::string; using std::vector; using std::cout; using std::endl; void mergevalue(vector void sort(vector #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started