def ddi_dist_without_singletons(model, rs, cfg): logger.info("Defining genome X.") xes, indexing_set = define_guided_matching_using_graph( model=model, edge_conditions={edge: True for edge in cfg.allowable_ancestral_edges}, equiv_map=cfg.equiv_map, edge_variables=rs) logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables( model=model, vertex_set=cfg.ind_bg_A_vertices, suffix="as") add_certain_connectivity_constraints(model=model, edges=cfg.ind_bg_A_edges, connect_vars=bs) add_uncertain_connectivity_constraints(model=model, edge_set=indexing_set, edge_vars=xes, connect_vars=bs, biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_odd_path_variables( model=model, connect_vars=bs, telomeric_vertices=cfg.ind_bg_A_telomers, biggest_const=cfg.biggest_const) return tilde_bs, hat_bs
def di_dist_without_singletons(model, rs, cfg, ind): completion_for_m = cfg.ind_ancestral_set - cfg.ind_cbg_p_i_vertex_sets[ind] completion_for_p_ind = cfg.ind_cbg_p_i_vertex_sets[ind] - cfg.ind_ancestral_set logger.info("Creating completion variables.") hat_rs = define_matching_vars(model=model, edge_set=general_allowable_set(completion_for_p_ind), edge_conditions=general_conditional_set(completion_for_p_ind), vertex_set=[], vertex_conditions={x: False for x in completion_for_p_ind}) hat_ps = define_matching_vars(model=model, edge_set=general_allowable_set(completion_for_m), edge_conditions=general_conditional_set(completion_for_m), vertex_set=[], vertex_conditions={x: False for x in completion_for_m}) logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables(model=model, vertex_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind])) add_certain_connectivity_constraints(model=model, edges=cfg.ind_cbg_p_i_edges[ind], connect_vars=bs) add_uncertain_connectivity_constraints(model=model, edge_set=cfg.allowable_ancestral_edges, connect_vars=bs, edge_vars=rs, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=bs, edge_vars=hat_ps, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(completion_for_p_ind), connect_vars=bs, edge_vars=hat_rs, biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_vars_count_odd_paths(model=model, connect_vars=bs, telomeric_vertices=cfg.ind_cbg_p_i_telomers[ind], biggest_const=cfg.biggest_const) return tilde_bs, hat_bs
def create_ilp_formulation_for_ddp(config): try: model = gurobipy.Model(config.name_model) logger.info("Creating completion variables.") hat_as = define_matching_vars( model=model, edge_set=general_allowable_set(config.ind_compl_for_A), edge_conditions=general_conditional_set(config.ind_compl_for_A), vertex_set=[], vertex_conditions={x: False for x in config.ind_compl_for_A}, name="hat_as") hat_xs = define_matching_vars( model=model, edge_set=general_allowable_set(config.ind_compl_for_R), edge_conditions=general_conditional_set(config.ind_compl_for_R), vertex_set=[], vertex_conditions={x: False for x in config.ind_compl_for_R}, name="hat_bs") logger.info("Defining genome X.") xes, indexing_set = define_guided_matching_using_graph( model=model, edge_conditions={edge: False for edge in config.ind_cbg_R_edges}, equiv_map=config.equiv_map, edge_variables=[]) logger.info("Creating connectivity variables and constraints.") aes, tilde_as = create_connectivity_variables( model=model, vertex_set=config.bg_vertex2ind.values()) add_certain_connectivity_constraints(model=model, edges=config.ind_bg_A_edges, connect_vars=aes) add_uncertain_connectivity_constraints( model=model, edge_set=indexing_set, edge_vars=xes, connect_vars=aes, biggest_const=config.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(config.ind_compl_for_A), edge_vars=hat_as, connect_vars=aes, biggest_const=config.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(config.ind_compl_for_R), edge_vars=hat_xs, connect_vars=aes, biggest_const=config.biggest_const) logger.info("Creating telomeric variables and constraints.") dot_as = create_odd_path_variables( model=model, telomeric_vertices=config.ind_bg_A_telomers, connect_vars=aes, biggest_const=config.biggest_const) logger.info("CREATING OBJECTIVE FUNCTION.") model.setObjective( tilde_as.sum('*') - dot_as.sum('*'), gurobipy.GRB.MAXIMIZE) logger.info("FINISH CREATE MODEL.") model.params.logFile = config.log_file model.params.MIPFocus = 2 model.params.timeLimit = config.time_limit model.optimize() logger.info("The number of cycles and paths is " + str(int(model.objVal))) answer = get_param_of_solution_for_double_distance( model=model, multiplicity=config.multiplicity, number_of_genes=len(config.s_all_genes), number_of_R_telomers=len(config.ind_cbg_R_telomers)) return answer except gurobipy.GurobiError as e: logger.error( "Some error has been raised. Please, report to github bug tracker. \n Text exception: {0}" .format(e)) return None
def di_dist_with_singletons(model, rs, cfg, ind): completion_for_m = cfg.ind_ancestral_set - cfg.ind_cbg_p_i_vertex_sets[ind] completion_for_p_ind = cfg.ind_cbg_p_i_vertex_sets[ind] - cfg.ind_ancestral_set logger.info("Creating variables and constraints for singletons.") ss, tilde_ss = create_connectivity_variables(model=model, vertex_set=completion_for_m) filtered_obverse_edges = {(u, v) for u, v in cfg.ind_cbg_obverse_edges if u in completion_for_m and v in completion_for_m} add_certain_connectivity_constraints(model=model, edges=filtered_obverse_edges, connect_vars=ss) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=ss, edge_vars=rs, biggest_const=cfg.biggest_const) dot_ss = create_varibles_vertex_in_component(model=model, vertex_set=completion_for_m, connect_vars=ss, component_set=completion_for_m) hat_ss = create_vars_vertices_in_singleton(model=model, vertex_set=completion_for_m, dot_vars=dot_ss) add_constr_representative_neq_zero(model=model, dot_vars=dot_ss, component_set=completion_for_m, tilde_vars=tilde_ss, biggest_const=cfg.biggest_const) add_constr_vertex_in_singleton_has_edge(model=model, hat_vars=hat_ss, genome_vars=rs, vertex_set=completion_for_m) logger.info("Creating completion variables.") hat_rs = define_matching_vars(model=model, edge_set=general_allowable_set(completion_for_p_ind), edge_conditions=general_conditional_set(completion_for_p_ind), vertex_set=[], vertex_conditions={x: False for x in completion_for_p_ind}) hat_ps = define_matching_vars(model=model, edge_set=general_allowable_set(completion_for_m), edge_conditions=general_conditional_set(completion_for_m), vertex_set=hat_ss, vertex_conditions={x: True for x in completion_for_m}) logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables(model=model, vertex_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind])) add_certain_connectivity_constraints(model=model, edges=cfg.ind_cbg_p_i_edges[ind], connect_vars=bs) add_uncertain_connectivity_constraints(model=model, edge_set=cfg.allowable_ancestral_edges, connect_vars=bs, edge_vars=rs, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=bs, edge_vars=hat_ps, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(completion_for_p_ind), connect_vars=bs, edge_vars=hat_rs, biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_vars_count_odd_paths(model=model, connect_vars=bs, telomeric_vertices=cfg.ind_cbg_p_i_telomers[ind], biggest_const=cfg.biggest_const) logger.info("Creating variables and constraints for avoiding singletons.") dot_bs = create_varibles_vertex_in_component(model=model, vertex_set=completion_for_m, connect_vars=bs, component_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind])) add_constr_vertices_in_comp_or(model=model, vertex_set=completion_for_m, dot_vars=dot_bs, singleton_vars=hat_ss) return tilde_bs, hat_bs, tilde_ss, hat_ss
def ddi_dist_without_singletons(model, rs, cfg): # TODO HERE # completion_for_A = cfg.ind_bg_predup_set - cfg.ind_bg_vertex_set_A # completion_for_R = cfg.ind_bg_vertex_set_A - cfg.ind_bg_predup_set # J_R, J_A, bar_J_hat_A, bar_J_hat_A_compl, all_R_edges, A_edges_in_hat_A, # J_T_A_in_hat_A, equiv_map, biggest_const): logger.info("Defining completion variables.") # hat_as = define_matching_vars(model=model, # edge_set=create_general_allowable_set(completion_for_R), # edge_conditions=create_general_conditional_set(completion_for_R), # vertex_set=[], # vertex_conditions={x: False for x in completion_for_R}) # # hat_xs = define_matching_vars(model=model, # edge_set=create_general_allowable_set(completion_for_A), # edge_conditions=create_general_conditional_set(completion_for_A), # vertex_set=[], # vertex_conditions={x: False for x in completion_for_A}) # hat_as = create_vars_completion_for_genome(model=model, vertex_set=bar_J_hat_A_compl) # hat_rs = create_vars_completion_for_genome(model=model, vertex_set=(J_A - J_R)) logger.info("Defining genome X.") # rs here xes, indexing_set = define_guided_matching_using_graph(model=model, edge_conditions={edge: True for edge in cfg.allowable_ancestral_edges}, equiv_map=cfg.equiv_map, edge_variables=rs) # indexing_set = set() # xes = gurobipy.tupledict() # indexing_set.update(define_uncertain_guided_matching_between_graphs(model=model, xes=xes, res=rs, # edge_set=all_R_edges, # equiv_map=equiv_map)) # indexing_set.update(define_uncertain_guided_matching_between_graphs(model=model, xes=xes, res=hat_rs, # edge_set=itertools.combinations(J_A - J_R, 2), # equiv_map=equiv_map)) logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables(model=model, vertex_set=cfg.ind_bg_A_vertices) # (cfg.ind_ancestral_set | cfg.ind_bg_A_vertices)) # TODO HERE # aes, tilde_as = create_connectivity_variables(model=model, vertex_set=bar_J_hat_A) add_certain_connectivity_constraints(model=model, edges=cfg.ind_bg_A_edges, connect_vars=bs) # add_certain_connectivity_constraints(model=model, # edges=A_edges_in_hat_A, # connect_vars=aes) add_uncertain_connectivity_constraints(model=model, edge_set=indexing_set, edge_vars=xes, connect_vars=bs, biggest_const=cfg.biggest_const) # add_uncertain_connectivity_constraints(model=model, # edge_set=indexing_set, # connect_vars=aes, # edge_vars=xes, # biggest_const=biggest_const) # CORRECT ONE # add_uncertain_connectivity_constraints(model=model, # edge_set=create_general_allowable_set(completion_for_R), # edge_vars=hat_as, # connect_vars=bs, # biggest_const=cfg.biggest_const) # add_uncertain_connectivity_constraints(model=model, # connect_vars=aes, # edge_set=itertools.combinations(bar_J_hat_A_compl, 2), # edge_vars=hat_as, # biggest_const=biggest_const) # CORRECT ONE # add_uncertain_connectivity_constraints(model=model, # edge_set=create_general_allowable_set(completion_for_A), # edge_vars=hat_xs, # connect_vars=bs, # biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_vars_count_odd_paths(model=model, connect_vars=bs, telomeric_vertices=cfg.ind_bg_A_telomers, biggest_const=cfg.biggest_const) return tilde_bs, hat_bs
def di_dist_with_singletons(model, rs, cfg, ind): """ Function constructs part of ILP formulation, which express DCJ-indel distance between ordinary genomes M and P_ind. :param model: ILP model that will be submitted to solver :param rs: variables corresponding to median genome :param cfg: config with all information about problem instance :param ind: Integer the index of ordinary genome for which constraints will be constructed :return: return two types of variables required for objective function """ completion_for_m = cfg.ind_ancestral_set - cfg.ind_cbg_p_i_vertex_sets[ind] completion_for_p_ind = cfg.ind_cbg_p_i_vertex_sets[ ind] - cfg.ind_ancestral_set filtered_obverse_edges = { (u, v) for u, v in cfg.ind_cbg_obverse_edges if u in completion_for_m and v in completion_for_m } logger.info("Creating variables and constraints for singletons.") ss, tilde_ss = create_connectivity_variables(model=model, vertex_set=completion_for_m, suffix="ss") add_certain_connectivity_constraints(model=model, edges=filtered_obverse_edges, connect_vars=ss) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=ss, edge_vars=rs, biggest_const=cfg.biggest_const) dot_ss = create_representing_variables(model=model, vertex_set=completion_for_m, component_set=completion_for_m, component_vars=ss, name="dot_ss") hat_ss = model.addVars(completion_for_m, vtype=gurobipy.GRB.BINARY) add_ensuring_belonging_constraints(model=model, vertex_set=completion_for_m, representing_vars=dot_ss, belonging_vars=hat_ss, negotiation=False) add_ensuring_non_zero_constraints(model=model, dot_vars=dot_ss, component_set=completion_for_m, tilde_vars=tilde_ss, biggest_const=cfg.biggest_const) add_ensuring_edge_constraints(model=model, hat_vars=hat_ss, genome_vars=rs, vertex_set=completion_for_m) logger.info("Creating completion variables.") hat_rs = define_matching_vars( model=model, edge_set=general_allowable_set(completion_for_p_ind), edge_conditions=general_conditional_set(completion_for_p_ind), vertex_set=[], vertex_conditions={x: False for x in completion_for_p_ind}, name="hat_rs") hat_ps = define_matching_vars( model=model, edge_set=general_allowable_set(completion_for_m), edge_conditions=general_conditional_set(completion_for_m), vertex_set=hat_ss, vertex_conditions={x: True for x in completion_for_m}, name="hat_ps") logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables( model=model, vertex_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind]), suffix="ps") add_certain_connectivity_constraints(model=model, edges=cfg.ind_cbg_p_i_edges[ind], connect_vars=bs) add_uncertain_connectivity_constraints( model=model, edge_set=cfg.allowable_ancestral_edges, connect_vars=bs, edge_vars=rs, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=bs, edge_vars=hat_ps, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(completion_for_p_ind), connect_vars=bs, edge_vars=hat_rs, biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_odd_path_variables( model=model, connect_vars=bs, telomeric_vertices=cfg.ind_cbg_p_i_telomers[ind], biggest_const=cfg.biggest_const) logger.info("Creating variables and constraints for avoiding singletons.") dot_bs = create_representing_variables( model=model, vertex_set=completion_for_m, component_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind]), component_vars=bs, name="dot_ps") add_ensuring_belonging_constraints(model=model, vertex_set=completion_for_m, representing_vars=dot_bs, belonging_vars=hat_ss, negotiation=True) return tilde_bs, hat_bs, tilde_ss, hat_ss
def di_dist_without_singletons(model, rs, cfg, ind): """ Function constructs part of ILP formulation, which express DCJ distance between ordinary genomes M and P_ind. Function assumption is no singletons in M wrt P_i. :param model: ILP model that will be submitted to solver :param rs: variables corresponding to median genome :param cfg: config with all information about problem instance :param ind: Integer the index of ordinary genome for which constraints will be constructed :return: return two types of variables required for objective function """ completion_for_m = cfg.ind_ancestral_set - cfg.ind_cbg_p_i_vertex_sets[ind] completion_for_p_ind = cfg.ind_cbg_p_i_vertex_sets[ ind] - cfg.ind_ancestral_set logger.info("Creating completion variables.") hat_rs = define_matching_vars( model=model, edge_set=general_allowable_set(completion_for_p_ind), edge_conditions=general_conditional_set(completion_for_p_ind), vertex_set=[], vertex_conditions={x: False for x in completion_for_p_ind}, name='hat_rs') hat_ps = define_matching_vars( model=model, edge_set=general_allowable_set(completion_for_m), edge_conditions=general_conditional_set(completion_for_m), vertex_set=[], vertex_conditions={x: False for x in completion_for_m}, name='hat_ps') logger.info("Creating connectivity variables and constraints.") bs, tilde_bs = create_connectivity_variables( model=model, vertex_set=(cfg.ind_ancestral_set | cfg.ind_cbg_p_i_vertex_sets[ind]), suffix="ps") add_certain_connectivity_constraints(model=model, edges=cfg.ind_cbg_p_i_edges[ind], connect_vars=bs) add_uncertain_connectivity_constraints( model=model, edge_set=cfg.allowable_ancestral_edges, connect_vars=bs, edge_vars=rs, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(completion_for_m), connect_vars=bs, edge_vars=hat_ps, biggest_const=cfg.biggest_const) add_uncertain_connectivity_constraints( model=model, edge_set=general_allowable_set(completion_for_p_ind), connect_vars=bs, edge_vars=hat_rs, biggest_const=cfg.biggest_const) logger.info("Creating telomeric variables and constraints.") hat_bs = create_odd_path_variables( model=model, connect_vars=bs, telomeric_vertices=cfg.ind_cbg_p_i_telomers[ind], biggest_const=cfg.biggest_const) return tilde_bs, hat_bs
def create_ilp_formulation_for_ddp(config, ilp_type='improved'): try: start_time = datetime.datetime.now() model = gurobipy.Model(config.name_model) logger.info("Creating completion variables.") logger.info("Defining R completion.") hat_rs = define_matching_vars(model=model, edge_set=general_allowable_set(config.ind_cbg_compl_R), edge_conditions=general_conditional_set(config.ind_cbg_compl_R), vertex_set=[], vertex_conditions={x: False for x in config.ind_cbg_compl_R}) logger.info("Defining genome X.") xes, indexing_set = define_guided_matching_using_graph(model=model, edge_conditions={**{edge: False for edge in config.ind_cbg_R_edges}, **{pair: True for pair in general_allowable_set( config.ind_cbg_compl_R)}}, equiv_map=config.equiv_map, edge_variables=hat_rs) logger.info("Creating connectivity variables and constraints.") aes, tilde_as = create_connectivity_variables(model=model, vertex_set=config.bg_vertex2ind.values()) add_certain_connectivity_constraints(model=model, edges=config.ind_bg_A_edges, connect_vars=aes) if ilp_type == 'basic': logger.info("Defining A completion.") hat_as = define_matching_vars(model=model, edge_set=general_allowable_set(config.ind_compl_A), edge_conditions=general_conditional_set(config.ind_compl_A), vertex_set=[], vertex_conditions={x: False for x in config.ind_compl_A}) add_uncertain_connectivity_constraints(model=model, edge_set=indexing_set, edge_vars=xes, connect_vars=aes, biggest_const=config.biggest_const) add_uncertain_connectivity_constraints(model=model, edge_set=general_allowable_set(config.ind_compl_A), edge_vars=hat_as, connect_vars=aes, biggest_const=config.biggest_const) logger.info("Creating telomeric variables and constraints.") dot_as = create_vars_count_odd_paths(model=model, telomeric_vertices=config.ind_bg_A_telomers, connect_vars=aes, biggest_const=config.biggest_const) logger.info("CREATING OBJECTIVE FUNCTION.") model.setObjective(tilde_as.sum('*') - dot_as.sum('*'), gurobipy.GRB.MAXIMIZE) elif ilp_type == 'improved': set_pairs_for_paths = general_allowable_set(config.ind_forth_type_telomers) | \ product_set(config.ind_second_type_telomers, config.ind_third_type_telomers) | \ product_set(config.ind_second_type_telomers, config.ind_forth_type_telomers) logger.info("Creating variables for counting paths.") hat_cs = create_path_variables(model=model, pairs_set=set_pairs_for_paths) add_uncertain_connectivity_constraints_a(model=model, edge_set=indexing_set, edge_vars=xes, connect_vars=aes, biggest_const=config.biggest_const) add_uncertain_connectivity_constraints_c(model=model, edge_set=set_pairs_for_paths, edge_vars=hat_cs, connect_vars=aes, biggest_const=config.biggest_const) p44 = create_helping_path_variables(model=model, connect_vars=hat_cs, pair_set=general_allowable_set(config.ind_forth_type_telomers), biggest_const=config.biggest_const) p23 = create_helping_path_variables(model=model, connect_vars=hat_cs, pair_set=product_set(config.ind_second_type_telomers, config.ind_third_type_telomers), biggest_const=config.biggest_const) p24 = create_helping_path_variables(model=model, connect_vars=hat_cs, pair_set=product_set(config.ind_second_type_telomers, config.ind_forth_type_telomers), biggest_const=config.biggest_const) c1 = len(config.ind_first_type_telomers) # create_objective logger.info("CREATING OBJECTIVE FUNCTION.") tmp = model.addVar(lb=-config.biggest_const, ub=config.biggest_const, vtype=gurobipy.GRB.INTEGER, name="tmp") model.addConstr(tmp == p24 - c1 - p23) abs_var = model.addVar(ub=config.biggest_const, vtype=gurobipy.GRB.INTEGER, name="abs_tmp") model.addConstr(abs_var == gurobipy.abs_(tmp)) model.setObjective(tilde_as.sum('*') - p44 - 1 / 4 * (c1 + p23 + 3 * p24 + abs_var), gurobipy.GRB.MAXIMIZE) else: logger.error('Error: Unknown type. Use "basic" or "improved"') return None logger.info("FINISH CREATE MODEL.") model.params.logFile = config.log_file model.params.MIPFocus = 2 model.params.timeLimit = config.time_limit model.optimize() logger.info("The number of cycles and paths is " + str(int(model.objVal))) print("The number of cycles and paths is " + str(int(model.objVal))) answer = get_param_of_solution_for_double_distance(model=model, multiplicity=config.multiplicity, number_of_genes=len(config.s_all_genes), number_of_R_telomers=len(config.ind_cbg_R_telomers), working_time=datetime.datetime.now() - start_time) return answer except gurobipy.GurobiError as e: logger.error( "Some error has been raised. Please, report to github bug tracker. \n Text exception: {0}".format(e)) return None