Ejemplo n.º 1
0
        public void run_algorithm(string path)
        {
            Stopwatch stopWatch = new Stopwatch();

            _chosenItems = "";
            ReadDataFromFile(path);
            Console.WriteLine("Probelm: " + path);
            _counter = 0;
            switch (_neglectedConstrain)
            {
            case NeglectedConstrain.Capacity:
                _estimationBound = (uint)Weights.Sum(num => num); break;

            case NeglectedConstrain.Integrality:
                BuildItemsList(true);
                _estimationBound = calc_estimate_neglecting_integrality(); break;
            }
            _bestLeaf = new Node(0, NumOfknapsacks, Capacities.ToArray(), 0, 0);
            short[] rooms = new short[NumOfknapsacks];
            Array.Copy(Capacities.ToArray(), rooms, NumOfknapsacks);
            Node root = new Node(0, NumOfknapsacks, rooms, _estimationBound, 0);

            //solving while iterating between Branch and Bound
            switch (_searchAlgorithm)
            {
            case SearchAlgorithm.BestFirstSearch:
                stopWatch.Start();
                BestFirstSearch(root);
                break;

            case SearchAlgorithm.DepthFirstSearch:
                stopWatch.Start();
                DepthFirstSearch(root);
                break;
            }
            stopWatch.Stop();
            double totalTicks = (stopWatch.ElapsedTicks / (double)Stopwatch.Frequency) * 1000;

            stopWatch.Restart();
            print_result_details();
            Console.WriteLine("Total Ticks " + (long)totalTicks + "\n");
        }
Ejemplo n.º 2
0
        private uint calc_estimate_neglecting_integrality(string chosenItems = "")
        {
            double estimateBound = 0;
            //avilableItems holds the current items according to the chosen items so far in the recursive search
            List <Item> avilableItems = new List <Item>();
            List <int>  numbers       = new List <int>();

            if (!chosenItems.Equals("")) //no items were selected yet. add all items.
            {
                var binaryNumbers = chosenItems.Replace(" ", "");
                foreach (var bin in binaryNumbers)
                {
                    numbers.Add(Int32.Parse(bin.ToString()));
                }
                for (int i = 0; i < numbers.Count; i++)
                {
                    if (numbers[i] == 1)
                    {
                        avilableItems.Add(Items[i]);
                    }
                }
            }
            for (int i = numbers.Count; i < NumOfItems; i++) // add the rest of the items
            {
                avilableItems.Add(Items[i]);
            }
            var itemsSorted = avilableItems.OrderByDescending(x => x.DensitiesAvg);

            short[] rooms = new short[NumOfknapsacks];
            Array.Copy(Capacities.ToArray(), rooms, NumOfknapsacks);
            foreach (var item in itemsSorted)
            {
                bool canBeAddedToAllSacks = true;
                //check if this item cab be added to all knapsacks
                for (int j = 0; j < NumOfknapsacks; j++)
                {
                    if (rooms[j] < item.Constrains[j])
                    {
                        canBeAddedToAllSacks = false;
                        break;
                    }
                }
                if (canBeAddedToAllSacks == true)
                {
                    for (int j = 0; j < NumOfknapsacks; j++)
                    {
                        rooms[j] = (short)(rooms[j] - item.Constrains[j]);
                    }
                    estimateBound += item.Weight;
                }
                else //add a fraction of this item to all knapsacks
                {
                    double fraction = Int16.MaxValue;
                    for (int j = 0; j < NumOfknapsacks; j++)
                    {
                        if (item.Constrains[j] != 0)
                        {
                            double temp = (rooms[j] / (double)item.Constrains[j]) * item.Weight;
                            if (fraction > temp)
                            {
                                fraction = temp;                  //take the smallest fraction
                            }
                        }
                    }
                    estimateBound += fraction;
                    break; //stop because we filled up at least one room
                }
            }
            return((uint)estimateBound);
        }