Ecological Bin Packing

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.

In this problem you will be solving a bin packing problem that deals with recycling glass.

 

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.

The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.

For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

 

The Input

The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31

indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.

Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.

 

The Output

For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.

The output should consist of a string of the three upper case characters ‘G’, ‘B’, ‘C’ (representing the colors green, brown, and clear) representing the color associated with each bin.

The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.

The integer indicating the minimum number of bottle movements should follow the string.

If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.

 

Sample Input

1 2 3 4 5 6 7 8 9

5 10 5 20 10 5 10 20 10

 

Sample Output

BCG 30

CBG 50

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

 

namespace BinPack

{

    class Bin

    {

        public int brown;

        public int green;

        public int clear;

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            int NumberOfBins = 3;

            Bin[] binBottles = new Bin[NumberOfBins];

            char[] color = new char[3];

            Console.Write("Enter bottle arrangements in respective bins: ");

            string str = Console.ReadLine();

            Regex reg = new Regex(" ");

            int i = 0, flag = 0; ;

            foreach (string val in reg.Split(str))

            {

                if (flag == 0)

                {

                    binBottles[i] = new Bin();

                    binBottles[i].brown = int.Parse(val);

                    flag++;

                }

                else if (flag == 1)

                {

                    binBottles[i].green = int.Parse(val);

                    flag++;

                }

                else

                {

                    binBottles[i].clear = int.Parse(val);

                    flag = 0;

                    i++;

                }

            }

           

            int[] pointer = new int[NumberOfBins];

            int minimalMoves = -1;

            //Initializing the pointer array

            for (i = 0; i < NumberOfBins; i++)

            {

                pointer[i] = i;

            }

            pointer[NumberOfBins – 1]–;

 

            //Permutation loop

            do

            {

                i = NumberOfBins – 1;

 

            label1:

 

                pointer[i]++;

                if (pointer[i] == NumberOfBins && i != 0)

                {

                    pointer[i] = 0;

                    i–;

                    goto label1;

                }

 

                else if (pointer[i] == NumberOfBins && i == 0)

                {

                    break;

                }

 

                else

                {

                    //Check pointer validity

                    int[] counter = new int[NumberOfBins];

                    flag = 0;

                    for (i = 0; i < NumberOfBins; i++)

                    {

                        counter[pointer[i]]++;

                        if (counter[pointer[i]] > 1)

                        {

                            flag = 1;

                            break;

                        }

                    }

 

                    if (flag == 0)

                    {

                        if (minimalMoves == -1)

                        {

                            minimalMoves = TotalMoves(binBottles[pointer[0]], binBottles[pointer[1]], binBottles[pointer[2]]);

                            color[pointer[0]] = ‘B’;

                            color[pointer[1]] = ‘C’;

                            color[pointer[2]] = ‘G’;

                        }

                        else

                        {

                            if (minimalMoves > TotalMoves(binBottles[pointer[0]], binBottles[pointer[1]], binBottles[pointer[2]]))

                            {

                                minimalMoves = TotalMoves(binBottles[pointer[0]], binBottles[pointer[1]], binBottles[pointer[2]]);

                                color[pointer[0]] = ‘B’;

                                color[pointer[1]] = ‘C’;

                                color[pointer[2]] = ‘G’;

                            }

                        }

                    }

                }

            } while (pointer[0] != NumberOfBins);

            Console.WriteLine("Minimal Number of Moves = " + minimalMoves);

            Console.WriteLine("Optimal Bin Arrangement = " + color[0] + ", " + color[1] + ", " + color[2]);

        }

        public static int TotalMoves(Bin Brown, Bin Clear, Bin Green)

        {

            int moves = 0;

            moves = moves + Clear.brown + Green.brown; //Brown move

            moves = moves + Brown.green + Clear.green; //Green move

            moves = moves + Brown.clear + Green.clear; //Clear move

            return moves;

        }

    }

}

Enter bottle arrangements in respective bins: 10 15 20 30 12 8 15 8 31
Minimal Number of Moves = 73
Optimal Bin Arrangement = G, B, C
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s