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)
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 ---------------------")
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 compute_C(depth,r,l,master_prob): num_leafs = 2**depth num_nodes = num_leafs-1 C = master_prob.solution.get_dual_values("constraint_5_" + str(r)) #theta if not DEPTH_CONSTRAINTS: for i in range(get_num_features()): for j in range(num_nodes): if l in get_left_leafs(j,num_nodes): C = C + master_prob.solution.get_dual_values("constraint_2_" + str(i) + "_" + str(j) + "_" +str(r)) elif l in get_right_leafs(j,num_nodes): C = C + master_prob.solution.get_dual_values("constraint_3_" + str(i) + "_" + str(j) + "_" +str(r)) else: C = C + master_prob.solution.get_dual_values("constraint_depth_leaf_"+str(l)+"_"+str(r)) for j in range(num_nodes): if get_depth(j,num_nodes) != 1: C = C + master_prob.solution.get_dual_values("constraint_depth_node_"+str(j)+"_"+str(r)) for t in range(get_num_targets()): if TARGETS[t] != get_target(r): C = C + master_prob.solution.get_dual_values("constraint_4_" + str(l) + "_" +str(t)) # gamma leaf C = C + master_prob.solution.get_dual_values("constraint_4bis_" + str(r) + "_" +str(l)) # gamma row return C
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
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
def print_nodes(node, diff, depth, solution_values, num_nodes, num_features): if diff > 0: for f in range(num_features): if solution_values[VARS["node_feature_" + str(node) + "_" + str(f)]] > 0.5: print " " * depth, node, get_feature( f), "<=", solution_values[VARS["node_constant_" + str(node)]] print_nodes(node - diff, diff / 2, depth + 1, solution_values, num_nodes, num_features) for f in range(num_features): if solution_values[VARS["node_feature_" + str(node) + "_" + str(f)]] > 0.5: print " " * depth, node, get_feature(f), ">", solution_values[ VARS["node_constant_" + str(node)]] print_nodes(node + diff, diff / 2, depth + 1, solution_values, num_nodes, num_features) else: for f in range(num_features): if solution_values[VARS["node_feature_" + str(node) + "_" + str(f)]] > 0.5: print " " * depth, node, get_feature( f), "<=", solution_values[VARS["node_constant_" + str(node)]] for l in get_left_leafs(node, num_nodes): for s in range(get_num_targets()): if solution_values[VARS["prediction_type_" + str(s) + "_" + str(l)]] > 0.5: print " " * (depth + 1), l, TARGETS[s] for f in range(num_features): if solution_values[VARS["node_feature_" + str(node) + "_" + str(f)]] > 0.5: print " " * depth, node, get_feature(f), ">", solution_values[ VARS["node_constant_" + str(node)]] for l in get_right_leafs(node, num_nodes): for s in range(get_num_targets()): if solution_values[VARS["prediction_type_" + str(s) + "_" + str(l)]] > 0.5: print " " * (depth + 1), l, TARGETS[s]
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
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
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
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
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
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
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
def create_rows(depth, C_set): 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 for r in range(data_size): #constraint (1) for j in range(num_nodes): col_names = [ "x_" + str(l) + "_" + str(r) for l in get_left_leafs(j, num_nodes) ] col_values = [-1 for l in get_left_leafs(j, num_nodes)] for i in range(num_features): for v in range(len(C_set[i])): if get_feature_value(r, i) > C_set[i][v]: col_names.append("rho_" + str(i) + "_" + str(j) + "_" + str(v)) col_values.append(-1) for j2 in get_parents(j, depth): for i in range(num_features): for v in range(len(C_set[i])): if get_feature_value( r, i) <= C_set[i][v] and j in get_left_nodes( j2, num_nodes): col_names.append("rho_" + str(i) + "_" + str(j2) + "_" + str(v)) col_values.append(1) if get_feature_value( r, i) > C_set[i][v] and j in get_right_nodes( j2, num_nodes): col_names.append("rho_" + str(i) + "_" + str(j2) + "_" + str(v)) col_values.append(1) row_names.append("constraint_1_" + str(r) + "_" + str(j)) row_values.append([col_names, col_values]) row_right_sides.append(get_depth(j, num_nodes) - 2) row_senses = row_senses + "L" row_value = row_value + 1 for r in range(data_size): #constraint (1bis) for j in range(num_nodes): col_names = [ "x_" + str(l) + "_" + str(r) for l in get_right_leafs(j, num_nodes) ] col_values = [-1 for l in get_right_leafs(j, num_nodes)] for i in range(num_features): for v in range(len(C_set[i])): if get_feature_value(r, i) <= C_set[i][v]: col_names.append("rho_" + str(i) + "_" + str(j) + "_" + str(v)) col_values.append(-1) for j2 in get_parents(j, depth): for i in range(num_features): for v in range(len(C_set[i])): if get_feature_value( r, i) <= C_set[i][v] and j in get_left_nodes( j2, num_nodes): col_names.append("rho_" + str(i) + "_" + str(j2) + "_" + str(v)) col_values.append(1) if get_feature_value( r, i) > C_set[i][v] and j in get_right_nodes( j2, num_nodes): col_names.append("rho_" + str(i) + "_" + str(j2) + "_" + str(v)) col_values.append(1) row_names.append("constraint_1bis_" + str(r) + "_" + str(j)) row_values.append([col_names, col_values]) row_right_sides.append(get_depth(j, num_nodes) - 2) row_senses = row_senses + "L" row_value = row_value + 1 for j in range(num_nodes): #constraint (2) col_names, col_values = [], [] for i in range(num_features): for v in range(len(C_set[i])): col_names.append("rho_" + str(i) + "_" + str(j) + "_" + str(v)) col_values.append(1) row_names.append("constraint_2_" + str(j)) 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 (3) for l in range(num_leafs): col_names, col_values = [], [] col_names.append("x_" + str(l) + "_" + str(r)) #x_{l,s} col_values.append(1) for t in range(get_num_targets()): if TARGETS[t] == get_target(r): col_names.append("prediction_type_" + str(l) + "_" + str(t)) col_values.append(-1) col_names.append("row_error_" + str(r)) col_values.append(-1) row_names.append("constraint_3_" + 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 for l in range(num_leafs): #constraint (4) col_names = [ "prediction_type_" + str(l) + "_" + str(t) for t in range(get_num_targets()) ] col_values = [1 for t in range(get_num_targets())] row_names.append("constraint_4_" + str(l)) 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 (4) col_names = ["x_" + str(l) + "_" + str(r) for l in range(num_leafs)] col_values = [1 for l in range(num_leafs)] row_names.append("constraint_4_" + str(l)) 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