Example #1
0
        public static BB_Node create_BB_node(Tuple <int, int> varToFix, bool fix1, BB_Node parent, double UB)
        {
            statistics.n_BB_nodes += 1;
            BB_Node no = new BB_Node(varToFix, fix1, parent, UB);

            return(no);
        }
Example #2
0
        public static double update_UB(BB_Node node)
        {
            if (node.left_child == null || node.right_child == null)
            {
                return(node.UB);
            }

            return(node.UB = Math.Min(update_UB(node.left_child), update_UB(node.right_child)));
        }
Example #3
0
 public BB_Node(Tuple <int, int> varToFix, bool fix1, BB_Node parent, double UB)
 {
     this.varToFix    = varToFix;
     this.fix1        = fix1;
     this.parent      = parent;
     this.left_child  = null;
     this.right_child = null;
     this.UB          = UB;
 }
Example #4
0
        public static void add_branch_contraints(BB_Node n_i)
        {
            if (n_i.parent != null)
            {
                if (n_i.fix1)
                {
                    X[n_i.varToFix.Item1, n_i.varToFix.Item2].LB = 1;
                }
                else
                {
                    X[n_i.varToFix.Item1, n_i.varToFix.Item2].UB = 0;
                }

                add_branch_contraints(n_i.parent);
            }
        }
Example #5
0
        public static BB_Node select_BB_node(List <BB_Node> L)
        {
            BB_Node selected_node = null;
            double  max_UB        = 0.0;

            foreach (BB_Node node in L)
            {
                if (node.UB > max_UB)
                {
                    max_UB        = node.UB;
                    selected_node = node;
                }
            }

            L.Remove(selected_node);

            return(selected_node);
        }
Example #6
0
        public static void BranchAndBound()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            build_initial_model(true);
            int[,] best_sol = statistics.solucao_heuristica;
            double z_ = statistics.heuristic_sol_val;

            BB_Node        n_0 = create_BB_node(new Tuple <int, int>(-1, -1), false, null, int.MaxValue);
            List <BB_Node> L   = new List <BB_Node>();

            add_BB_node(L, n_0);
            var     is_root_node = true;
            BB_Node root_node    = null;

            while (L.Count != 0)
            {
                if (is_root_node == false)
                {
                    update_UB(root_node);

                    if (root_node.UB >= z_ + e)
                    {
                        break;
                    }
                }


                stopwatch.Stop();
                statistics.spent_time_in_seconds = stopwatch.ElapsedMilliseconds;

                if (statistics.spent_time_in_seconds / 1000 > config.time_limit)
                {
                    time_limit_exceeded(best_sol, z_, root_node.UB);
                    return;
                }

                stopwatch.Start();

                BB_Node n_i = select_BB_node(L);
                clear_branch_contraints();
                add_branch_contraints(n_i);

                var    status = solve_LP();
                double z_i;

                if (status == 3)
                {
                    continue;
                }
                else
                {
                    z_i    = model.ObjVal;
                    n_i.UB = z_i;

                    if (is_root_node)
                    {
                        statistics.first_linear_relax_val = z_i;
                        root_node    = n_i;
                        is_root_node = false;
                    }
                }

                if (z_i >= z_ + e)
                {
                    continue;
                }

                if (is_integer())
                {
                    z_ = z_i;
                    copy_solution(best_sol);
                    continue;
                }

                Tuple <int, int> i_var = select_frac_var();
                n_i.left_child  = create_BB_node(i_var, true, n_i, z_i);
                n_i.right_child = create_BB_node(i_var, false, n_i, z_i);
                add_BB_node(L, n_i.left_child);
                add_BB_node(L, n_i.right_child);
            }

            stopwatch.Stop();
            statistics.spent_time_in_seconds = stopwatch.Elapsed.TotalSeconds;
            statistics.objective_value       = z_;
            print_solution(best_sol);
        }
Example #7
0
 public static void add_BB_node(List <BB_Node> L, BB_Node no)
 {
     L.Add(no);
 }