Exemplo n.º 1
0
        private static bool[] Trace_selected_items(Problem problem, VirtualTable table)
        {
            var selections = new bool[problem.Items.Length];

            var item_col = problem.Items.Length;
            var capacity_of_row = problem.Capacity;
            while (item_col > 0 && capacity_of_row > 0)
            {
                var i = item_col - 1;

                var currObj = table[item_col, capacity_of_row];
                var prevObj = table[item_col - 1, capacity_of_row];

                if (currObj != prevObj)
                {
                    selections[i] = true;
                    capacity_of_row -= problem.Items[i].Weight;
                }

                File.AppendAllText("dynamicsolver.log", string.Format("Trace: {0}\r\n", item_col));
                item_col--;
            }

            return selections;
        }
Exemplo n.º 2
0
        private static int Calc_objective_values(Problem problem)
        {
            var start = DateTime.Now;

            var curr_col = new int[problem.Capacity+1];
            for (var item_col = 1; item_col <= problem.Items.Length; item_col++)
            {
                var prev_col = curr_col;
                curr_col = new int[problem.Capacity+1];

                for (var capacity_of_row = 1; capacity_of_row <= problem.Capacity; capacity_of_row++)
                {
                    var i = item_col - 1;

                    if (problem.Items[i].Weight <= capacity_of_row)
                    {
                        var prevObj = prev_col[capacity_of_row];
                        var currObj = problem.Items[i].Value +
                                      prev_col[capacity_of_row - problem.Items[item_col - 1].Weight];
                        curr_col[capacity_of_row] = Math.Max(prevObj, currObj);
                    }
                    else
                        curr_col[capacity_of_row] = prev_col[capacity_of_row];
                }

                File.AppendAllText("hybridsolver.log", string.Format("Calc: {0}, {1}\r\n", item_col, DateTime.Now));
            }

            return curr_col[problem.Capacity];
        }
Exemplo n.º 3
0
        public static Problem Parse(string[] text)
        {
            var p = new Problem();

            p.Capacity = int.Parse(text[0].Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)[1].Trim());
            var n = int.Parse(text[0].Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)[0].Trim());
            p.Items = new Item[n];
            var items = new List<Item>();
            for (var i = 0; i < n; i++)
                p.Items[i] = Item.Parse(text[i + 1]);
            return p;
        }
Exemplo n.º 4
0
        public static Solution Solve(Problem problem)
        {
            File.WriteAllText("dynamicsolver.log", "progress...\r\n");

            using (var table = new VirtualTable(problem.Items.Length + 1, problem.Capacity + 1))
            {

                Calc_objective_values(problem, table);
                var selection = Trace_selected_items(problem, table);

                return new Solution
                    {
                        ObjectiveValue = table[problem.Items.Length, problem.Capacity],
                        ItemSelections = selection
                    };
            }
        }
Exemplo n.º 5
0
        private static int Calc_objective_values(Problem problem)
        {
            File.Delete("dynamicfwdsolver_diffs.log");

            var total_diff_count = 0;

            var start = DateTime.Now;

            var curr_col = new int[problem.Capacity+1];
            for (var item_col = 1; item_col <= problem.Items.Length; item_col++)
            {
                var prev_col = curr_col;
                curr_col = new int[problem.Capacity+1];

                for (var capacity_of_row = 1; capacity_of_row <= problem.Capacity; capacity_of_row++)
                {
                    var i = item_col - 1;

                    if (problem.Items[i].Weight <= capacity_of_row)
                    {
                        var prevObj = prev_col[capacity_of_row];
                        var currObj = problem.Items[i].Value +
                                      prev_col[capacity_of_row - problem.Items[item_col - 1].Weight];
                        curr_col[capacity_of_row] = Math.Max(prevObj, currObj);
                    }
                    else
                        curr_col[capacity_of_row] = prev_col[capacity_of_row];
                }

                File.AppendAllText("dynamicfwdsolver.log", string.Format("Calc {0}, {1}\r\n", item_col, DateTime.Now.Subtract(start)));

                var differences = new List<int>();
                for (var i = 0; i < curr_col.Length; i++)
                    if (prev_col[i] != curr_col[i]) differences.Add(i);
                File.AppendAllText("dynamicfwdsolver_diffs.log", string.Format("item {0}: {1}\r\n", item_col - 1, differences.Count));

                total_diff_count += differences.Count;

                _differencesStore.Store_differences(item_col-1, differences.ToArray());
            }

            File.AppendAllText("dynamicfwdsolver_diffs.log", string.Format("total: {0}, avg: {1}\r\n", total_diff_count, total_diff_count / problem.Items.Length));

            return curr_col[problem.Capacity];
        }
Exemplo n.º 6
0
        public static Solution Solve(Problem problem)
        {
            File.WriteAllText("hybridsolver.log", "progress...\r\n");

            var objectiveValue = Calc_objective_values(problem);
            var selection = Decompose_objective_value(problem, objectiveValue);

            var sum = 0;
            for (var i = 0; i < selection.Length; i++)
                if (selection[i]) sum += problem.Items[i].Value;

            File.AppendAllText("hybridsolver.log", string.Format("B&B result: {0}, found: {1}\r\n", objectiveValue, sum));

            return new Solution
            {
                ObjectiveValue = objectiveValue,
                ItemSelections = selection,
                IsExact = sum == objectiveValue
            };
        }
Exemplo n.º 7
0
        public static Solution Solve(Problem problem)
        {
            using (_differencesStore = new DifferencesStore())
            {
                File.WriteAllText("dynamicfwdsolver.log", "progress...\r\n");

                var objectiveValue = Calc_objective_values(problem);
                var selection = Trace_item_selection(problem);

                var sum = 0;
                for (var i = 0; i < selection.Length; i++)
                    if (selection[i]) sum += problem.Items[i].Value;

                File.AppendAllText("dynamicfwdsolver.log",
                                   string.Format("Trace result: {0}, found: {1}\r\n", objectiveValue, sum));

                return new Solution
                    {
                        ObjectiveValue = objectiveValue,
                        ItemSelections = selection,
                        IsExact = sum == objectiveValue
                    };
            }
        }
Exemplo n.º 8
0
        private static void Calc_objective_values(Problem problem, VirtualTable table)
        {
            var start = DateTime.Now;

            for (var item_col = 1; item_col <= problem.Items.Length; item_col++)
            {
                for (var capacity_of_row = 1; capacity_of_row <= problem.Capacity; capacity_of_row++)
                {
                    var i = item_col - 1;

                    if (problem.Items[i].Weight <= capacity_of_row)
                    {
                        var prevObj = table[item_col - 1, capacity_of_row];
                        var currObj = problem.Items[i].Value +
                                      table[item_col - 1, capacity_of_row - problem.Items[item_col - 1].Weight];
                        table[item_col, capacity_of_row] = Math.Max(prevObj, currObj);
                    }
                    else
                        table[item_col, capacity_of_row] = table[item_col - 1, capacity_of_row];
                }

                File.AppendAllText("dynamicsolver.log", string.Format("Calc: {0}, {1}\r\n", item_col, DateTime.Now));
            }
        }
Exemplo n.º 9
0
        // ergebnis ks_19_0: 4461 knoten besucht
        private static bool[] Decompose_objective_value(Problem problem, int objectiveValue)
        {
            const double ERROR_MARGIN_PCT = 0.00002;
            _errorMargin = (int)(objectiveValue * ERROR_MARGIN_PCT);

            var sorted_items = problem.Items.Select((item, i) => new SortedItem{Value=item.Value, Weight=item.Weight, Index=i})
                                            .OrderByDescending(item => item.Value).ToArray();
            for (var i = sorted_items.Length - 2; i >= 0; i--)
                sorted_items[i].MaxTotal = sorted_items[i + 1].Value + sorted_items[i + 1].MaxTotal;

            var selected_item_indexes = new Stack<int>();

            nodes_visited = 0;
            Decompose_by_branching_and_bounding(objectiveValue, problem.Capacity, sorted_items, -1, selected_item_indexes);
            File.AppendAllText("hybridsolver.log", string.Format("B&B: {0}, {1}\r\n", nodes_visited,  DateTime.Now));

            var selection = new bool[sorted_items.Length];
            selected_item_indexes.ToList().ForEach(i => selection[i]=true);

            return selection;
        }
Exemplo n.º 10
0
        private static bool[] Trace_item_selection(Problem problem)
        {
            var selected_items = new bool[problem.Items.Length];

            var remaining_capacity = problem.Capacity;
            for (var i = problem.Items.Length - 1; i >= 0; i--)
            {
                var differences = _differencesStore.Load_differences(i);
                if (differences.Length > 0)
                {
                    if (differences.Contains(remaining_capacity))
                    {
                        selected_items[i] = true;
                        remaining_capacity -= problem.Items[i].Weight;
                    }
                }
            }

            return selected_items;
        }