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 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
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_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 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(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 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 ---------------------")
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_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
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
def create_variables_pricing_all_at_once(depth, master_prob): 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 l in range(num_leafs)], [[] for l in range(num_leafs)] for leaf in range(num_leafs): 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: print(s) input("STOP B") B_i_l[leaf].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: print(s) input("STOP A") A_i_l[leaf].append(-s) print("SUM DUALS :", A_i_l, " ", B_i_l) # z_{r,l}, main decision variables for leaf in range(num_leafs): #print("SUM0",sum([duals[constraint_indicators[2] + r2] + duals[constraint_indicators[4] + r2*num_leafs] for r2 in [0, 1, 4, 8, 11, 15, 16, 17, 18, 19, 21, 23, 24, 27, 28, 29]])+duals[constraint_indicators[3]]) #print("SUM1",sum([duals[constraint_indicators[2] + r3] + duals[constraint_indicators[4] + r3*num_leafs + 1] for r3 in [2, 3, 5, 6, 7, 9, 10, 12, 13, 14, 20, 22, 25, 26]])+duals[constraint_indicators[3]+1]) for r in range(data_size): var_names.append("row_" + str(leaf) + "_" + 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,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): var_names.append("kappa_" + str(leaf) + "_" + str(r) + "_" + str(i)) var_types = var_types + "B" var_lb.append(0) var_ub.append(1) var_obj.append(A_i_l[leaf][i] * get_feature_value(r, i)) var_value = var_value + 1 # 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): var_names.append("omega_" + str(leaf) + "_" + str(r) + "_" + str(i)) var_types = var_types + "B" var_lb.append(0) var_ub.append(1) var_obj.append(-B_i_l[leaf][i] * get_feature_value(r, i)) var_value = var_value + 1 return var_names, var_types, var_lb, var_ub, var_obj