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

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

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