def MCTS(root, verbose=False):
    """initialization of the chemical trees and grammar trees"""
    run_time = time.time() + 600 * 2
    rootnode = Node(state=root)
    state = root.Clone()
    maxnum = 0
    iteration_num = 0
    start_time = time.time()
    """----------------------------------------------------------------------"""
    """global variables used for save valid compounds and simulated compounds"""
    valid_compound = []
    all_simulated_compound = []
    desired_compound = []
    max_score = -100.0
    desired_activity = []
    time_distribution = []
    num_searched = []
    current_score = []
    depth = []
    all_score = []
    """----------------------------------------------------------------------"""

    while maxnum < 10100:
        print maxnum
        node = rootnode
        state = root.Clone()
        """selection step"""
        node_pool = []
        print "current found max_score:", max_score

        while node.childNodes != []:
            node = node.Selectnode()
            state.SelectPosition(node.position)
        print "state position:,", state.position
        depth.append(len(state.position))
        if len(state.position) >= 81:
            re = -1.0
            while node != None:
                node.Update(re)
                node = node.parentNode
        else:
            """------------------------------------------------------------------"""
            """expansion step"""
            """calculate how many nodes will be added under current leaf"""
            expanded = expanded_node(model, state.position, val)
            nodeadded = node_to_add(expanded, val)

            all_posible = chem_kn_simulation(model, state.position, val,
                                             nodeadded)
            generate_smile = predict_smile(all_posible, val)
            new_compound = make_input_smile(generate_smile)

            node_index, score, valid_smile, all_smile = check_node_type(
                new_compound, SA_mean, SA_std, logP_mean, logP_std, cycle_mean,
                cycle_std)

            print node_index
            valid_compound.extend(valid_smile)
            all_simulated_compound.extend(all_smile)
            all_score.extend(score)
            iteration_num = len(all_simulated_compound)
            if len(node_index) == 0:
                re = -1.0
                while node != None:
                    node.Update(re)
                    node = node.parentNode
            else:
                re = []
                for i in range(len(node_index)):
                    m = node_index[i]
                    maxnum = maxnum + 1
                    node.Addnode(nodeadded[m], state)
                    node_pool.append(node.childNodes[i])
                    if score[i] >= max_score:
                        max_score = score[i]
                        current_score.append(max_score)
                    else:
                        current_score.append(max_score)
                    depth.append(len(state.position))
                    """simulation"""
                    re.append((0.8 * score[i]) / (1.0 + abs(0.8 * score[i])))
                    if maxnum == 100:
                        maxscore100 = max_score
                        time100 = time.time() - start_time
                    if maxnum == 500:
                        maxscore500 = max_score
                        time500 = time.time() - start_time
                    if maxnum == 1000:

                        maxscore1000 = max_score
                        time1000 = time.time() - start_time
                    if maxnum == 5000:
                        maxscore5000 = max_score
                        time5000 = time.time() - start_time
                    if maxnum == 10000:
                        time10000 = time.time() - start_time
                        maxscore10000 = max_score
                        #valid10000=10000*1.0/len(all_simulated_compound)
                    """backpropation step"""
                #print "node pool length:",len(node.childNodes)

                for i in range(len(node_pool)):

                    node = node_pool[i]
                    while node != None:
                        node.Update(re[i])
                        node = node.parentNode

            #finish_iteration_time=time.time()-iteration_time
            #print "four step time:",finish_iteration_time
        """check if found the desired compound"""

    #print "all valid compounds:",valid_compound

    finished_run_time = time.time() - start_time

    print "logp max found:", current_score
    #print "length of score:",len(current_score)
    #print "time:",time_distribution

    print "valid_com=", valid_compound
    print "num_valid:", len(valid_compound)
    print "all compounds:", len(all_simulated_compound)
    print "score=", all_score
    print "depth=", depth
    print len(depth)
    print "runtime", finished_run_time
    #print "num_searched=",num_searched
    print "100 max:", maxscore100, time100
    print "500 max:", maxscore500, time500
    print "1000 max:", maxscore1000, time1000
    print "5000 max:", maxscore5000, time5000
    print "10000 max:", maxscore10000, time10000
    return valid_compound
    def simulation(self, state):
        predicted_smile = predict_smile(model, state)
        input_smile = make_input_smile(predicted_smile)
        logp, valid_smile, all_smile = logp_calculation(input_smile)

        return logp, valid_smile, all_smile
Exemple #3
0
def MCTS(root, verbose=False):
    """initialization of the chemical trees and grammar trees"""
    run_time = time.time() + 600
    rootnode = Node(state=root)
    state = root.Clone()
    maxnum = 0
    iteration_num = 0
    start_time = time.time()
    """----------------------------------------------------------------------"""
    """global variables used for save valid compounds and simulated compounds"""
    valid_compound = []
    all_simulated_compound = []
    desired_compound = []
    max_score = -100.0
    desired_activity = []
    time_distribution = []
    num_searched = []
    current_score = []
    depth = []
    all_score = []
    """----------------------------------------------------------------------"""

    while time.time() <= run_time:
        iteration_time = time.time()

        node = rootnode  # important !    this node is different with state / node is the tree node
        state = root.Clone(
        )  # but this state is the state of the initialization .  too important !!!
        """selection step"""
        node_pool = []
        print "current found max_score:", max_score

        while node.childNodes != []:
            node = node.Selectnode()
            state.SelectPosition(node.position)
        print "state position:,", state.position
        #depth.append(len(state.position))
        """------------------------------------------------------------------"""
        """expansion step"""
        all_posible = chem_kn_simulation(model, state.position, val, val2)
        generate_smile = predict_smile(all_posible, val)
        new_compound = make_input_smile(generate_smile)
        node_index, score, valid_smile, all_smile = check_node_type(
            new_compound, SA_mean, SA_std, logP_mean, logP_std, cycle_mean,
            cycle_std)
        valid_compound.extend(valid_smile)
        all_simulated_compound.extend(all_smile)
        all_score.extend(score)
        iteration_num = len(all_simulated_compound)
        #if maxnum in [100,200,300,400,500,600,700,800,900,1000]:
        #   used_time=time.time()-start_time
        #  print used_time
        # time_distribution.append(used_time)
        #now_time=time.time()
        #num_searched.append(now_time-start_time)

        if len(node_index) == 0:
            re = 0.0
            while node != None:
                node.Update(re)
                node = node.parentNode
        else:
            for i in range(len(node_index)):
                m = node_index[i]
                maxnum = maxnum + 1
                now_time = time.time()
                if maxnum in [
                        100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
                        1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900,
                        2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800,
                        2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700,
                        3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600,
                        4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500,
                        5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400,
                        6500, 6600, 6700, 6800, 6900, 7000, 7100, 7200, 7300,
                        7400, 7500, 7600, 7700, 7800, 7900, 8000, 8100, 8200,
                        8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000, 9100,
                        9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000
                ]:
                    used_time = time.time() - start_time
                    #print used_time
                    time_distribution.append(used_time)
                num_searched.append(now_time - start_time)
                node.Addnode(m, state)
                node_pool.append(node.childNodes[i])
                #print max_score
                if score[i] >= max_score:
                    max_score = score[i]
                    current_score.append(max_score)
                else:
                    current_score.append(max_score)
                depth.append(len(state.position))
                """simulation"""
                re = 1.0 / (1.0 + np.exp(-score[i]))
                #print re
                """backpropation step"""
            #print "node pool length:",len(node.childNodes)

            for i in range(len(node_pool)):

                node = node_pool[i]
                while node != None:
                    node.Update(re)
                    node = node.parentNode
            finish_iteration_time = time.time() - iteration_time
            print "four step time:", finish_iteration_time
        """check if found the desired compound"""

    #print "all valid compounds:",valid_compound

    #print "logp max found:", current_score
    #print "length of score:",len(current_score)
    #print "time:",time_distribution

    #print "found compounds:",valid_compound
    #print "all valid compounds:", len(valid_compound)
    #print "all compounds:",len(all_simulated_compound)
    #print "all score:", all_score
    print "depth:", depth
    print len(depth)
    print "num_searched=", num_searched

    return valid_compound
def MCTS(root, verbose = False):

    """initialization of the chemical trees and grammar trees"""
    run_time=time.time()+3600*48
    rootnode = Node(state = root)
    state = root.Clone()
    """----------------------------------------------------------------------"""


    """global variables used for save valid compounds and simulated compounds"""
    valid_compound=[]
    all_simulated_compound=[]
    desired_compound=[]
    max_logp=[]
    desired_activity=[]
    depth=[]
    min_score=1000
    score_distribution=[]
    min_score_distribution=[]

    """----------------------------------------------------------------------"""

    while time.time()<=run_time:

        node = rootnode # important !    this node is different with state / node is the tree node
        state = root.Clone() # but this state is the state of the initialization .  too important !!!
        """selection step"""
        node_pool=[]

        while node.childNodes!=[]:
            node = node.Selectnode()
            state.SelectPosition(node.position)
        print "state position:,",state.position


        """------------------------------------------------------------------"""

        """expansion step"""
        expanded=expanded_node(model,state.position,val)
        nodeadded=node_to_add(expanded,val)
        all_posible=chem_kn_simulation(model,state.position,val,nodeadded)
        generate_smile=predict_smile(all_posible,val)
        new_compound=make_input_smile(generate_smile)



        node_index,rdock_score,valid_smile=check_node_type(new_compound)
        valid_compound.extend(valid_smile)
        score_distribution.extend(rdock_score)

        if len(node_index)==0:
            re=-1.0
            while node != None:
                node.Update(re)
                node = node.parentNode
        else:
            re=[]
            for i in range(len(node_index)):
                m=node_index[i]
                node.Addnode(nodeadded[m],state)
                node_pool.append(node.childNodes[i])
                depth.append(len(state.position))
                print "current minmum score",min_score
                if rdock_score[i]<=min_score:
                    min_score_distribution.append(rdock_score[i])
                    min_score=rdock_score[i]
                else:
                    min_score_distribution.append(min_score)
                """simulation"""
                re.append((-0.8*rdock_score[i])/(1+0.8*abs(rdock_score[i])))
                """backpropation step"""

            for i in range(len(node_pool)):

                node=node_pool[i]
                while node != None:
                    node.Update(re[i])
                    node = node.parentNode




        """check if found the desired compound"""

    #print "all valid compounds:",valid_compound
    #print "all active compounds:",desired_compound
    print "rdock_score",score_distribution
    print "num valid_compound:",len(valid_compound)
    print "valid compounds",valid_compound
    print "depth",depth
    print "min_score",min_score_distribution


    return valid_compound
Exemple #5
0
def MCTS(root):
    """initialization of the chemical trees and grammar trees"""
    rootnode = Node()
    maxnum = 0
    """----------------------------------------------------------------------"""
    """global variables used for save valid compounds and simulated compounds"""
    valid_compound = []
    max_score = -100.0
    current_score = []
    depth = []
    """----------------------------------------------------------------------"""

    while maxnum < 10:
        node = rootnode
        state = root.Clone()
        """selection step"""
        node_pool = []

        while node.childNodes != []:
            node = node.Selectnode()
            state.SelectPosition(node.position)
        print("state position: ", state.position)
        if len(state.position) > 3:
            a = 1
        depth.append(len(state.position))
        if len(state.position) >= 81:
            re = -1.0
            while node != None:
                node.Update(re)
                node = node.parentNode
        else:
            """------------------------------------------------------------------"""
            """expansion step"""
            """calculate how many nodes will be added under current leaf"""
            expanded = expanded_node(model, state.position,
                                     val)  # find the possible element to add
            try:
                expanded.remove(0)
            except:
                None

            nodeadded = node_to_add(expanded, val)  # add the possible element

            all_posible = chem_kn_simulation(model, state.position, val,
                                             nodeadded)  # get the branch

            generate_smile = predict_smile(all_posible, val)
            new_compound = make_input_smile(generate_smile)

            node_index, score, valid_smile = check_node_type(
                new_compound, SA_mean, SA_std, logP_mean, logP_std, cycle_mean,
                cycle_std)

            for s in valid_smile:
                if s not in valid_compound:
                    valid_compound.append(s)
                    print('!!!found ', s)
                    # if node.depth == 2:
                    #     a = 1
            if len(node_index) == 0:
                re = -1.0
                while node != None:
                    node.Update(re)
                    node = node.parentNode
            else:
                re = []
                for i in range(len(node_index)):
                    m = node_index[i]
                    print('runtime: ', maxnum)
                    print("current found max_score:", max_score)
                    print('===========================')
                    print('number of all poossible:', len(all_posible))
                    print('number of valid: ', len(valid_compound))

                    maxnum = maxnum + 1
                    node.Addnode(nodeadded[m])
                    node_pool.append(node.childNodes[i])
                    if score[i] >= max_score:
                        max_score = score[i]
                        current_score.append(max_score)
                    else:
                        current_score.append(max_score)
                    depth.append(len(state.position))
                    """simulation"""
                    re.append((0.8 * score[i]) / (1.0 + abs(0.8 * score[i])))
                    """backpropation step"""
                # print("node pool length:",len(node.childNodes))

                for i in range(len(node_pool)):
                    node = node_pool[i]
                    while node != None:
                        node.Update(re[i])
                        node = node.parentNode
        """check if found the desired compound"""

    print("logp max found:", current_score)
    # print("length of score:",len(current_score))

    print("valid_com=", valid_compound)
    print("num_valid:", len(valid_compound))
    print("depth=", depth)
    print(len(depth))
    return valid_compound