Write a c++ program to solve the knapsack problem

Assignment Help C/C++ Programming
Reference no: EM132641867

Problem

The Knapsack Problem is a constraint problem. Imagine you have a knapsack (a backpack) which can hold a maximum of maxwt kilograms. You have a pile of k items. Each item has a weight in kilograms. You can envision the problem defined by a list of weights:

wt = [wt0, wt1, wt2, . . . wtk-1]

A solution is a corresponding list of Boolean values:

b = [b0, b1, b2, . . . bk-1] where bi ∈ {0, 1}

indicating which of the k items you will put in the pack. The constraint imposed by the knapsack is that the sum of the weights of the chosen items must not exceed the maximum capacity of the pack:

           k-1
load =   Σ wtibi ≤ maxwt
           i=0

The knapsack problem is that given wt find the b that maximizes load under the constraint that load ≤ maxwt. Write a C++ program to solve the Knapsack Problem using a generational GA. The program will be called knapsack. It will take four (4) command line arguments:
1. the population size
2. the maximum number of generations
3. tournament size
4. the mutation probability per locus pm is given, where mutations will at each locus will have probability of pm/k. So if the parameter is 1.0 then the mutation probability at each locus is 1/k.
5. the xover probability
The program will then read from stdin a specific problem in this order:
1. the maxwt
2. the number of weights in wt (k the problem description)
3. the list of weights wt An example invocation:
knapsack 100 5000 3 1.0 0.2 < testcase01.txt
where testcase01.txt contains the data from stdin. An example problem file might be:
107
11
2.0 3.0 5.0 7.0 11.0 13.0 17.0 19.0 23.0 29.0 31.0

Program has these requirements:
• it is a generational GA.
• use elitism of two individuals.
• selection will be tournament selection.
xover will be uniform xover (sometimes just denoted ux) and is performed with probability given by the xover probability
• xover will produce one child
• mutate will flip each bi with probability pm/k.
the program will allow ?b with weight exceeding capacity, maxwt, but it will punish the fitness of those genes by the overage as follows:
          k-1
load = ∑wtibi
          i=0

fitness = if load > maxwt then load - (2 ∗ (load - maxwt))
otherwise then load

• the program will stop when the maxgenerations have been run or when the fitness is within 99.995% of maxwt. Note that selection can load up the second population with individuals from the first population and operators like mutate and xover can then modify the individuals in place. Just one possible way to manage the memory.
The output of your program will be the final population. X's indicate the item was selected. The number is the fitness. If the program stops because it has found the answer it should also print that it was found in the exact format seen below. Note the location of spaces to separate even the colon. Your output may be read by another program so keep to the format.

.....X...XX..X...X.. 499.960000
.....X...XX..X...X.. 499.960000
.....X.X..X..X...X.. 482.210000
..........XX.X...X.. 459.690000
.....X...XX. X.X.. 482.310000
.......X..X.XX. XX 367.640000
........X.X..X...X.. 482.250000
..X..X....X..X...X.. 441.230000
.....X..XXX..X..XX.X 17.310000
.....X..XXXX.X...X.. 212.500000
......XX..X..X..XX.. 287.930000
XX...X....X.XX...X.. 181.820000
.....X. XXXXX..X.. 105.080000
..X......XXX.X...X.. 377.260000
.....XX...X..X. XX 499.990000
.........XX. X.. 316.800000
.....X...XX..X...X.. 499.960000
.....X...XX..X...X.. 499.960000
.X...X...XX..X...XX. 292.900000
X....X...X...X...X.. 403.780000
FOUND Gen 2676 : .....XX...X..X....XX 499.990000
If it was not found it should print the last population in the format as above but use the FAILED message:

FAILED Gen 5001

2 Turning in Your Programs

Please tar up your code as a set of files and not a directory of a set of files. You tar should include a makefile to build the code. My scripts will automatically explode your tar file into a fresh directory and then execute your code with various parameters to see that it is working.

Homework will be submitted as an uncompressed tar file to the homework submission page linked from the main class page. You can submit as many times as you like. The LAST file you submit BEFORE the deadline will be the one graded. There are no late papers. For all submissions you will receive email giving you some automated feedback on the unpacking and compiling and running of code and possibly some other things that can be autotested. I will read the results of the runs and the reports you submit.

Have fun.

Reference no: EM132641867

Questions Cloud

Define how you would evaluate a program : "Culture" has become more of an integral component to research in the social sciences. Briefly describe the importance of incorporating cultural concepts.
Compute the annual interest rate or rate of return : Compute the annual interest rate or rate of return that you will earn on the following investments:
Coping with a crisis : This Seminar will focus on how individuals involved in a crisis situation deal with their roles during and after the incident.
Post descriptions of the two alternate validation methods : Post descriptions of the two alternate validation methods you selected. Then, explain when each might be appropriate in employee selection.
Write a c++ program to solve the knapsack problem : Write a C++ program to solve the Knapsack Problem using a generational GA. The program will be called knapsack. It will take four (4) command line arguments
Prepare relevant journal entries in the general ledger : Finished goods Security gates could from mid January 20X8 be imported from Germany at $750 per gate. Prepare relevant journal entries in the general ledger
Create statement of retained earnings for j and l accounting : Create the statement of retained earnings for J & L Accounting, Inc. using the ending balance from the statement of retained earnings from the prior period
Summarize your current understanding of indigenous identity : Writing is a continuous part of the fieldwork conducted by an anthropologist. It occurs when the anthropologist observes and records information.
Post adjusting journal entries to respective general ledger : Post the adjusting journal entries to the respective general ledger accounts, again being sure that the postings are to the correct debit or credit side

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Calculates the equivalent total number of seconds

The program should declare 4 variables, hours, minutes, seconds, and totalSeconds, all of type int. You may declare at most 1 additional variable for temporary usage. No other variable are allowed.

  Important aspects of structured programming practices

UCCD1004: PROGRAMMING CONCEPTS AND PRACTICES - demonstrate that you have acquired the skills and ability to be proficient in the area of developing C++

  Using a for loop, print the contents of the array.

Write the code to update every other element within the array with a lowercase x.

  Create a phonebook class

Create a PhoneBook class. Fields include first name, last name, area code, and phone number. Include an extraction operator that prompts the user for values for each field.

  Ansi-c program complete assignment as per written in the

complete assignment as per written in the attached

  Define the operations using peano arithmetic

Write five tests for each using numbers larger than 100 and verify using natZint and intZnat that your numbers work the same way as the usual natural numbers.

  Compute the character with maximum number of connections

Compute the character with maximum number of connections print out the result - Network theory has a host of applications. One fun application is to find the central character in a novel.

  Wap to calculate the area of the circle

Write a C++ program that prompts the user for the radius of a circle, then calls inline function circle Area to calculate the area of that circle.

  Wap that uses an array list of parameter type contact

Write a program that uses an Array List of parameter type Contact to store a database of contacts. The Contact class should store the contact's first.

  How to create and run a batch file in windows

Decision how you want to maintain and name your program in files. That is, you may use only one single .cpp file to include all of your source code

  Develop a simple poker game complete with basic ai

In this assignment, you will develop a simple poker game, complete with basic AI, using the object oriented programming principles discussed in class.

  Wrtie a function called gen_rand_double_array

Wrtie a function called gen_rand_double_array that generates 900 samples of size 22500 random numbers from U(10, 12). For each of these 900 samples, write a main funciton that calculates the mean and finds the simulated probability that the mean is b..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd