Sunday, March 29, 2015

A practical solution to Knapsack problem with the list of items selected (using Dynamic Programming)

A practical solution to Knapsack problem with the list of items selected (using Dynamic Programming)



namespace ConsoleKnapsack
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    class Program
    {
        static void Main(string[] args)
        {
            var knapsack = new Knapsack();
            double ask = 10;

            Stopwatch watch = new Stopwatch();
            watch.Start();
            var value = knapsack.GetMaxValueForSize(ask);
            watch.Stop();

            double totalSize = 0;
            foreach (var take in value.Item2.Keys.ToList())
            {
                if (value.Item2[take])
                {
                    Console.Write(take + "|" + knapsack.items[take].Item1 + "|" + knapsack.items[take].Item2 + " ");
                    totalSize += knapsack.items[take].Item1;
                }
            }

            Console.WriteLine("");
            Console.WriteLine("Total Size = " + totalSize);
            Console.WriteLine("Asked for Size = " + ask);
            Console.WriteLine("Max Calculated Value = " + value.Item1 + "\n");
            Console.WriteLine("Dictionary Hit Count = " + knapsack.HitCount);
            Console.WriteLine("Elapsed = " + watch.ElapsedMilliseconds);
            Console.ReadLine();
        }
    }


    class Knapsack
    {
        public List<Tuple<double, double>> items = new List<Tuple<double, double>>
                                                {
                                                    new Tuple<double, double>(1.1, 1.2),
                                                    new Tuple<double, double>(1.3, 1.4),
                                                    new Tuple<double, double>(0.5, 0.8),
                                                    new Tuple<double, double>(1.1, 2.1),
                                                    new Tuple<double, double>(0.5, 0.5),
                                                    new Tuple<double, double>(1.0, 1.2),
                                                    new Tuple<double, double>(1.4, 1.6),
                                                    new Tuple<double, double>(0.6, 0.8),
                                                    new Tuple<double, double>(1.2, 1.1),
                                                    new Tuple<double, double>(0.9, 1.5),
                                                };

        public long HitCount = 0;


        public Knapsack()
        {
            this.items = new List<Tuple<double, double>>();
            var rand = new Random();
            for (long i = 0; i < 50; i++)
            {
                double weight = 0.01 + rand.Next(0, 100) / 100.0;
                double size = rand.Next(0, 100) / 100.0;

                items.Add(new Tuple<double, double>(weight, size));
            }
        }

        Dictionary<double, Dictionary<double, Tuple<double, Dictionary<int, bool>>>> solutions = new Dictionary<double, Dictionary<double, Tuple<double, Dictionary<int, bool>>>>();
        // Tuple - size, value

        public Tuple<double, Dictionary<int, bool>> GetMaxValueForSize(double size)
        {
            return this.GetValue(0, size, new Dictionary<int, bool>());
        }

        private Tuple<double, Dictionary<int, bool>> GetValue(int startItem, double size, Dictionary<int, bool> takes)
        {
            if (startItem >= items.Count)
            {
                return new Tuple<double, Dictionary<int, bool>>(0, takes);
            }

            //var dictionaryKey = startItem.ToString() + "|" + size.ToString();
            if (this.solutions.ContainsKey(startItem))
            {
                var dict = this.solutions[startItem];
                if (dict.ContainsKey(size))
                {
                    this.HitCount++;
                    return dict[size];
                }
            }

            // if we take the item
            var item = items.ElementAt(startItem);
            var weight = item.Item1;
            var value = item.Item2;

            Tuple<double, Dictionary<int, bool>> firstTaken;
            double firstValue;
            if (weight < size)
            {
                var newTakes = new Dictionary<int, bool>(takes);
                newTakes[startItem] = true;
                firstTaken = this.GetValue(startItem + 1, size - weight, newTakes);
                // firstTaken = this.GetValue(startItem + 1, size - weight, takes);
                firstValue = firstTaken.Item1 + value;
            }
            else
            {
                firstTaken = new Tuple<double, Dictionary<int, bool>>(0, takes);
                firstValue = 0;
            }

            // if we skip the item
            var firstSkipped = this.GetValue(startItem + 1, size, takes);

            Tuple<double, Dictionary<int, bool>> returnValue;
            if (firstValue >= firstSkipped.Item1)
            {
                returnValue = new Tuple<double, Dictionary<int, bool>>(firstValue, firstTaken.Item2);
            }
            else
            {
                returnValue = firstSkipped;
            }

            if (this.solutions.ContainsKey(startItem))
            {
                this.solutions[startItem].Add(size, returnValue);
            }
            else
            {
                var dict = new Dictionary<double, Tuple<double, Dictionary<int, bool>>>();
                dict.Add(size, returnValue);
                this.solutions.Add(startItem, dict);
            }

            //this.solutions.Add(dictionaryKey, returnValue);
            return returnValue;
        }
    }
}

Saturday, August 9, 2014

Static IP for raspberry Pi

Two essential things to do when you get a raspberry pi is to set up wifi access for it and configure it to a static ip address so that you can ssh into it without guessing the ip. To be honest being connected to a HDMI monitor all the time is not going to be practical for most of the projects.

Since the board does not come with built-in wifi support you need to get a USB Wifi adapter for the board, which you have probably done already.

Refer to this if you are having trouble setting up the USB Wifi adapter.
http://www.maketecheasier.com/setup-wifi-on-raspberry-pi/

Using the GUI makes the Wifi configuration a lot easier. Once you have set that up your
/etc/network/interfaces file is going to look like almost the following

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp


All you need to do is edit the last line to make the ip configuration static and add the lines that follow.

iface default inet static
        address 192.168.0.141
        netmask 255.255.255.0
        network 192.168.0.0
        gateway 129.168.0.1

Depending on what ip you want modify the address.