Exemplo n.º 1
0
def main():
    # grid = g.read_grid('grid.txt')
    grid = g.make_grid(10, 10)

    # Print grid with a space between columns and a newline between rows
    print('\n'.join(' '.join([str(col) for col in row]) for row in grid))
    print()

    start = get_user_coords(grid, 'start')
    end = get_user_coords(grid, 'goal')

    # Prompt user to choose breadth-first or depth-first search
    while True:
        print('Choose breadth-first (0) or depth-first (1) search:', end=' ')
        try:
            # Default to breadth-first unless 1 entered
            breadth = int(input()) != 1
            break
        except ValueError:
            print('Invalid selection. Please enter 0 or 1')

    path = uninformed_search(grid, start, end, breadth)
    fname = 'path.txt'

    if path is None:
        print('No path found.')
    else:
        g.output_grid(fname, grid, start, end, path)
Exemplo n.º 2
0
    def call(self, inputs):

        if type(inputs) is not list or len(inputs) != 2:
            raise Exception(
                "Homography must be called on a list of 2 tensors, image and homography. Got: "
                + str(inputs)
            )
        images = inputs[0]
        grids = make_grid(*images.shape)
        homos = inputs[1]
        transformed_grids = apply_homography(homos, grids)
        return bilinear_sampler(images, transformed_grids)
Exemplo n.º 3
0
def error_S(m_2, N_, delta_t_, theta_, l_m1_, true_price_):
    l_err_DO = []
    l_err_CS = []
    l_err_MCS = []
    l_err_HV = []
    for m_1 in l_m1_:
        m_0 = (m_1 + 1) * (m_2 + 1)
        l_s, d_s, l_v, d_v, _, _ = grid.make_grid(m_1, S, S_0, K, c, m_2, V,
                                                  V_0, d)
        a_0, a_1, a_2, a = fac.make_matrices(m_1, m_2, m_0, rho, sigma, r_d,
                                             r_f, kappa, eta, l_s, l_v, d_s,
                                             d_v)
        b_0, b_1, b_2, b = fac.make_boundaries(m_1, m_2, m_0, r_d, r_f, N_,
                                               l_s, delta_t_)

        uu_0 = np.array([[max(l_s[i] - K, 0) for i in range(m_1 + 1)]
                         for _ in range(m_2 + 1)])
        u_0 = uu_0.flatten()

        idx_s = l_s.index(S_0)
        idx_v = l_v.index(V_0)

        price_DO = solver.DO_scheme(m_0, N_, u_0, delta_t_, theta_, a, a_0,
                                    a_1, a_2, b, b_0, b_1, b_2, r_f)[0]
        price_DO = np.reshape(price_DO, (m_2 + 1, m_1 + 1))

        price_CS = solver.CS_scheme(m_0, N_, u_0, delta_t_, theta_, a, a_0,
                                    a_1, a_2, b, b_0, b_1, b_2, r_f)[0]
        price_CS = np.reshape(price_CS, (m_2 + 1, m_1 + 1))

        price_MCS = solver.MCS_scheme(m_0, N_, u_0, delta_t_, theta_, a, a_0,
                                      a_1, a_2, b, b_0, b_1, b_2, r_f)[0]
        price_MCS = np.reshape(price_MCS, (m_2 + 1, m_1 + 1))

        price_HV = solver.HV_scheme(m_0, N_, u_0, delta_t_, theta_, a, a_0,
                                    a_1, a_2, b, b_0, b_1, b_2, r_f)[0]
        price_HV = np.reshape(price_HV, (m_2 + 1, m_1 + 1))

        l_err_DO.append(
            abs(price_DO[idx_v, idx_s] - true_price_) / true_price_)
        l_err_CS.append(
            abs(price_CS[idx_v, idx_s] - true_price_) / true_price_)
        l_err_MCS.append(
            abs(price_MCS[idx_v, idx_s] - true_price_) / true_price_)
        l_err_HV.append(
            abs(price_HV[idx_v, idx_s] - true_price_) / true_price_)

    return l_m1_, l_err_DO, l_err_CS, l_err_MCS, l_err_HV
Exemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        """
        GridSearchCVParallel accepts all the parameters for GridSearchCV from sklearn,
         but the has some additional parameters in the constructor
        callback: callable, optional
            Function to be called every every iteration
            Must accept 3 parameters: number of task done, total number of tasks and time elapsed (seconds)
        cacher: dictionary or CacherABC subclass, optional
        mapper: callable, optional: a map function (function, iterable). if not provided, itertools.imap will be used
        loader: callable, optional
            Parameterless function to be called on remote machines to load data.
            Must return X,y as a tuple, must handle its own imports
        fit_callback: callable, optional: a function to be called after _fit_and_score,
            parameters: index, {estimator, X, y, parameters, fit_params, train, test, scorer}
        """

        self.callback = kwargs.pop('callback', None)
        self.mapper = kwargs.pop('mapper', itertools.imap)
        self.cacher = kwargs.pop('cacher', {})
        self.loader = kwargs.pop('loader', None)
        self.fit_callback = kwargs.pop('fit_callback', None)
        self.max_iter = kwargs.pop('max_iter', 100)

        composite_grid = args[1]
        if isinstance(composite_grid, dict):
            grid = composite_grid
            self.transforms = {}
        else:
            grid, self.transforms = make_grid(
                *split_constraints_and_transforms(composite_grid),
                max_iter=self.max_iter)
        args = list(args)
        print('grid length: %d' % len(ParameterGrid(grid)))
        args[1] = grid
        args = tuple(args)

        super(GridSearchCVParallel, self).__init__(*args, **kwargs)
Exemplo n.º 5
0
true_price = 8.8948693600540167  # from Monte Carlo simulation (ref: https://github.com/RedwanBouizi/MC-Heston)

# grid [0, S] x [0, V]
m1 = 50  # S
m2 = 25  # V
m = (m1 + 1) * (m2 + 1)  # matrix A and vector U size
c = K / 5
d = V / 500

# line [0, T]
N = 20
delta_t = T / N

# model setup
Vec_s, Delta_s, Vec_v, Delta_v, X, Y = grid.make_grid(m1, S, S_0, K, c, m2, V,
                                                      V_0, d)
A_0, A_1, A_2, A = fac.make_matrices(m1, m2, m, rho, sigma, r_d, r_f, kappa,
                                     eta, Vec_s, Vec_v, Delta_s, Delta_v)
B_0, B_1, B_2, B = fac.make_boundaries(m1, m2, m, r_d, r_f, N, Vec_s, delta_t)

# pricing
print("--True Price", true_price)

theta = 0.8

print("\n--CN Scheme")
UU_0 = np.array([[max(Vec_s[i] - K, 0) for i in range(m1 + 1)]
                 for j in range(m2 + 1)])
U_0 = UU_0.flatten()

price, time = solver.CN_scheme(m, N, U_0, delta_t, A, B, r_f)
Exemplo n.º 6
0
    done = False

    clock = pygame.time.Clock()

    # -------- Main Program Loop -----------
    while not done:
        # --- Main event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        # start working
        visited = []

        screen.fill(Grid.BLACK)

        pygame.display.flip()

        # Limit to 60 frames per second
        clock.tick(60)

    # Close the window and quit.
    pygame.quit()


#create new Grid object for testing
input_grid = np.zeros(shape=(20, 20))
Grid = make_grid(input_grid)
Grid.print_grid()

traversal(Grid)
Exemplo n.º 7
0
    pg.image.load('./assets/sad_background.png'), (1650, 1000))
font = pg.font.Font('freesansbold.ttf', 50)
clockfont = pg.font.Font('./assets/clock_font.ttf', 90)
fontcols = {
    1: (0, 0, 255),
    2: (0, 128, 0),
    3: (255, 0, 0),
    4: (2, 13, 113),
    5: (77, 0, 0),
    6: (0, 139, 139),
    7: (0, 0, 0),
    8: (131, 131, 131),
    '*': (255, 0, 0)
}

g = make_grid(16, 32, 80)
g2 = make_blank_grid(16, 32, 80)
flags = make_blank_grid(16, 32, 80)
flagcount = 80
flagprifix = '0'
timer = 0
timerprifix = '00'
clock = pg.time.Clock()
pg.time.set_timer(pg.USEREVENT, 1000)
play = True
start = False
lost = False


def printgrid():
    global lost