Ejemplo n.º 1
0
def select_var_to_branch(node,depth): # TO DO ; proprely
    
    data_size = get_data_size()
    
    num_features = get_num_features()

    num_leafs = 2**depth

    num_nodes = num_leafs-1
    
    for l in range(num_leafs):
        
        for t in range(get_num_targets()):
            
            if not already_branch(node,'p',(l,t)):
                
                return 'p', (l,t)
    
    for i in range(num_features):
        
        for j in range(num_nodes):
            
            if not already_branch(node,'f',(i,j)):
                
                return 'f', (i, j)
            
    input("CAREFUL, THIS PART IS NOT CORRECT, MASTER WILL BE RE-BUILT")
    
    for r in range(data_size):
        
        for l in range(num_leafs):
            
            if not already_branch(node,'row_leaf',(r,l)):
                
                return 'row_leaf', (r, l)
Ejemplo n.º 2
0
    def select_var_to_branch(self):  #return a tuple (row, leaf)

        global VARS

        best_row, best_leaf, best_score = None, None, 0

        sol = self.prob.solution.get_values()

        for r in range(get_data_size()):

            for l in range(len(self.segments_set)):

                score = 0

                for s in self.segments_set[l]:

                    if r in s:

                        score = score + sol[VARS["segment_leaf_" + str(s) +
                                                 "_" + str(l)]]

                if score > best_score and score < 0.99:

                    best_row, best_leaf, best_score = r, l, score

        return best_row, best_leaf
Ejemplo n.º 3
0
def display_RMP_solution_primal(depth, prob, CGiter, segments_set):

    print("--------------------- RMP(" + str(CGiter) +
          ") Solution ---------------------")

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    num_features = get_num_features()

    data_size = get_data_size()

    # node n had a boolean test on feature f, boolean. On the paper: f_{i,j}

    for j in range(num_nodes):

        for i in range(num_features):

            print("f_" + str(i) + "_" + str(j) + "*= " + "%.2f" % round(
                prob.solution.get_values("node_feature_" + str(j) + "_" +
                                         str(i)), 2))

    # value used in the boolean test in node n, integer. On the paper: c_{j}

    for j in range(num_nodes):

        print("c_" + str(j) + "*= " + "%.2f" %
              round(prob.solution.get_values("node_constant_" + str(j)), 2))

    # leaf l predicts type t, boolean. On the paper: p_{l,t}

    for l in range(num_leafs):

        for t in range(get_num_targets()):

            print("p_" + str(l) + "_" + str(t) + "*= " + "%.2f" % round(
                prob.solution.get_values("prediction_type_" + str(t) + "_" +
                                         str(l)), 2))

    # row error, variables to minimize. On the paper: e_{r}

    for r in range(data_size):

        print("e_" + str(r) + "*= " + "%.2f" %
              round(prob.solution.get_values("row_error_" + str(r)), 2))

    for l in range(num_leafs):  # x_{l,s}

        for s in range(len(segments_set[l])):

            print("x_" + str(l) + "_" + str(s) + "*= " + "%.2f" % round(
                prob.solution.get_values("segment_leaf_" + str(s) + "_" +
                                         str(l)), 2))

    print("Objective value :", round(prob.solution.get_objective_value(), 2))

    print("--------------------- RMP(" + str(CGiter) +
          ") Solution ---------------------")
Ejemplo n.º 4
0
def solve_ILP(depth, C_set):

    prob = cplex.Cplex()

    data_size = get_data_size()

    prob.objective.set_sense(prob.objective.sense.minimize)

    var_names, var_types, var_lb, var_ub, var_obj = create_variables(
        depth, C_set)

    prob.variables.add(obj=var_obj,
                       lb=var_lb,
                       ub=var_ub,
                       types=var_types,
                       names=var_names)

    row_names, row_values, row_right_sides, row_senses = create_rows(
        depth, C_set)

    prob.linear_constraints.add(lin_expr=row_values,
                                senses=row_senses,
                                rhs=row_right_sides,
                                names=row_names)

    prob.parameters.emphasis.mip.set(1)

    #prob.parameters.advance.set(2)

    #prob.parameters.mip.strategy.branch.set(1)
    #prob.parameters.mip.strategy.backtrack.set(1.0)
    #prob.parameters.mip.strategy.nodeselect.set(2)
    prob.parameters.mip.strategy.variableselect.set(-1)
    #prob.parameters.mip.strategy.bbinterval.set(0)
    #prob.parameters.mip.strategy.rinsheur.set(50)
    #prob.parameters.mip.strategy.lbheur.set(1)
    #prob.parameters.mip.strategy.probe.set(3)

    #prob.parameters.preprocessing.presolve.set(1)

    prob.solve()

    display_prob_lite(prob, "primal")

    print

    print "Solution status = ", prob.solution.get_status(
    ), ":", prob.solution.status[prob.solution.get_status()]

    if "infeasible" in prob.solution.status[prob.solution.get_status()]:

        return []

    print "Solution value  = ", prob.solution.get_objective_value()

    print "Accuracy = ", (data_size -
                          prob.solution.get_objective_value()) / data_size
Ejemplo n.º 5
0
def display_pricing_all_at_once(depth, prob):

    data_size = get_data_size()

    num_leafs = 2**depth

    num_features = get_num_features()

    for leaf in range(num_leafs):

        for r in range(data_size):

            print("z_" + str(r) + "_" + str(leaf) + "*= " + "%.2f" % round(
                prob.solution.get_values("row_" + str(leaf) + "_" + str(r)), 2)
                  )

    # kappa_{i,r,l}, indicate the min feature of row r

    for leaf in range(num_leafs):

        for i in range(num_features):

            for r in range(data_size):

                if round(
                        prob.solution.get_values("kappa_" + str(leaf) + "_" +
                                                 str(r) + "_" + str(i)),
                        2) == 1.0:

                    print("ka_" + str(i) + "_" + str(r) + "_" + str(leaf) +
                          "*= " + "%.2f" % round(
                              prob.solution.get_values("kappa_" + str(leaf) +
                                                       "_" + str(r) + "_" +
                                                       str(i)), 2) +
                          " feat value: " + str(get_feature_value(r, i)))

    # omega_{i,r,l}, indicate the max feature of row r

    for leaf in range(num_leafs):

        for i in range(num_features):

            for r in range(data_size):

                if round(
                        prob.solution.get_values("omega_" + str(leaf) + "_" +
                                                 str(r) + "_" + str(i)),
                        2) == 1.0:

                    print("om_" + str(i) + "_" + str(r) + "_" + str(leaf) +
                          "*= " + "%.2f" % round(
                              prob.solution.get_values("omega_" + str(leaf) +
                                                       "_" + str(r) + "_" +
                                                       str(i)), 2) +
                          " feat value: " + str(get_feature_value(r, i)))

    print("Objective value :", round(prob.solution.get_objective_value(), 2))
Ejemplo n.º 6
0
def solve_pricing(
        prob, segments_set, branched_rows, branched_leaves, ID,
        pricing_method):  #return a tuple (segments_to_be_added, convergence)

    num_leafs = len(segments_set)

    segments_to_be_added, obj_values = [], []

    if pricing_method == 1:

        for l in range(num_leafs):  # TODO ; implement new pricing_method

            segments, value = solve_pricing_given_leaf(prob, l, branched_rows,
                                                       branched_leaves, ID,
                                                       segments_set[l])

            segments_to_be_added.append(segments)

            obj_values.append(value)

            print("Reduced cost ", str(value))

    elif pricing_method == 2:

        excl_rows = []

        for l in range(num_leafs):

            if l != num_leafs - 1:

                segment, value = solve_pricing_given_leaf(
                    prob, l, branched_rows, branched_leaves, ID,
                    segments_set[l], excl_rows)

                segments_to_be_added.append(segment)

                excl_rows.extend(segment)

                obj_values.append(value)

                print(segment)

                print("Reduced cost ", str(value))

            else:

                segment = [
                    i for i in range(get_data_size()) if i not in excl_rows
                ]

                segments_to_be_added.append(segment)

                print(segment)

    return segments_to_be_added, ((min(obj_values) - int(pricing_method == 2))
                                  > -0.01)
Ejemplo n.º 7
0
def extract_rows_pricing(
        pricing_prob):  #return the segment given by the pricing pb

    seg, sol = [], pricing_prob.solution.get_values()

    for r in range(get_data_size()):

        if sol[r] == 1:

            seg.append(r)

    return seg
Ejemplo n.º 8
0
def random_partition(depth):

    data_size = get_data_size()

    segments_set = [[[]] for l in range(2**depth)]

    for r in range(data_size):

        l = int((2**depth) * random())

        segments_set[l][0].append(r)

    return segments_set
Ejemplo n.º 9
0
def print_solution(sol, depth):

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    for j in range(num_nodes):
        print("c_{j}", j, sol[VARS["node_constant_" + str(j)]])

    for r in range(data_size):

        print("Path: (pt_{r,j} ", "r=", r)

        for j in range(num_nodes):

            print(sol[VARS["path_node_" + str(j) + "_" + str(r)]])

        print("Final leaf:")

        for l in range(num_leafs):

            print(sol[VARS["path_leaf_" + str(l) + "_" + str(r)]])
        print

    for r in range(data_size):

        print("Row error (" + str(r) + ") ", sol[VARS["row_error_" + str(r)]])

    for t in range(get_num_targets()):

        for l in range(num_leafs):

            print(
                "p_{l,t} l=", l, " t=", t, " ",
                sol[VARS["prediction_type_" + str(int(TARGETS[t])) + "_" +
                         str(l)]])

    for j in range(num_nodes):

        for i in range(num_features):

            print("f_{i,j} i=", i, " j=", j,
                  sol[VARS["node_feature_" + str(j) + "_" + str(i)]])
def extract_rows_pricing_all_at_once(pricing_prob_all_at_once, num_leafs):

    seg, datasize = [[] for l in range(num_leafs)], get_data_size()

    for l in range(num_leafs):

        for r in range(datasize):

            if 0.99 <= pricing_prob_all_at_once.solution.get_values(
                    "row_" + str(l) + "_" + str(r)) <= 1.01:

                seg[l].append(r)
    """
                
    from learn_tree_funcs import get_feature_value, get_num_features
    
    
    for i in range(get_num_features()):
        
        for l in range(len(seg)):
                        
            for r in range(datasize):
                                
                if pricing_prob_all_at_once.solution.get_values("kappa_" + str(l) + "_"  + str(r) + "_" + str(i))==1:
                    
                    #print(l,r,i,r in seg[l])
                    
                    #print(min([get_feature_value(r2,i) for r2 in seg[l]]),get_feature_value(r,i))
            
                    assert min([get_feature_value(r2,i) for r2 in seg[l]]) == get_feature_value(r,i)
                    
                if pricing_prob_all_at_once.solution.get_values("omega_" + str(l) + "_"  + str(r) + "_" + str(i))==1:
                    
                    #print(l,r,i,r in seg[l])
                    
                    #print(min([get_feature_value(r2,i) for r2 in seg[l]]),get_feature_value(r,i))
            
                    assert max([get_feature_value(r2,i) for r2 in seg[l]]) == get_feature_value(r,i)
    """
    return seg
def create_variables_pricing_all_at_once(depth,master_prob):
    
    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    data_size = get_data_size()
    
    num_leafs = 2**depth
        
    # z_{r,l}, main decision variables
    
    for l in range(num_leafs):

        for r in range(data_size):
    
            var_names.append("row_" + str(l) + "_" + str(r))
    
            var_types = var_types + "B"
    
            var_lb.append(0)
    
            var_ub.append(1)
                    
            var_obj.append(compute_C(depth,r,l,master_prob))
    
            var_value = var_value + 1
            
    return var_names, var_types, var_lb, var_ub, var_obj
Ejemplo n.º 12
0
def create_variables_pricing(depth, master_prob, leaf):

    global VARS2

    VARS2 = {}

    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    duals = master_prob.solution.get_dual_values()

    #compute useful sums of dual values

    A_i_l, B_i_l = [], []

    for i in range(num_features):

        s = 0

        for j in range(num_nodes):

            left_leaves = get_left_leafs(j, num_nodes)

            if leaf in left_leaves:

                idx = left_leaves.index(leaf)

                s = s + duals[i * num_features + j * num_nodes + idx]

            print(s)

            if s != 0:

                input()

        A_i_l.append(-s)

    for i in range(num_features):

        s = 0

        for j in range(num_nodes):

            right_leaves = get_right_leafs(j, num_nodes)

            if leaf in right_leaves:

                idx = right_leaves.index(leaf)

                s = s + duals[constraint_indicators[1] + i * num_features +
                              j * num_nodes + idx]

            print(s)

            if s < 0:

                input()

        B_i_l.append(s)

    # z_{r}, main decision variables

    for r in range(data_size):

        VARS2["row_" + str(r)] = var_value

        var_names.append("#" + str(var_value))

        var_types = var_types + "B"

        var_lb.append(0)

        var_ub.append(1)

        #print(r,leaf,"C_{r,l} ",duals[constraint_indicators[2] + r] + duals[constraint_indicators[4] + r*num_leafs + leaf])

        var_obj.append(duals[constraint_indicators[2] + r] +
                       duals[constraint_indicators[4] + r * num_leafs + leaf])

        var_value = var_value + 1

    # kappa_{i,r}, indicate the min feature of row r

    for i in range(num_features):

        for r in range(data_size):

            VARS2["kappa_" + str(r) + "_" + str(i)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(-B_i_l[i] * get_feature_value(r, i))

            var_value = var_value + 1

    # omega_{i,r}, indicate the max feature of row r

    for i in range(num_features):

        for r in range(data_size):

            VARS2["omega_" + str(r) + "_" + str(i)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(A_i_l[i] * get_feature_value(r, i))

            var_value = var_value + 1

    return VARS2, var_names, var_types, var_lb, var_ub, var_obj
Ejemplo n.º 13
0
def solve_pricing(
    depth, prob, segments_set, branch_var, branch_index, ID, pricing_method
):  #return a triple (segments_to_be_added, convergence, min(red_cost))

    from BaP_Node import count_iter

    num_leafs = len(segments_set)

    segments_to_be_added, obj_values, segments_to_be_added_ordered = [], [], []

    if pricing_method == 1:

        for l in range(num_leafs):  # TODO ; implement new pricing_method

            segments, value = solve_pricing_given_leaf(depth, prob, l,
                                                       branch_var,
                                                       branch_index, ID,
                                                       segments_set[l])

            segments_to_be_added_ordered.append(segments)

            obj_values.append(value)

            if value < 500:

                plt.scatter(count_iter, value, color=color_leaf(l))

                plt.pause(0.01)

            print("Reduced cost for leaf " + str(l) + " :", str(value))

            #print(segments)

    elif pricing_method == 2:

        excl_rows, remember_order = [], []

        shuffle_leaves = range(num_leafs)

        shuffle(shuffle_leaves)

        for l in range(num_leafs):

            true_l = shuffle_leaves[l]

            if l != num_leafs - 1:

                segment, value = solve_pricing_given_leaf(
                    depth, prob, l, branch_var, branch_index, ID,
                    segments_set[l])

                segments_to_be_added.append(segment)

                excl_rows.extend(segment)

                obj_values.append(value)

                #print(segment)

                plt.scatter(count_iter, value, color=color_leaf(true_l))

                plt.pause(0.01)

                #print(segment)

                print("Reduced cost for leaf " + str(true_l) + " :",
                      str(value))

            else:

                segment = [
                    i for i in range(get_data_size()) if i not in excl_rows
                ]

                segments_to_be_added.append(segment)

                #print(segment)

            remember_order.append(true_l)

        segments_to_be_added_ordered = [
            x for _, x in sorted(zip(remember_order, segments_to_be_added))
        ]

    elif pricing_method == 3:

        segments, value = solve_pricing_all_at_once(depth, prob, branch_var,
                                                    branch_index, ID,
                                                    segments_set)

        segments_to_be_added_ordered = segments

        obj_values.append(value)

        plt.scatter(count_iter, value, color='k')

        plt.pause(0.01)

        #print(prob.solution.get_dual_values(),segments)

        print("Reduced cost for partition : ", str(value))

    elif pricing_method == 4:

        l = 1

        segments, value = solve_pricing_given_leaf(depth, prob, l, branch_var,
                                                   branch_index, ID,
                                                   segments_set[l])

        segments_to_be_added_ordered.append(segments)

        obj_values.append(value)

        if value > -200:

            plt.scatter(count_iter, value, color=color_leaf(l))

            plt.pause(0.01)

        print("Reduced cost for leaf " + str(l) + " :", str(value))

        segments_to_be_added_ordered.append([])

        #print(segments)

    return segments_to_be_added_ordered, ((max(obj_values) < 0.01)
                                          and (pricing_method
                                               == 1)), max(obj_values)
Ejemplo n.º 14
0
def lpdtree(depth):

    global SORTED_FEATURE, inputstart, inputtime, inputpolish

    prob = cplex.Cplex()

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    try:

        prob.objective.set_sense(prob.objective.sense.minimize)

        var_names, var_types, var_lb, var_ub, var_obj = create_variables(
            depth, prob)

        print "num_vars", len(var_names)

        prob.variables.add(obj=var_obj, lb=var_lb, ub=var_ub,
                           types=var_types)  #, names = var_names)

        row_names, row_values, row_right_sides, row_senses = create_rows(depth)

        print "num_rows", len(row_names)

        prob.linear_constraints.add(lin_expr=row_values,
                                    senses=row_senses,
                                    rhs=row_right_sides,
                                    names=row_names)

        order_set = []

        for n in range(num_nodes):

            for f in range(num_features):

                order_set.append(
                    (VARS["node_feature_" + str(n) + "_" + str(f)],
                     11 + 2 * ((num_nodes - get_num_parents(n, num_nodes))),
                     1))

        for n in range(num_nodes):

            order_set.append(
                (VARS["node_constant_" + str(n)],
                 10 + 2 * ((num_nodes - get_num_parents(n, num_nodes))), -1))

        prob.order.set(order_set)

        if inputstart == 1:

            col_var_names, col_var_values = get_start_solutions_new(depth)

            #return get_start_solutions_new(depth)

            prob.MIP_starts.add(
                [col_var_names, col_var_values], 1
            )  #prob.MIP_starts.effort_level.auto)#  level=1 forces cplex to take the entire solution as it is

        prob.write("test.lp")

        prob.parameters.emphasis.mip.set(1)

        #prob.parameters.advance.set(2)

        #prob.parameters.mip.strategy.branch.set(1)
        #prob.parameters.mip.strategy.backtrack.set(1.0)
        #prob.parameters.mip.strategy.nodeselect.set(2)
        prob.parameters.mip.strategy.variableselect.set(-1)
        #prob.parameters.mip.strategy.bbinterval.set(0)
        #prob.parameters.mip.strategy.rinsheur.set(50)
        #prob.parameters.mip.strategy.lbheur.set(1)
        #prob.parameters.mip.strategy.probe.set(3)

        #prob.parameters.preprocessing.presolve.set(1)

        prob.parameters.mip.polishafter.time.set(inputtime - inputpolish)
        prob.parameters.timelimit.set(inputtime)

        prob.solve()

    except CplexError, exc:

        print exc

        return []
Ejemplo n.º 15
0
def get_start_solutions_new(depth):

    global inputsym
    global TARGETS

    col_var_names = []

    col_var_values = []

    num_features = get_num_features()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    data_size = get_data_size()

    values = tr.dt.tree_.value

    for n in range(num_nodes):

        feat = sget_feature(tr.dt, convert_node2(tr.dt, n, num_nodes))

        if feat < 0:

            feat = 0

        for f in range(num_features):  #f_{i,j}

            if f == feat:

                col_var_names.extend(
                    [VARS["node_feature_" + str(n) + "_" + str(f)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend(
                    [VARS["node_feature_" + str(n) + "_" + str(f)]])

                col_var_values.extend([0])

    for n in range(num_nodes):  #c_{j}
        """TODO
        
        if inputsym == 1 and len(get_left_leafs(n, num_nodes)) == 1:

            ll = get_left_leafs(n, num_nodes)[0]

            rl = get_right_leafs(n, num_nodes)[0]

            predl = values[convert_leaf2(tr.dt, ll, num_nodes)].tolist()[0]

            predr = values[convert_leaf2(tr.dt, rl, num_nodes)].tolist()[0]

            if predl.index(max(predl)) == predr.index(max(predr)):

                val = get_max_value_f(feat)
                
        else:
        """

        val = sget_node_constant(tr.dt, convert_node2(tr.dt, n, num_nodes))

        col_var_names.extend([VARS["node_constant_" + str(n)]])

        col_var_values.extend([val])

    odd = False

    prev_max_class = None

    for l in range(num_leafs):

        predictions = values[convert_leaf2(tr.dt, l, num_nodes)].tolist()[0]

        max_index = predictions.index(max(predictions))

        max_class = tr.dt.classes_[max_index]
        """TODO
        
        if inputsym == 1 and odd and max_class == prev_max_class: #trick to be sure that valid inequalties hold

            if TARGETS[0] == max_class:

                col_var_names.extend([VARS["prediction_type_" + str(0) + "_" + str(l)]])

                col_var_values.extend([0])

                col_var_names.extend([VARS["prediction_type_" + str(1) + "_" + str(l)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend([VARS["prediction_type_" + str(0) + "_" + str(l)]])

                col_var_values.extend([1])

                col_var_names.extend([VARS["prediction_type_" + str(1) + "_" + str(l)]])

                col_var_values.extend([0])

            for s in range(2,get_num_targets()):

                col_var_names.extend([VARS["prediction_type_" + str(s) + "_" + str(l)]])

                col_var_values.extend([0])
                
        else:
        """

        for s in range(get_num_targets()):

            if TARGETS[s] == max_class:

                col_var_names.extend(
                    [VARS["prediction_type_" + str(s) + "_" + str(l)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend(
                    [VARS["prediction_type_" + str(s) + "_" + str(l)]])

                col_var_values.extend([0])

        odd = not odd

        prev_max_class = max_class

    for r in range(data_size):  #e_{r}

        final_leaf = tr.dt.apply(
            np.array([tr.df.drop(tr.df.columns[-1], axis=1).values[r]],
                     dtype=np.float32))[0]

        predictions = values[final_leaf].tolist()[0]

        max_index = predictions.index(max(predictions))

        max_class = tr.dt.classes_[max_index]

        if get_target(r) == (max_class):

            col_var_names.extend([VARS["row_error_" + str(r)]])

            col_var_values.extend([0])

        else:

            col_var_names.extend([VARS["row_error_" + str(r)]])

            col_var_values.extend([1])

    M = tr.dt.decision_path(
        np.array(tr.df.drop(tr.df.columns[-1], axis=1).values,
                 dtype=np.float32))

    for r in range(data_size):  #pt_{r,j}

        for j in range(num_nodes):

            if M[r, convert_node2(tr.dt, j, num_nodes)] == 1:

                col_var_names.extend(
                    [VARS["path_node_" + str(j) + "_" + str(r)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend(
                    [VARS["path_node_" + str(j) + "_" + str(r)]])

                col_var_values.extend([0])

        for l in range(num_leafs):

            if M[r, convert_leaf2(tr.dt, l, num_nodes)] == 1:

                col_var_names.extend(
                    [VARS["path_leaf_" + str(l) + "_" + str(r)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend(
                    [VARS["path_leaf_" + str(l) + "_" + str(r)]])

                col_var_values.extend([0])

    return col_var_names, col_var_values
Ejemplo n.º 16
0
def add_variable_to_master_and_rebuild(depth, prob, prev_segments_set,
                                       segments_to_add, leaf):

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    value = prob.variables.get_num()

    var_types, var_lb, var_ub, var_obj, var_names = "", [], [], [], []

    my_columns = [[[], []]]

    s = segments_to_add

    var_types += "C"

    var_lb.append(0)

    var_ub.append(1)

    var_obj.append(0)

    var_names.append("segment_leaf_" + str(len(prev_segments_set)) + "_" +
                     str(leaf))

    value = value + 1

    row_value = 0

    for i in range(num_features):  #constraint (15)

        for j in range(num_nodes):

            for l in get_left_leafs(j, num_nodes):

                my_columns[0][0].append("constraint_15_" + str(i) + "_" +
                                        str(j) + "_" + str(l))

                my_columns[0][1].append(
                    max([get_feature_value(r, i) for r in s]))  #mu^{i,s} max

                row_value = row_value + 1

    for i in range(num_features):  #constraint (16)

        for j in range(num_nodes):

            for l in get_right_leafs(j, num_nodes):

                my_columns[0][0].append("constraint_16_" + str(i) + "_" +
                                        str(j) + "_" + str(l))

                my_columns[0][1].append(
                    max([get_feature_value(r, i) for r in s]))  #mu^{i,s} max

                row_value = row_value + 1

    for r in range(data_size):  #constraint (17)

        if r in s:

            my_columns[0][0].append("constraint_17_" + str(r))

            my_columns[0][1].append(1)

        row_value = row_value + 1

    for l in range(num_leafs):  #constraint (18)

        if l == leaf:

            my_columns[0][0].append("constraint_18_" + str(l))

            my_columns[0][1].append(1)

        row_value = row_value + 1

    for r in range(data_size):  #constraint (19)

        for l in range(num_leafs):

            if l == leaf:

                if r in s:

                    my_columns[0][0].append("constraint_19_" + str(r) + "_" +
                                            str(l))

                    my_columns[0][1].append(1)

            row_value = row_value + 1

    prob.variables.add(obj=var_obj,
                       lb=var_lb,
                       ub=var_ub,
                       types=var_types,
                       columns=my_columns,
                       names=var_names)

    #row_names, row_values, row_right_sides, row_senses = create_rows_CG(inputdepth,segments_set)

    #prob.linear_constraints.delete()

    #prob.linear_constraints.add(lin_expr = row_values, senses = row_senses, rhs = row_right_sides, names = row_names)

    prob.set_problem_type(0)

    return prob
Ejemplo n.º 17
0
def create_variables_CG(depth, segments_set):

    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    # node n had a boolean test on feature f, boolean. On the paper: f_{i,j}

    for j in range(num_nodes):

        for i in range(num_features):

            var_names.append("node_feature_" + str(j) + "_" + str(i))

            var_types = var_types + "C"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    # value used in the boolean test in node n, integer. On the paper: c_{j}

    for j in range(num_nodes):

        var_names.append("node_constant_" + str(j))

        var_types = var_types + "C"

        var_lb.append(get_min_value())

        var_ub.append(get_max_value())

        var_obj.append(0)

        var_value = var_value + 1

    # leaf l predicts type t, boolean. On the paper: p_{l,t}

    for l in range(num_leafs):

        for t in range(get_num_targets()):

            var_names.append("prediction_type_" + str(t) + "_" + str(l))

            var_types = var_types + "C"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    # row error, variables to minimize. On the paper: e_{r}

    for r in range(data_size):

        var_names.append("row_error_" + str(r))

        var_types = var_types + "C"

        var_lb.append(0)

        var_ub.append(1)

        var_obj.append(1)

        var_value = var_value + 1

    for l in range(num_leafs):  # x_{l,s}

        for s in range(len(segments_set[l])):

            var_names.append("segment_leaf_" + str(s) + "_" + str(l))

            var_types = var_types + "C"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    return var_names, var_types, var_lb, var_ub, var_obj
Ejemplo n.º 18
0
def create_rows_CG(depth, segments_set):

    global TARGETS
    global constraint_indicators

    constraint_indicators = []

    row_value = 0

    row_names = []

    row_values = []

    row_right_sides = []

    row_senses = ""

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    if not DEPTH_CONSTRAINTS:

        constraint_indicators.append(row_value)

        for i in range(num_features):  #constraint (2), indicator 0

            for j in range(num_nodes):

                for r in range(data_size):

                    col_names, col_values = [], []

                    for l in get_left_leafs(j, num_nodes):

                        col_names.extend([
                            "segment_leaf_" + str(s) + "_" + str(l)
                            for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])  #x_{l,s}

                        col_values.extend([
                            get_feature_value(r, i)
                            for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])

                    col_names.extend([
                        "node_feature_" + str(j) + "_" + str(i),
                        "node_constant_" + str(j)
                    ])

                    col_values.extend([1, -1])

                    row_names.append("constraint_2_" + str(i) + "_" + str(j) +
                                     "_" + str(r))

                    row_values.append([col_names, col_values])

                    row_right_sides.append(1)

                    row_senses = row_senses + "L"

                    row_value = row_value + 1

        constraint_indicators.append(row_value)

        for i in range(num_features):  #constraint (3), indicator 1

            for j in range(num_nodes):

                for r in range(data_size):

                    col_names, col_values = [], []

                    for l in get_right_leafs(j, num_nodes):

                        col_names.extend([
                            "segment_leaf_" + str(s) + "_" + str(l)
                            for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])  #x_{l,s}

                        col_values.extend([
                            1 for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])

                    col_names.extend([
                        "node_feature_" + str(j) + "_" + str(i),
                        "node_constant_" + str(j)
                    ])

                    col_values.extend([1, 1])

                    row_names.append("constraint_3_" + str(i) + "_" + str(j) +
                                     "_" + str(r))

                    row_values.append([col_names, col_values])

                    row_right_sides.append(get_feature_value(r, i) + 2 - eps)

                    row_senses = row_senses + "L"

                    row_value = row_value + 1

    else:

        for j in range(num_nodes):  #constraint (2d)

            for r in range(data_size):

                col_names, col_values = [], []

                d_j = get_depth(j, num_nodes) - 1

                col_names.extend(["d_" + str(r) + "_" + str(d_j)])

                col_values.extend([1])

                tmp = get_pathn(j, num_nodes)

                path_j = [i for i in tmp if (i == 'right' or i == 'left')]

                del path_j[0]

                path_j.reverse()

                count_right = 0

                for h in range(len(path_j)):

                    if path_j[h] == 'left':

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([-1])

                    else:

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([1])

                        count_right += 1

                col_names.extend([
                    "node_feature_" + str(j) + "_" + str(i)
                    for i in range(num_features)
                ])

                col_values.extend(
                    [get_feature_value(r, i) for i in range(num_features)])

                col_names.extend(["node_constant_" + str(j)])

                col_values.extend([-1])

                row_names.append("constraint_2d_" + str(j) + "_" + str(r))

                row_values.append([col_names, col_values])

                row_right_sides.append(1 + count_right - d_j)

                row_senses = row_senses + "L"

                row_value = row_value + 1

        for j in range(num_nodes):  #constraint (3d)

            for r in range(data_size):

                col_names, col_values = [], []

                d_j = get_depth(j, num_nodes) - 1

                col_names.extend(["d_" + str(r) + "_" + str(d_j)])

                col_values.extend([-1])

                tmp = get_pathn(j, num_nodes)

                path_j = [i for i in tmp if (i == 'right' or i == 'left')]

                del path_j[0]

                path_j.reverse()

                count_right = 0

                for h in range(len(path_j)):

                    if path_j[h] == 'left':

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([-1])

                    else:

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([1])

                        count_right += 1

                col_names.extend([
                    "node_feature_" + str(j) + "_" + str(i)
                    for i in range(num_features)
                ])

                col_values.extend(
                    [-get_feature_value(r, i) for i in range(num_features)])

                col_names.extend(["node_constant_" + str(j)])

                col_values.extend([1])

                row_names.append("constraint_3d_" + str(j) + "_" + str(r))

                row_values.append([col_names, col_values])

                row_right_sides.append(-eps + count_right - d_j)

                row_senses = row_senses + "L"

                row_value = row_value + 1

        for l in range(num_leafs):  #constraint (depth_leaf)

            for r in range(data_size):

                col_names, col_values = [], []

                tmp = get_path(l, num_nodes)

                path_l = [i for i in tmp if (i == 'right' or i == 'left')]

                path_l.reverse()

                count_right = 0

                for h in range(len(path_l)):

                    if path_l[h] == 'left':

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([-1])

                    else:

                        col_names.extend(["d_" + str(r) + "_" + str(h)])

                        col_values.extend([1])

                        count_right += 1

                col_names.extend([
                    "segment_leaf_" + str(s) + "_" + str(l)
                    for s in range(len(segments_set[l]))
                    if r in segments_set[l][s]
                ])

                col_values.extend([
                    depth for s in range(len(segments_set[l]))
                    if r in segments_set[l][s]
                ])

                row_names.append("constraint_depth_leaf_" + str(l) + "_" +
                                 str(r))

                row_values.append([col_names, col_values])

                row_right_sides.append(count_right)

                row_senses = row_senses + "L"

                row_value = row_value + 1

        for j in range(num_nodes):  #constraint (depth_node)

            d_j = get_depth(j, num_nodes) - 1

            tmp = get_pathn(j, num_nodes)

            path_j = [i for i in tmp if (i == 'right' or i == 'left')]

            del path_j[0]

            path_j.reverse()

            if len(path_j) > 0:

                for r in range(data_size):

                    col_names, col_values = [], []

                    count_right = 0

                    for h in range(len(path_j)):

                        if path_l[h] == 'left':

                            col_names.extend(["d_" + str(r) + "_" + str(h)])

                            col_values.extend([-1])

                        else:

                            col_names.extend(["d_" + str(r) + "_" + str(h)])

                            col_values.extend([1])

                            count_right += 1

                    for l in get_left_leafs(j, num_nodes) + get_right_leafs(
                            j, num_nodes):

                        col_names.extend([
                            "segment_leaf_" + str(s) + "_" + str(l)
                            for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])

                        col_values.extend([
                            d_j for s in range(len(segments_set[l]))
                            if r in segments_set[l][s]
                        ])

                    row_names.append("constraint_depth_node_" + str(j) + "_" +
                                     str(r))

                    row_values.append([col_names, col_values])

                    row_right_sides.append(count_right)

                    row_senses = row_senses + "L"

                    row_value = row_value + 1

    constraint_indicators.append(row_value)

    #compute r_t

    r_t = [0 for t in range(get_num_targets())]

    for r in range(data_size):

        for t in range(get_num_targets()):

            if TARGETS[t] != get_target(r):

                r_t[t] += 1

    for l in range(num_leafs):  #constraint (4), indicator 2

        for t in range(get_num_targets()):

            col_names, col_values = [], []

            for s in range(len(segments_set[l])):

                col_names.extend(["segment_leaf_" + str(s) + "_" + str(l)
                                  ])  #x_{l,s}

                s_t = 0

                for r in segments_set[l][s]:

                    if TARGETS[t] != get_target(r):

                        s_t += 1

                col_values.extend([s_t])

            col_names.extend(["prediction_type_" + str(t) + "_" + str(l)])

            col_values.extend([r_t[t]])

            col_names.extend(["leaf_error_" + str(l)])

            col_values.extend([-1])

            row_names.append("constraint_4_" + str(l) + "_" + str(t))

            row_values.append([col_names, col_values])

            row_right_sides.append(r_t[t])

            row_senses = row_senses + "L"

            row_value = row_value + 1

    constraint_indicators.append(row_value)

    for r in range(data_size):  #constraint (4bis), indicator 3

        for l in range(num_leafs):

            col_names, col_values = [], []

            for s in range(len(segments_set[l])):

                if r in segments_set[l][s]:

                    col_names.extend(["segment_leaf_" + str(s) + "_" + str(l)
                                      ])  #x_{l,s}

                    col_values.extend([1])

            for t in range(get_num_targets()):

                if TARGETS[t] == get_target(r):

                    col_names.extend(
                        ["prediction_type_" + str(t) + "_" + str(l)])

                    col_values.extend([-1])

            col_names.extend(["row_error_" + str(r)])

            col_values.extend([-1])

            row_names.append("constraint_4bis_" + str(r) + "_" + str(l))

            row_values.append([col_names, col_values])

            row_right_sides.append(0)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    constraint_indicators.append(row_value)

    for r in range(data_size):  #constraint (5), indicator 4

        col_names, col_values = [], []

        for l in range(num_leafs):

            for s in range(len(segments_set[l])):

                if r in segments_set[l][s]:

                    col_names.extend(["segment_leaf_" + str(s) + "_" + str(l)
                                      ])  #x_{l,s}

                    col_values.extend([1])

        row_names.append("constraint_5_" + str(r))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for l in range(num_leafs):  #constraint (6), indicator 5

        col_names = [
            "segment_leaf_" + str(s) + "_" + str(l)
            for s in range(len(segments_set[l]))
        ]  #x_{l,s}

        col_values = [1 for s in range(len(segments_set[l]))]  #x_{l,s}

        row_names.append("constraint_6_" + str(l))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for l in range(num_leafs):  #constraint (8), indicator 6

        col_names = [
            "prediction_type_" + str(s) + "_" + str(l)
            for s in range(get_num_targets())
        ]

        col_values = [1 for t in range(get_num_targets())]

        row_names.append("constraint_8_" + str(l))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for j in range(num_nodes):  #constraint (9), indicator 7

        col_names = [
            "node_feature_" + str(j) + "_" + str(i)
            for i in range(num_features)
        ]

        col_values = [1 for i in range(num_features)]

        row_names.append("constraint_9_" + str(j))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    return row_names, row_values, row_right_sides, row_senses
Ejemplo n.º 19
0
    print "Solution status = ", prob.solution.get_status(
    ), ":", prob.solution.status[prob.solution.get_status()]

    if "infeasible" in prob.solution.status[prob.solution.get_status()]:

        return []

    print "Solution value  = ", prob.solution.get_objective_value()

    print "Accuracy = ", (data_size -
                          prob.solution.get_objective_value()) / data_size

    num_features = get_num_features()

    data_size = get_data_size()  #useless ?

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    solution = []

    solution_values = prob.solution.get_values()

    print_tree(num_nodes, solution_values, num_features)

    return solution_values


def main(argv):
def init_rand_hash():
    random.seed(0)
    global rand_hash
    rand_hash = [random.random() for r in range(get_data_size())]
def create_rows_pricing_all_at_once(depth,exc_rows,incl_rows,existing_segments):
    
    row_value = 0

    row_names = []

    row_values = []

    row_right_sides = []

    row_senses = ""
    
    data_size = get_data_size()
    
    num_leafs = 2**depth
    
    # at least one row per leaf constraint
    
    for l in range(num_leafs):
        
        col_names = ["row_" + str(l) + "_" + str(r) for r in range(data_size)]
        
        col_values = [-1 for r in range(data_size)]
        
        row_names.append("constraint_atleastonerow_"+str(l))
    
        row_values.append([col_names,col_values])

        row_right_sides.append(-1)

        row_senses = row_senses + "L"

        row_value = row_value + 1
    
    #partition constraint
    
    for r in range(data_size): #new contraint for "all at once" pricing problem
        
        col_names = ["row_" + str(l) + "_" + str(r) for l in range(num_leafs)]
        
        col_values = [1 for l in range(num_leafs)]
        
        row_names.append("constraint_partition_"+str(r))
    
        row_values.append([col_names,col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1
    
    # constraint to prevent the pricing from giving existing segments as output
    
    for l in range(num_leafs):
    
        for s in range(len(existing_segments[l])):
            
            col_names, col_values = [], []
            
            for r in range(data_size):
                
                if r in existing_segments[l][s]:
            
                    col_names.extend(["row_"+str(l)+"_"+str(r)])
            
                    col_values.extend([1])
                    
                else:
                    
                    col_names.extend(["row_"+str(l)+"_"+str(r)])
            
                    col_values.extend([-1])               
                        
            row_names.append("constraint_segment_"+str(s)+"_"+str(l))
        
            row_values.append([col_names,col_values])
        
            row_right_sides.append(len(existing_segments[l][s]) - 1)
        
            row_senses = row_senses + "L"
        
            row_value = row_value + 1
            
    for l in range(num_leafs):
            
        #branching constraint (0)
        
        if len(exc_rows[l]) > 0:
            
            col_names = ["row_"+str(l)+"_"+str(r) for r in exc_rows[l]]
            
            col_values = [1 for r in exc_rows[l]]
            
            row_names.append("constraint_branching_0_leaf"+str(l))
        
            row_values.append([col_names,col_values])
        
            row_right_sides.append(0)
        
            row_senses = row_senses + "L"
        
            row_value = row_value + 1
        
        #branching constraint (1)
        
        if len(incl_rows[l]) > 0:
        
            col_names = ["row_"+str(l)+"_"+str(r) for r in incl_rows[l]]
            
            col_values = [-1 for r in incl_rows[l]]
            
            row_names.append("constraint_branching_1_leaf"+str(l))
        
            row_values.append([col_names,col_values])
        
            row_right_sides.append(-len(incl_rows[l]))
        
            row_senses = row_senses + "L"
        
            row_value = row_value + 1
        
    return row_names, row_values, row_right_sides, row_senses
def create_variables_pricing(depth, master_prob, leaf):

    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    #compute useful sums of dual values

    A_i_l, B_i_l = [], []

    for i in range(num_features):

        s = 0

        for j in range(num_nodes):

            left_leaves = get_left_leafs(j, num_nodes)

            if leaf in left_leaves:

                s = s + master_prob.solution.get_dual_values(
                    "constraint_15_" + str(i) + "_" + str(j) + "_" + str(leaf))

            #print(s)

            if s > 0:

                input("STOP B")

        B_i_l.append(-s)

    for i in range(num_features):

        s = 0

        for j in range(num_nodes):

            right_leaves = get_right_leafs(j, num_nodes)

            if leaf in right_leaves:

                s = s + master_prob.solution.get_dual_values(
                    "constraint_16_" + str(i) + "_" + str(j) + "_" + str(leaf))

            #print(s)

            if s > 0:

                input("STOP A")

        A_i_l.append(-s)

    #print("SUM DUALS :",A_i_l," ",B_i_l)
    # z_{r}, main decision variables

    for r in range(data_size):

        var_names.append("row_" + str(r))

        var_types = var_types + "B"

        var_lb.append(0)

        var_ub.append(1)

        #print(r,leaf,"C_{r,l} ",duals[constraint_indicators[2] + r] + duals[constraint_indicators[4] + r*num_leafs + leaf])

        var_obj.append(
            master_prob.solution.get_dual_values("constraint_17_" + str(r)) +
            master_prob.solution.get_dual_values("constraint_19_" + str(r) +
                                                 "_" + str(leaf)))

        var_value = var_value + 1

    # kappa_{i,r}, indicate the min feature of row r

    for i in range(num_features):

        for r in range(data_size):

            var_names.append("kappa_" + str(r) + "_" + str(i))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(A_i_l[i] * get_feature_value(r, i))

            var_value = var_value + 1

    # omega_{i,r}, indicate the max feature of row r

    for i in range(num_features):

        for r in range(data_size):

            var_names.append("omega_" + str(r) + "_" + str(i))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(-B_i_l[i] * get_feature_value(r, i))

            var_value = var_value + 1

    return var_names, var_types, var_lb, var_ub, var_obj
def create_rows_pricing(depth, exc_rows, incl_rows, existing_segments):

    row_value = 0

    row_names = []

    row_values = []

    row_right_sides = []

    row_senses = ""

    num_features = get_num_features()

    data_size = get_data_size()

    for r in range(data_size):  #constraint (32)

        for i in range(num_features):

            col_names = ["kappa_" + str(r) + "_" + str(i)]

            col_values = [1]

            for r2 in range(data_size):

                if get_feature_value(r2, i) < get_feature_value(r, i):

                    col_names.extend(["row_" + str(r2)])

                    col_values.extend([1. / data_size])

            row_names.append("contraint_32_" + str(r) + "_" + str(i))

            row_values.append([col_names, col_values])

            row_right_sides.append(1)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    for r in range(data_size):  #constraint (33)

        for i in range(num_features):

            col_names = ["omega_" + str(r) + "_" + str(i)]

            col_values = [1]

            for r2 in range(data_size):

                if get_feature_value(r2, i) > get_feature_value(r, i):

                    col_names.extend(["row_" + str(r2)])

                    col_values.extend([1. / data_size])

            row_names.append("contraint_33_" + str(r) + "_" + str(i))

            row_values.append([col_names, col_values])

            row_right_sides.append(1)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    for r in range(data_size):  #constraint (34)

        col_names = [
            "kappa_" + str(r) + "_" + str(i) for i in range(num_features)
        ]

        col_values = [1 for i in range(num_features)]

        col_names.extend(["row_" + str(r)])

        col_values.extend([-num_features])

        row_names.append("constraint_34_" + str(r))

        row_values.append([col_names, col_values])

        row_right_sides.append(0)

        row_senses = row_senses + "L"

        row_value = row_value + 1

    for r in range(data_size):  #constraint (35)

        col_names = [
            "omega_" + str(r) + "_" + str(i) for i in range(num_features)
        ]

        col_values = [1 for i in range(num_features)]

        col_names.extend(["row_" + str(r)])

        col_values.extend([-num_features])

        row_names.append("constraint_35_" + str(r))

        row_values.append([col_names, col_values])

        row_right_sides.append(0)

        row_senses = row_senses + "L"

        row_value = row_value + 1

    for i in range(num_features):  #constraint (36)

        col_names = [
            "kappa_" + str(r) + "_" + str(i) for r in range(data_size)
        ]

        col_values = [1 for r in range(data_size)]

        row_names.append("constraint_36_" + str(i))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    for i in range(num_features):  #constraint (37)

        col_names = [
            "omega_" + str(r) + "_" + str(i) for r in range(data_size)
        ]

        col_values = [1 for r in range(data_size)]

        row_names.append("constraint_37_" + str(i))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    # constraint to prevent the generated segment to be the entire set

    col_names = ["row_" + str(r) for r in range(data_size)]

    col_values = [1 for r in range(data_size)]

    row_names.append("constraint_entire_set")

    row_values.append([col_names, col_values])

    row_right_sides.append(data_size - 1)

    row_senses = row_senses + "L"

    row_value = row_value + 1

    # constraints to prevent the pricing from generating existing segments

    for s in existing_segments:

        col_names, col_values = [], []

        for r in range(data_size):

            col_names.extend(["row_" + str(r)])

            if r in s:

                col_values.extend([1])

            else:

                col_values.extend([-1])

        row_names.append("constraint_segment_" + str(s))

        row_values.append([col_names, col_values])

        row_right_sides.append(len(s) - 1)

        row_senses = row_senses + "L"

        row_value = row_value + 1

    #branching constraint (0)

    if len(exc_rows) > 0:

        col_names = ["row_" + str(r) for r in exc_rows]

        col_values = [1 for r in exc_rows]

        row_names.append("constraint_branching_0")

        row_values.append([col_names, col_values])

        row_right_sides.append(0)

        row_senses = row_senses + "L"

        row_value = row_value + 1

    #branching constraint (1)

    if len(incl_rows) > 0:

        col_names = ["row_" + str(r) for r in incl_rows]

        col_values = [1 for r in exc_rows]

        row_names.append("constraint_branching_1")

        row_values.append([col_names, col_values])

        row_right_sides.append(len(incl_rows))

        row_senses = row_senses + "R"

        row_value = row_value + 1

    return row_names, row_values, row_right_sides, row_senses
Ejemplo n.º 24
0
def add_variable_to_master_and_rebuild2(depth, prob, prev_segments_set,
                                        segments_to_add, leaf):

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    value = prob.variables.get_num()

    var_types, var_lb, var_ub, var_obj, var_names = "", [], [], [], []

    my_columns = [[[], []]]

    s = segments_to_add

    var_types += "C"

    var_lb.append(0)

    var_ub.append(1.5)

    var_obj.append(0)

    var_names.append("segment_leaf_" + str(len(prev_segments_set)) + "_" +
                     str(leaf))

    value = value + 1

    row_value = 0

    if not DEPTH_CONSTRAINTS:

        for i in range(num_features):  #constraint (2)

            for r in range(data_size):

                for j in range(num_nodes):

                    if leaf in get_left_leafs(j, num_nodes) and r in s:

                        my_columns[0][0].append("constraint_2_" + str(i) +
                                                "_" + str(j) + "_" + str(r))

                        my_columns[0][1].append(get_feature_value(r, i))

                        row_value = row_value + 1

        for i in range(num_features):  #constraint (3)

            for r in range(data_size):

                for j in range(num_nodes):

                    if leaf in get_right_leafs(j, num_nodes) and r in s:

                        my_columns[0][0].append("constraint_3_" + str(i) +
                                                "_" + str(j) + "_" + str(r))

                        my_columns[0][1].append(1)

                        row_value = row_value + 1

    else:

        for r in range(data_size):  #constraints (depth leaf) and (depth node)

            if r in s:

                my_columns[0][0].append("constraint_depth_leaf_" + str(leaf) +
                                        "_" + str(r))

                my_columns[0][1].append(depth)

                row_value = row_value + 1

                for j in range(num_nodes):

                    if get_depth(j, num_nodes) != 1:

                        if leaf in get_right_leafs(
                                j, num_nodes) + get_left_leafs(j, num_nodes):

                            my_columns[0][0].append("constraint_depth_node_" +
                                                    str(j) + "_" + str(r))

                            my_columns[0][1].append(
                                get_depth(j, num_nodes) - 1)

                            row_value = row_value + 1

    for t in range(get_num_targets()):  #constraint (4)

        for l in range(num_leafs):

            if l == leaf:

                my_columns[0][0].append("constraint_4_" + str(leaf) + "_" +
                                        str(t))

                my_columns[0][1].append(
                    sum([1 for r in s if get_target(r) != TARGETS[t]]))

                row_value = row_value + 1

    for r in range(data_size):  #constraint (4bis)

        if r in s:

            my_columns[0][0].append("constraint_4bis_" + str(r) + "_" +
                                    str(leaf))

            my_columns[0][1].append(1)

            row_value = row_value + 1

    for r in range(data_size):  #constraint (5)

        if r in s:

            my_columns[0][0].append("constraint_5_" + str(r))

            my_columns[0][1].append(1)

            row_value = row_value + 1

    for l in range(num_leafs):  #constraint (6)

        if l == leaf:

            my_columns[0][0].append("constraint_6_" + str(l))

            my_columns[0][1].append(1)

            row_value = row_value + 1

    prob.variables.add(obj=var_obj,
                       lb=var_lb,
                       ub=var_ub,
                       types=var_types,
                       columns=my_columns,
                       names=var_names)

    #row_names, row_values, row_right_sides, row_senses = create_rows_CG(inputdepth,segments_set)

    #prob.linear_constraints.delete()

    #prob.linear_constraints.add(lin_expr = row_values, senses = row_senses, rhs = row_right_sides, names = row_names)

    prob.set_problem_type(0)

    return prob
Ejemplo n.º 25
0
def create_rows_CG(depth, segments_set):

    global TARGETS
    global constraint_indicators

    constraint_indicators = []

    row_value = 0

    row_names = []

    row_values = []

    row_right_sides = []

    row_senses = ""

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    big_M = get_max_value() - get_min_value()

    constraint_indicators.append(row_value)

    for i in range(num_features):  #constraint (15), indicator 0

        for j in range(num_nodes):

            for l in get_left_leafs(j, num_nodes):

                col_names = [
                    "segment_leaf_" + str(s) + "_" + str(l)
                    for s in range(len(segments_set[l]))
                ]  #x_{l,s}

                col_values = [
                    max([get_feature_value(r, i) for r in s])
                    for s in segments_set[l]
                ]  #mu^{i,s} max

                col_names.extend([
                    "node_feature_" + str(j) + "_" + str(i),
                    "node_constant_" + str(j)
                ])

                col_values.extend([big_M, -1])

                row_names.append("constraint_15_" + str(i) + "_" + str(j) +
                                 "_" + str(l))

                row_values.append([col_names, col_values])

                row_right_sides.append(big_M)

                row_senses = row_senses + "L"

                row_value = row_value + 1

    constraint_indicators.append(row_value)

    for i in range(num_features):  #constraint (16), indicator 1

        for j in range(num_nodes):

            for l in get_right_leafs(j, num_nodes):

                col_names = [
                    "segment_leaf_" + str(s) + "_" + str(l)
                    for s in range(len(segments_set[l]))
                ]  #x_{l,s}

                col_values = [
                    -min([get_feature_value(r, i) for r in s])
                    for s in segments_set[l]
                ]  #mu^{i,s} min

                col_names.extend([
                    "node_feature_" + str(j) + "_" + str(i),
                    "node_constant_" + str(j)
                ])

                col_values.extend([big_M, 1])

                row_names.append("constraint_16_" + str(i) + "_" + str(j) +
                                 "_" + str(l))

                row_values.append([col_names, col_values])

                row_right_sides.append(big_M + 0.01)

                row_senses = row_senses + "L"

                row_value = row_value + 1

    constraint_indicators.append(row_value)

    for r in range(data_size):  #constraint (17), indicator 2

        col_names, col_values = [], []

        for l in range(num_leafs):

            for s in range(len(segments_set[l])):

                if r in segments_set[l][s]:

                    col_names.extend(["segment_leaf_" + str(s) + "_" + str(l)
                                      ])  #x_{l,s}

                    col_values.extend([1])

        row_names.append("constraint_17_" + str(r))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for l in range(num_leafs):  #constraint (18), indicator 3

        col_names = [
            "segment_leaf_" + str(s) + "_" + str(l)
            for s in range(len(segments_set[l]))
        ]  #x_{l,s}

        col_values = [1 for s in range(len(segments_set[l]))]  #x_{l,s}

        row_names.append("constraint_18_" + str(l))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for r in range(data_size):  #constraint (19), indicator 4

        for l in range(num_leafs):

            col_names, col_values = [], []

            for s in range(len(segments_set[l])):

                if r in segments_set[l][s]:

                    col_names.extend(["segment_leaf_" + str(s) + "_" + str(l)
                                      ])  #x_{l,s}

                    col_values.extend([1])

            for t in range(get_num_targets()):

                if TARGETS[t] != get_target(r):

                    col_names.extend(
                        ["prediction_type_" + str(t) + "_" + str(l)])

                    col_values.extend([1])

            col_names.extend(["row_error_" + str(r)])

            col_values.extend([-1])

            row_names.append("constraint_19_" + str(r) + "_" + str(l))

            row_values.append([col_names, col_values])

            row_right_sides.append(1)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    constraint_indicators.append(row_value)

    for l in range(num_leafs):  #constraint (20), indicator 5

        col_names = [
            "prediction_type_" + str(s) + "_" + str(l)
            for s in range(get_num_targets())
        ]

        col_values = [1 for s in range(get_num_targets())]

        row_names.append("constraint_20_" + str(l))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    constraint_indicators.append(row_value)

    for j in range(num_nodes):  #constraint (21), indicator 6

        col_names = [
            "node_feature_" + str(j) + "_" + str(i)
            for i in range(num_features)
        ]

        col_values = [1 for i in range(num_features)]

        row_names.append("constraint_21_" + str(j))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    return row_names, row_values, row_right_sides, row_senses
Ejemplo n.º 26
0
def get_start_solutions(depth):

    global inputsym

    #hello

    col_var_names = []

    col_var_values = []

    num_features = get_num_features()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    values = tr.dt.tree_.value

    for n in range(num_nodes):

        feat = sget_feature(tr.dt, convert_node(tr.dt, n, num_nodes))

        if feat < 0:

            feat = 0

        for f in range(num_features):

            if f == feat:

                col_var_names.extend(
                    [VARS["node_feature_" + str(n) + "_" + str(f)]])

                col_var_values.extend([1])

            else:

                col_var_names.extend(
                    [VARS["node_feature_" + str(n) + "_" + str(f)]])

                col_var_values.extend([0])

        if inputsym == 1 and len(get_left_leafs(n, num_nodes)) == 1:

            ll = get_left_leafs(n, num_nodes)[0]

            rl = get_right_leafs(n, num_nodes)[0]

            predl = values[convert_leaf(tr.dt, ll, num_nodes)].tolist()[0]

            predr = values[convert_leaf(tr.dt, rl, num_nodes)].tolist()[0]

            if predl.index(max(predl)) == predr.index(max(predr)):

                val = get_max_value_f(feat)

    for n in range(num_nodes):

        val = sget_node_constant(tr.dt, convert_node(tr.dt, n, num_nodes))

        col_var_names.extend([VARS["node_constant_" + str(n)]])

        col_var_values.extend([int(math.floor(val))])

    odd = False

    prev_max_class = -1

    for l in reversed(range(num_leafs)):

        predictions = values[convert_leaf(tr.dt, l, num_nodes)].tolist()[0]

        max_index = predictions.index(max(predictions))

        max_class = tr.dt.classes_[max_index]

        if inputsym == 1 and odd and max_class == prev_max_class:

            if TARGETS[0] == max_class:

                col_var_names.extend([
                    VARS["prediction_type_" + str(0) + "_" +
                         str(num_leafs - 1 - l)]
                ])

                col_var_values.extend([0])

                col_var_names.extend([
                    VARS["prediction_type_" + str(1) + "_" +
                         str(num_leafs - 1 - l)]
                ])

                col_var_values.extend([1])

            else:

                col_var_names.extend([
                    VARS["prediction_type_" + str(0) + "_" +
                         str(num_leafs - 1 - l)]
                ])

                col_var_values.extend([1])

                col_var_names.extend([
                    VARS["prediction_type_" + str(1) + "_" +
                         str(num_leafs - 1 - l)]
                ])

                col_var_values.extend([0])

            for s in range(2, get_num_targets()):

                col_var_names.extend([
                    VARS["prediction_type_" + str(s) + "_" +
                         str(num_leafs - 1 - l)]
                ])

                col_var_values.extend([0])

        else:

            for s in range(get_num_targets()):

                if TARGETS[s] == max_class:

                    col_var_names.extend([
                        VARS["prediction_type_" + str(s) + "_" +
                             str(num_leafs - 1 - l)]
                    ])

                    col_var_values.extend([1])

                else:

                    col_var_names.extend([
                        VARS["prediction_type_" + str(s) + "_" +
                             str(num_leafs - 1 - l)]
                    ])

                    col_var_values.extend([0])

        prev_max_class = max_class

        odd = not odd

    return col_var_names, col_var_values
Ejemplo n.º 27
0
def create_variables(depth, prob):

    global VARS

    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    # node n had a boolean test on feature f, boolean. On the paper: f_{i,j}

    for j in range(num_nodes):

        for i in range(num_features):

            VARS["node_feature_" + str(j) + "_" + str(i)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    # value used in the boolean test in node n, integer. On the paper: c_{j}

    for j in range(num_nodes):

        VARS["node_constant_" + str(j)] = var_value

        var_names.append("#" + str(var_value))

        if continuousconstant == 1:

            var_types = var_types + "C"

        else:

            var_types = var_types + "I"

        var_lb.append(get_min_value())

        var_ub.append(get_max_value())

        var_obj.append(0)

        var_value = var_value + 1

    # leaf l predicts type t, boolean. On the paper: p_{l,t}

    for l in range(num_leafs):

        for t in range(get_num_targets()):

            VARS["prediction_type_" + str(t) + "_" + str(l)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    # row error, variables to minimize. On the paper: e_{r}

    for r in range(data_size):

        VARS["row_error_" + str(r)] = var_value

        var_names.append("#" + str(var_value))

        var_types = var_types + "C"

        var_lb.append(0)

        var_ub.append(1)

        var_obj.append(1)

        var_value = var_value + 1

    # indicates that data row r passes node j when executed in decision tree. Careful, leafs are included. On the paper: pt_{r,j}

    for r in range(data_size):

        for j in range(num_nodes):

            VARS["path_node_" + str(j) + "_" + str(r)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

        for l in range(num_leafs):

            VARS["path_leaf_" + str(l) + "_" + str(r)] = var_value

            var_names.append("#" + str(var_value))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    return var_names, var_types, var_lb, var_ub, var_obj
Ejemplo n.º 28
0
def display_RMP_solution_dual(depth, prob, CGiter):

    print("--------------------- RMP(" + str(CGiter) +
          ") Solution ---------------------")

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    num_features = get_num_features()

    for i in range(num_features):  #constraint (15),

        for j in range(num_nodes):

            left_leaves = get_left_leafs(j, num_nodes)

            for l in left_leaves:

                print(
                    "Pi(" + str(i) + "_" + str(j) + "_" + str(l) + "-15-)*= " +
                    "%.2f" % round(
                        -prob.solution.get_dual_values("constraint_15_" + str(
                            i) + "_" + str(j) + "_" + str(l)), 2))

    for i in range(num_features):  #constraint (16),

        for j in range(num_nodes):

            right_leaves = get_right_leafs(j, num_nodes)

            for l in right_leaves:

                print(
                    "Pi(" + str(i) + "_" + str(j) + "_" + str(l) + "-16-)*= " +
                    "%.2f" % round(
                        -prob.solution.get_dual_values("constraint_16_" + str(
                            i) + "_" + str(j) + "_" + str(l)), 2))

    data_size = get_data_size()

    for r in range(data_size):  #constraint (17),

        print("Th_" + str(r) + "*= " + "%.2f" % round(
            -prob.solution.get_dual_values("constraint_17_" + str(r)), 2))

    for l in range(num_leafs):  #constraint (18),

        print("Be_" + str(l) + "*= " + "%.2f" % round(
            -prob.solution.get_dual_values("constraint_18_" + str(l)), 2))

    for r in range(data_size):  #constraint (19),

        for l in range(num_leafs):

            print("Gm_" + str(r) + "_" + str(l) + "*= " + "%.2f" % round(
                -prob.solution.get_dual_values("constraint_19_" + str(r) +
                                               "_" + str(l)), 2))

    print("--------------------- RMP(" + str(CGiter) +
          ") Solution ---------------------")
Ejemplo n.º 29
0
def create_rows(depth):

    global VARS
    global TARGETS

    row_value = 0

    row_names = []

    row_values = []

    row_right_sides = []

    row_senses = ""

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    big_M = get_max_value() - get_min_value()

    big_M = 10 * big_M + 10

    for r in range(data_size):  #constraint (2)

        for j in range(num_nodes):

            col_names = [
                VARS["node_feature_" + str(j) + "_" + str(i)]
                for i in range(num_features)
            ]  #f_{i,j}

            col_values = [
                get_feature_value(r, i) for i in range(num_features)
            ]  #mu^{i,r}

            left_node = get_left_node(j, num_nodes)

            if int(left_node) != -1:  #if not a leaf

                col_names.extend(
                    [
                        VARS["path_node_" + str(j) + "_" + str(r)],
                        VARS["path_node_" + str(left_node) + "_" + str(r)]
                    ]
                )  #VARS["depth_true_" + str(r) + "_" + str(get_depth(j,num_nodes)-1)]])

            else:

                col_names.extend([
                    VARS["path_node_" + str(j) + "_" + str(r)],
                    VARS["path_leaf_" + str(j) + "_" + str(r)]
                ])

            col_values.extend([big_M, big_M])

            col_names.extend([VARS["node_constant_" + str(j)]])

            col_values.extend([-1])

            row_names.append("#" + str(row_value))

            row_values.append([col_names, col_values])

            row_right_sides.append(2 * big_M)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    for r in range(data_size):  #constraint (3)

        for j in range(num_nodes):

            col_names = [
                VARS["node_feature_" + str(j) + "_" + str(i)]
                for i in range(num_features)
            ]  #f_{i,j}

            col_values = [
                -get_feature_value(r, i) for i in range(num_features)
            ]  #mu^{i,r}

            right_node = get_right_node(j, num_nodes)

            if int(right_node) != -1:  #if not a leaf

                col_names.extend(
                    [
                        VARS["path_node_" + str(j) + "_" + str(r)],
                        VARS["path_node_" + str(right_node) + "_" + str(r)]
                    ]
                )  #VARS["depth_true_" + str(r) + "_" + str(get_depth(j,num_nodes)-1)]])

            else:

                col_names.extend([
                    VARS["path_node_" + str(j) + "_" + str(r)],
                    VARS["path_leaf_" + str(j + 1) + "_" + str(r)]
                ])

            #col_names.extend([VARS["path_node_" + str(j) + "_" + str(r)], VARS["depth_true_" + str(r) + "_" + str(get_depth(j,num_nodes)-1)]])

            col_values.extend([big_M, big_M])

            col_names.extend([VARS["node_constant_" + str(j)]])

            col_values.extend([1])

            row_names.append("#" + str(row_value))

            row_values.append([col_names, col_values])

            row_right_sides.append(2 * big_M - eps)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    for l in range(num_leafs):  #constraint (4)

        col_names = [
            VARS["prediction_type_" + str(s) + "_" + str(l)]
            for s in range(get_num_targets())
        ]

        col_values = [1 for s in range(get_num_targets())]

        row_names.append("#" + str(row_value))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    for j in range(num_nodes):  #constraint (5)

        col_names = [
            VARS["node_feature_" + str(j) + "_" + str(i)]
            for i in range(num_features)
        ]

        col_values = [1 for i in range(num_features)]

        row_names.append("#" + str(row_value))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    for r in range(data_size):  # constraint (6)

        col_names = [
            VARS["path_leaf_" + str(l) + "_" + str(r)]
            for l in range(num_leafs)
        ]

        col_values = [1 for l in range(num_leafs)]

        col_names.extend([
            VARS["path_node_" + str(j) + "_" + str(r)]
            for j in range(num_nodes)
        ])

        col_values.extend([1 for j in range(num_nodes)])

        row_names.append("#" + str(row_value))

        row_values.append([col_names, col_values])

        row_right_sides.append(depth + 1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    for r in range(data_size):  # contraint (7)

        col_names = []

        col_values = []

        for l in range(num_leafs):

            col_names.extend([VARS["path_leaf_" + str(l) + "_" + str(r)]])

            col_values.extend([1])

        row_names.append("#" + str(row_value))

        row_values.append([col_names, col_values])

        row_right_sides.append(1)

        row_senses = row_senses + "E"

        row_value = row_value + 1

    for r in range(data_size):  # constraint (8) for internal nodes

        for j in range(num_nodes):

            if j != (num_nodes - 1) / 2:

                col_names = [VARS["path_node_" + str(j) + "_" + str(r)]]

                col_values = [1]

                col_names.extend([
                    VARS["path_node_" + str(get_parent(j, depth)) + "_" +
                         str(r)]
                ])

                col_values.extend([-1])

                row_names.append("#" + str(row_value))

                row_values.append([col_names, col_values])

                row_right_sides.append(0)

                row_senses = row_senses + "L"

                row_value = row_value + 1

    for r in range(data_size):  # constraint (8) for leaves

        for l in range(num_leafs):

            col_names = [VARS["path_leaf_" + str(l) + "_" + str(r)]]

            col_values = [1]

            col_names.extend(
                [VARS["path_node_" + str(l - l % 2) + "_" + str(r)]])

            col_values.extend([-1])

            row_names.append("#" + str(row_value))

            row_values.append([col_names, col_values])

            row_right_sides.append(0)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    for r in range(data_size):  #constraint (9)

        for l in range(num_leafs):

            col_names = [VARS["path_leaf_" + str(l) + "_" + str(r)]]

            col_values = [1]

            for s in range(get_num_targets()):

                if TARGETS[s] != get_target(r):

                    col_names.extend(
                        [VARS["prediction_type_" + str(s) + "_" + str(l)]])

                    col_values.extend([1])

            col_names.extend([VARS["row_error_" + str(r)]])

            col_values.extend([-1])

            row_names.append("#" + str(row_value))

            row_values.append([col_names, col_values])

            row_right_sides.append(1)

            row_senses = row_senses + "L"

            row_value = row_value + 1

    if inputsym == 1:  #valid inequalties ?

        for n in range(num_nodes):

            left_leaf = get_left_leafs(n, num_nodes)

            right_leaf = get_right_leafs(n, num_nodes)

            if len(left_leaf) != 1:

                continue

            for s in range(get_num_targets()):

                col_names = [
                    VARS["prediction_type_" + str(s) + "_" + str(left_leaf[0])]
                ]

                col_values = [1]

                col_names.extend([
                    VARS["prediction_type_" + str(s) + "_" +
                         str(right_leaf[0])]
                ])

                col_values.extend([1])

                row_names.append("#" + str(row_value))

                row_values.append([col_names, col_values])

                row_right_sides.append(1)

                row_senses = row_senses + "L"

                row_value = row_value + 1

    return row_names, row_values, row_right_sides, row_senses
Ejemplo n.º 30
0
def create_variables(depth, C_set):

    var_value = 0

    var_names = []

    var_types = ""

    var_lb = []

    var_ub = []

    var_obj = []

    num_features = get_num_features()

    data_size = get_data_size()

    num_leafs = 2**depth

    num_nodes = num_leafs - 1

    for l in range(num_leafs):  #x_{l,r}

        for r in range(data_size):

            var_names.append("x_" + str(l) + "_" + str(r))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    for l in range(num_leafs):  #p_{l,t}

        for t in range(get_num_targets()):

            var_names.append("prediction_type_" + str(l) + "_" + str(t))

            var_types = var_types + "B"

            var_lb.append(0)

            var_ub.append(1)

            var_obj.append(0)

            var_value = var_value + 1

    for i in range(num_features):  #rho_{i,j,v}

        for j in range(num_nodes):

            for v in range(len(C_set[i])):

                var_names.append("rho_" + str(i) + "_" + str(j) + "_" + str(v))

                var_types = var_types + "B"

                var_lb.append(0)

                var_ub.append(1)

                var_obj.append(0)

                var_value = var_value + 1

    for r in range(data_size):

        var_names.append("row_error_" + str(r))

        var_types = var_types + "C"

        var_lb.append(0)

        var_ub.append(1)

        var_obj.append(1)

        var_value = var_value + 1

    return var_names, var_types, var_lb, var_ub, var_obj