Esempio n. 1
0
def min_with_nn(f, input_box, esp, func):
    game = BB(f, input_box, Interval(-999, 999), func)
    nnet = nn(game)

    nnet.load_checkpoint('./', 'best.pth.tar')

    current_box = input_box
    board = game.getBoardFromInput_box(current_box)

    r = game.getGameEnded(current_box, esp)
    step = 0
    while r == 0:
        board = game.getBoardFromInput_box(current_box)

        #print(board)

        pi, v = nnet.predict(board)
        pi = game.getValidMoves(current_box, esp) * pi
        #print(pi)
        a = np.argmax(pi)
        #print(a)
        current_box = game.getNextState(current_box, a)
        r = game.getGameEnded(current_box, esp)
        step += 1

    return r, step
Esempio n. 2
0
def bnb_with_nn_pq(f, input_box, eps, func):

    QSA = {}
    game = BB(f, input_box, Interval(-999, 999), func)
    nnet = nn(game)
    nnet.load_checkpoint('./', 'best.pth.tar')

    current_box = input_box
    board = game.getBoardFromInput_box(current_box)
    pi, v = nnet.predict(board)
    box_stack = [(v, interval(current_box))]
    QSA[game.stringRepresentation(current_box)] = (pi, v)
    g_min = eval_at_mid(f, current_box)
    step = 0

    while box_stack:

        v, cur_interval = heappop(box_stack)

        current_box = cur_interval.interval

        if g_min < get_lb(f, current_box):
            continue

        s = game.stringRepresentation(current_box)

        pi, v = QSA[s]

        pi = game.getValidMoves(current_box, eps) * pi

        if np.sum(pi) > 0:

            a = np.argmax(pi)
            if pi[a] > 0:
                pi[a] = 0
                QSA[s] = (pi, v)

                if a % 2 == 0:
                    alter_a = a + 1
                else:
                    alter_a = a - 1

                alter_box = game.getNextState(current_box, alter_a)

                current_box = game.getNextState(current_box, a)

                new_boxes = [alter_box, current_box]

                for b in new_boxes:
                    if eval_at_mid(f, b) < g_min - eps:
                        g_min = eval_at_mid(f, b)
                        #only push the box if its overestimation of lower bound is lower
                    if get_lb(f, b) < g_min - eps:

                        s = game.stringRepresentation(b)
                        if s not in QSA:
                            board = game.getBoardFromInput_box(b)
                            pi, v = nnet.predict(board)
                            QSA[s] = (pi, v)
                        if b not in box_stack:
                            heappush(box_stack, (v, interval(b)))
                            step += 1
    return g_min, step
    #-20 * exp(-0.2*sqrt(0.5*(x^2+y^2)))-exp(0.5*(cos(2*3.1415926535*x)+cos(2*3.1415926535*y)))+2.71828+20"

    i = 0

    generator = GF(["x1", "x2"], [3, 3], -5, 5, 20)
    generator.randomPara()
    function = generator.generateString(generator.coe, generator.degree_matrix)
    f = Function("x1", "x2", function)
    #f = Function("x", "y", "-20 * exp(-0.2*sqrt(0.5*(x^2+y^2)))-exp(0.5*(cos(2*3.1415926535*x)+cos(2*3.1415926535*y)))+2.71828+20")
    #Define the input domain of the function -- both[0.5,5] for x and y
    input_box = IntervalVector(2, [-5, 5])
    #Define the output range (i.e. desired value of the function) -- f range [1,1]
    output_range = Interval(-999, 999)

    g = BB(f, input_box, output_range, generator.generateFunc)
    nnet = nn(g)

    if args.load_model:
        nnet.load_checkpoint(args.load_folder_file[0],
                             args.load_folder_file[1])

    c = Coach(g, nnet, args, i)

    i += 1

    c.learn()

    while True:
        generator = GF(["x1", "x2"], [3, 3], -5, 5, 20)
        generator.randomPara()
        function = generator.generateString(generator.coe,
Esempio n. 4
0
from naive.BB import BB
from naive.pytorch.NNet import NNetWrapper as nn

from pyibex import *

THRESHOLD = 0.001

f = Function("x", "y", "-20 * exp(-0.2*sqrt(0.5*(x^2+y^2)))-exp(0.5*(cos(2*3.1415926535*x)+cos(2*3.1415926535*y)))+2.71828+20")
#Define the input domain of the function -- both[0.5,5] for x and y
input_box = IntervalVector([[-5,5],[-4,3]])
#Define the output range (i.e. desired value of the function) -- f range [1,1]
output_range = Interval(0,10)

game = BB(f, input_box, output_range)
nnet = nn(game)

nnet.load_checkpoint('temp/', 'best.pth.tar')

current_box = input_box
board = game.getBoardFromInput_box(current_box)

r = game.getGameEnded(current_box, THRESHOLD)

while r == 0:
    board = game.getBoardFromInput_box(current_box)
    pi, v = nnet.predict(board)
    pi = game.getValidMoves(current_box, THRESHOLD) * pi
    print(pi)
    a = np.argmax(pi)
    current_box = game.getNextState(current_box, a)