How to resolve the algorithm Knapsack problem/Unbounded step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Knapsack problem/Unbounded step by step in the D programming language

Table of Contents

Problem Statement

A traveler gets diverted and has to make an unscheduled stop in what turns out to be Shangri La.   Opting to leave, he is allowed to take as much as he likes of the following items, so long as it will fit in his knapsack, and he can carry it. He knows that he can carry no more than   25   'weights' in total;   and that the capacity of his knapsack is   0.25   'cubic lengths'. Looking just above the bar codes on the items he finds their weights and volumes.   He digs out his recent copy of a financial paper and gets the value of each item.

He can only take whole units of any item, but there is much more of any item than he could ever carry

Show how many of each item does he take to maximize the value of items he is carrying away with him.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Knapsack problem/Unbounded step by step in the D programming language

Source code in the d programming language

void main() @safe /*@nogc*/ {
    import std.stdio, std.algorithm, std.typecons, std.conv;

    static struct Bounty {
        int value;
        double weight, volume;
    }

    immutable Bounty panacea = {3000,  0.3, 0.025};
    immutable Bounty ichor =   {1800,  0.2, 0.015};
    immutable Bounty gold =    {2500,  2.0, 0.002};
    immutable Bounty sack =    {   0, 25.0, 0.25 };

    immutable maxPanacea = min(sack.weight / panacea.weight,
                               sack.volume / panacea.volume).to!int;
    immutable maxIchor   = min(sack.weight / ichor.weight,
                               sack.volume / ichor.volume).to!int;
    immutable maxGold    = min(sack.weight / gold.weight,
                               sack.volume / gold.volume).to!int;

    Bounty best = {0, 0, 0};
    Tuple!(int, int, int) bestAmounts;

    foreach (immutable nPanacea; 0 .. maxPanacea)
        foreach (immutable nIchor; 0 .. maxIchor)
            foreach (immutable nGold; 0 .. maxGold) {
                immutable Bounty current = {
                    value: nPanacea * panacea.value +
                           nIchor * ichor.value +
                           nGold * gold.value,
                    weight: nPanacea * panacea.weight +
                            nIchor * ichor.weight +
                            nGold * gold.weight,
                    volume: nPanacea * panacea.volume +
                            nIchor * ichor.volume +
                            nGold * gold.volume};

                if (current.value > best.value &&
                    current.weight <= sack.weight &&
                    current.volume <= sack.volume) {
                    best = Bounty(current.value, current.weight, current.volume);
                    bestAmounts = tuple(nPanacea, nIchor, nGold);
                }
            }

    writeln("Maximum value achievable is ", best.value);
    writefln("This is achieved by carrying (one solution) %d" ~
             " panacea, %d ichor and %d gold", bestAmounts[]);
    writefln("The weight to carry is %4.1f and the volume used is %5.3f",
             best.weight, best.volume);
}


void main() {
  import std.stdio, std.algorithm, std.typecons, std.range, std.conv;

  alias Bounty = Tuple!(int,"value", double,"weight", double,"volume");

  immutable panacea = Bounty(3000,  0.3, 0.025);
  immutable ichor =   Bounty(1800,  0.2, 0.015);
  immutable gold =    Bounty(2500,  2.0, 0.002);
  immutable sack =    Bounty(   0, 25.0, 0.25);

  immutable maxPanacea = min(sack.weight / panacea.weight, sack.volume / panacea.volume).to!int;
  immutable maxIchor   = min(sack.weight / ichor.weight,   sack.volume / ichor.volume).to!int;
  immutable maxGold    = min(sack.weight / gold.weight,    sack.volume / gold.volume).to!int;

  immutable best =
    cartesianProduct(maxPanacea.iota, maxIchor.iota, maxGold.iota)
    .map!(t => tuple(Bounty(t[0] * panacea.value  + t[1] * ichor.value  + t[2] * gold.value,
                            t[0] * panacea.weight + t[1] * ichor.weight + t[2] * gold.weight,
                            t[0] * panacea.volume + t[1] * ichor.volume + t[2] * gold.volume), t))
    .filter!(t => t[0].weight <= sack.weight && t[0].volume <= sack.volume)
    .reduce!max;

  writeln("Maximum value achievable is ", best[0].value);
  writefln("This is achieved by carrying (one solution) %d panacea, %d ichor and %d gold", best[1][]);
  writefln("The weight to carry is %4.1f and the volume used is %5.3f", best[0][1..$]);
}


  

You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the Racket programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Bc programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Java programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the zkl programming language
You may also check:How to resolve the algorithm Chat server step by step in the Prolog programming language