def find_imp_neig(imp_neigs, g1, perc, model, scaler, inputs, explore_prob,
                  rand_flag):  # rename to epsilon-greedy
    score_fin = None
    comp_bool_fin = None
    if len(imp_neigs) == 1:
        imp_neig = list(imp_neigs.keys())[0]
    else:
        cur_trial = rand_uniform(low=0.0, high=1.0)
        if cur_trial <= explore_prob:
            logging_debug("Exploring with low probability"
                          )  # Move to top for efficiency and remove del max
            imp_neig = random_choice(list(imp_neigs.keys()))
            rand_flag = 1
        else:
            if inputs["use_all_neigs"] == 0:
                imp_neigs = pick_top_weight_neigs(imp_neigs, perc, inputs)
                # Return none when imp_neigs is None

            for neig in imp_neigs:
                # Add to graph
                g1 = add_newnode(g1, neig, imp_neigs[neig]['graph_neigs'])
                # Check score
                (score_curr, comp_bool) = get_score(g1, model, scaler,
                                                    inputs['model_type'])
                imp_neigs[neig]['compScore'] = score_curr
                imp_neigs[neig]['compBool'] = comp_bool
                g1.remove_node(neig)
            imp_neig_tup = max(imp_neigs.items(),
                               key=lambda elem: elem[1]['compScore'])
            imp_neig = imp_neig_tup[0]
            score_fin = imp_neig_tup[1]['compScore']
            comp_bool_fin = imp_neig_tup[1]['compBool']
    return imp_neig, score_fin, comp_bool_fin, rand_flag
Exemple #2
0
def met(g1, model, scaler, inputs, score_prev):
    max_nodes = inputs["max_size"] - len(g1)

    num_iter = 1
    last_iter_imp = 0
    thres_neig = inputs["thres_neig"]
    prob_metropolis = inputs["prob_metropolis"]
    folNm = inputs['folNm']
    met_low_prob_acc = 0
    while num_iter < max_nodes:  # Limiting number of iteration rounds

        logging_debug("Adding next node")
        # neig_list_old = neig_list
        # g1, cc, node_to_add, score_curr, comp_bool, rand_flag, neig_list = add_top_neig(g1, thres_neig, folNm, inputs, model, scaler, neig_list)
        g1, cc, node_to_add, score_curr, comp_bool, rand_flag = add_top_neig(
            g1, thres_neig, folNm, inputs, model, scaler)
        if (score_curr is None) or (comp_bool is None):
            score_curr, comp_bool = get_score(g1, model, scaler,
                                              inputs['model_type'])

        if cc == 0:
            break
        if score_curr < inputs["classi_thresh"]:
            logging_debug("Complex found")

            # Remove the node last added
            g1.remove_node(node_to_add)
            break

        cur_trial = rand_uniform(low=0.0, high=1.0)
        if score_curr < score_prev:
            if cur_trial > prob_metropolis:
                # Remove the node last added
                g1.remove_node(node_to_add)
                # neig_list = neig_list_old
            else:
                logging_debug("Accepting with low probability")
                met_low_prob_acc += 1
                rand_flag = 1
        elif score_curr > score_prev:
            last_iter_imp = num_iter

        if (num_iter - last_iter_imp
            ) > 10:  # Has been a long time since a score improvement
            logging_debug("Long time since score improvement")
            break

        score_prev = score_curr

        num_iter += 1
    logging_debug("No. of low probability acceptances = ")
    logging_debug(str(met_low_prob_acc))

    # print(g1.nodes())
    # print(g1.edges())
    return frozenset(g1.nodes()), score_prev
def find_imp_neig(neig_list,explore_prob):
    if len(neig_list) == 1:
        imp_neig = list(neig_list.keys())[0]
    else:    
        cur_trial = rand_uniform(low=0.0,high=1.0)
        if  cur_trial <= explore_prob: 
            logging_debug("Exploring with low probability") # Move to top for efficiency and remove del max
            imp_neig = random_choice(list(neig_list.keys()))
        else:    
            imp_neig = max(iter(neig_list.items()), key=lambda elem: elem[1]['compScore'])[0]
    return imp_neig
Exemple #4
0
def search_isa(
    seed_node, scaler, par_inputs_fn
):  # Picks out of a subset of its neighbors and adds the best node
    with open(par_inputs_fn, 'rb') as f:
        inputs = pickle_load(f)
    with open(inputs['modelfname'], 'rb') as f:
        model = pickle_load(f)

    folNm = inputs['folNm']
    folNm_out = inputs['folNm_out']

    score_prev = 0
    cd, g1 = starting_edge(folNm, seed_node)
    if cd == 0:
        return
    max_nodes = inputs["max_size"] - len(g1)

    num_iter = 1
    last_iter_imp = 0
    thres_neig = inputs["thres_neig"]
    T = inputs["T0"]  # T0 value
    alpha = inputs["alpha"]

    while num_iter < max_nodes:  # Limiting number of iteration rounds

        logging_debug("Adding next node")
        # neig_list_old = neig_list
        # g1, cc, node_to_add, score_curr, comp_bool, rand_flag, neig_list = add_top_neig(g1, thres_neig, folNm, inputs, model, scaler, neig_list)
        g1, cc, node_to_add, score_curr, comp_bool, rand_flag = add_top_neig(
            g1, thres_neig, folNm, inputs, model, scaler)
        if (score_curr is None) or (comp_bool is None):
            score_curr, comp_bool = get_score(g1, model, scaler,
                                              inputs['model_type'])

        if cc == 0:
            break
        if score_curr < inputs["classi_thresh"]:
            logging_debug("Complex found")

            # Remove the node last added
            g1.remove_node(node_to_add)
            break

        cur_trial = rand_uniform(low=0.0, high=1.0)
        if score_curr < score_prev:

            prob_isa = np_exp((score_curr - score_prev) / T)
            if cur_trial > prob_isa:
                # Remove the node last added
                g1.remove_node(node_to_add)
                # neig_list = neig_list_old
            else:
                logging_debug("Accepting with low probability")
                rand_flag = 1
        elif score_curr > score_prev:
            last_iter_imp = num_iter

        if (num_iter - last_iter_imp
            ) > 10:  # Has been a long time since a score improvement
            logging_debug("Long time since score improvement")
            break

        score_prev = score_curr
        num_iter += 1
        T = float(T) / alpha

    # If number of nodes is less than 2, don't write.
    with open(folNm_out + "/" + seed_node, 'wb') as f:
        pickle_dump((frozenset(g1.nodes()), score_prev), f)
def met(g1,model,scaler,inputs,score_prev):
    # Assigning original graph to temporary variable
    for edge in g1.edges():
        (node1,node2) = edge
    
    max_nodes = inputs["max_size"] - len(g1)
    
    num_iter = 1
    last_iter_imp = 0        
    thres_neig = inputs["thres_neig"]
    prob_metropolis = inputs["prob_metropolis"]
    rem_nodes = []
    folNm = inputs['folNm']
    
    while num_iter < max_nodes: # Limiting number of iteration rounds 

        logging_debug("Adding next node")
            
        imp_neigs = dict()
        g1_nodes = g1.nodes()
        for node in g1_nodes:
            # get its max neighbor and weight and store in dict 
            with open(folNm+"/"+node,'rb') as f:
                neig_list = pickle_load(f)    
            
            # Remove neighbors already in graph - one small computation to save memory
            neig_fin = set(neig_list) -  set(list(g1_nodes)+rem_nodes)
            neig_list = dict([neig for neig in neig_list.items() if neig[0] in neig_fin])
            
            # Don't check all neighbors - just a subset if number of neighbors is large
            if len(neig_list) > thres_neig: # Make 500 
                neig_list = dict(random_sample(neig_list.items(),thres_neig))
            if not neig_list: # Checking if empty
                break
            imp_neig,max_score = find_max_neig(neig_list,g1,inputs['perc'],model,scaler,inputs)

            wt = neig_list[imp_neig]
            wt_edge = wt['weight']
            
            imp_neigs[imp_neig] = {'weight': wt_edge, 'compScore' : max_score}
                
        if not imp_neigs:
            logging_debug("No more neighbors to add")
            break
        
        node_to_add = find_imp_neig(imp_neigs,inputs['explore_prob'])
        #ADD ALL EDGES OF NEW NODE TO ORIG GRAPH
        with open(folNm+"/"+node_to_add,'rb') as f:
            its_neig_list = pickle_load(f)                  
        orig_nodes = g1.nodes()
        all_nodesWedges = set(orig_nodes).intersection(its_neig_list)
                
        for node in all_nodesWedges:
            wt = its_neig_list[node]
            wt_edge = wt['weight']
            g1.add_edge(node_to_add,node,weight=wt_edge)
            
        (score_curr,comp_bool) = get_score(g1,model,scaler,inputs['model_type'])    

        if comp_bool == 0:
            logging_debug("Complex found")
        
            # Remove the node last added                
            g1.remove_node(node_to_add)
            break

        cur_trial = rand_uniform(low=0.0,high=1.0)
        if score_curr < score_prev:
            if  cur_trial > prob_metropolis:  
            # Remove the node last added                
                g1.remove_node(node_to_add)
                rem_nodes.append(node_to_add)
            # since edges from this node to complex have been removed from tempG it will not be revisited 
            else:
                logging_debug("Accepting with low probability")
        elif score_curr > score_prev:
            last_iter_imp = num_iter

        if (num_iter - last_iter_imp)> 10: # Has been a long time since a score improvement
            logging_debug("Long time since score imporovement") 
            break
        
        score_prev = score_curr

        num_iter += 1
    
    #print(g1.nodes())
    #print(g1.edges())
    return (g1.nodes(),score_prev)