Ejemplo n.º 1
0
    def test_calculate_costs(self):
        node = PseudoCostBranchNode(small_branch.lp,
                                    small_branch.integerIndices)
        node.pseudo_costs = {}
        node._base_bound()

        # check that each fractional index gets proper pseudo cost instantiated
        # check that infeasible strong branch direction gets (0, 1) ie up and x >= 2
        for idx in [1, 2]:
            for strong_branch_node in node._strong_branch(idx).values():
                node._calculate_costs(strong_branch_node)
        for idx, direction in product([1, 2], ['up', 'down']):
            self.assertTrue(node.pseudo_costs[idx][direction]['times'] == 1)
            if idx == 1 and direction == 'down':
                self.assertTrue(node.pseudo_costs[idx][direction]['cost'] == 1)
            else:
                self.assertTrue(node.pseudo_costs[idx][direction]['cost'] == 0)

        # check that branched on index updates the instantiated value correctly
        rtn = node._base_branch(1)
        for direction, child_node in rtn.items():
            child_node.pseudo_costs = node.pseudo_costs
            child_node._base_bound()
            child_node._calculate_costs(child_node)
        for direction in ['up', 'down']:
            self.assertTrue(node.pseudo_costs[1][direction]['times'] == 2)
            if direction == 'down':
                self.assertTrue(node.pseudo_costs[1][direction]['cost'] == 1)
            else:
                self.assertTrue(node.pseudo_costs[1][direction]['cost'] == 0)
Ejemplo n.º 2
0
    def test_update_pseudo_costs(self):
        node = PseudoCostBranchNode(small_branch.lp,
                                    small_branch.integerIndices)
        node.pseudo_costs = {}
        node._base_bound()

        # for root node (sb_index = 2)
        # check we call strong branch once and calculate costs twice for each sb_index
        with patch.object(node, '_strong_branch') as sb, \
                patch.object(node, '_calculate_costs') as cc:
            sb.return_value = {
                'up':
                PseudoCostBranchNode(small_branch.lp,
                                     small_branch.integerIndices),
                'down':
                PseudoCostBranchNode(small_branch.lp,
                                     small_branch.integerIndices)
            }
            node._update_pseudo_costs()
            self.assertTrue(sb.call_count == 2)
            self.assertTrue(cc.call_count == 4)

        # branch on 2 (len(sb_index) = 1)
        # check we call strong branch len(sb_index) times and calc costs 2*len(sb_index) + 1
        rtn = node._base_branch(2)  # force x0 to go from int to fractional
        down_node = rtn['down']  # just do down bc up infeasible
        down_node.pseudo_costs = {
            1: {
                'up': {
                    'cost': 0,
                    'times': 1
                },
                'down': {
                    'cost': 1,
                    'times': 1
                }
            },
            2: {
                'up': {
                    'cost': 0,
                    'times': 1
                },
                'down': {
                    'cost': 0,
                    'times': 1
                }
            }
        }
        down_node._base_bound()
        with patch.object(down_node, '_strong_branch') as sb, \
                patch.object(down_node, '_calculate_costs') as cc:
            sb.return_value = {
                'up':
                PseudoCostBranchNode(small_branch.lp,
                                     small_branch.integerIndices),
                'down':
                PseudoCostBranchNode(small_branch.lp,
                                     small_branch.integerIndices)
            }
            down_node._update_pseudo_costs()
            self.assertTrue(sb.call_count == 1)
            self.assertTrue(cc.call_count == 3)