Skip to content

Instantly share code, notes, and snippets.

View fernandotenorio's full-sized avatar

fernandotenorio

  • São Paulo, Brazil
View GitHub Profile
@fernandotenorio
fernandotenorio / Linalg.py
Last active July 7, 2022 15:32
Linalg class
import numpy as np
class Linalg(object):
Zero = np.array([[1.0], [0.0]])
One = np.array([[0.0], [1.0]])
Plus = (Zero + One)/np.linalg.norm(Zero + One)
P0 = np.dot(Zero, Zero.T)
P1 = np.dot(One, One.T)
@fernandotenorio
fernandotenorio / Gate.py
Last active July 7, 2022 15:26
Quantum gate class.
import numpy as np
class Gate(object):
# Identity
I = np.eye(2)
# Hadamard
H = (1.0/np.sqrt(2)) * np.array([[1, 1], [1, -1]])
# Pauli-X (NOT)
@fernandotenorio
fernandotenorio / Experiment.py
Last active December 20, 2021 04:30
Experiment definition
from CA import CARoom, CACell
from itertools import product
import multiprocessing as mp
import numpy as np
from CustomRoom import CustomRoom
class Experiment(object):
def __init__(self, repeats=30):
self.repeats = repeats
self.results = []
@fernandotenorio
fernandotenorio / CAPanel.py
Last active December 20, 2021 04:17
Visualization for the CARoom class
import pygame
from pygame import gfxdraw
from CA import CACell, CARoom
from random import random, randint, seed, Random
from CustomRoom import CustomRoom
class SimuPanel(object):
def __init__(self, room, cell_size_px):
self.room = room
self.cell_size_px = cell_size_px
@fernandotenorio
fernandotenorio / CustomRoom.py
Last active December 20, 2021 04:19
Creates a room for the CARoom simulation
from CA import CARoom, CACell
from itertools import product
from random import random, Random
import multiprocessing as mp
class CustomRoom(object):
@staticmethod
def make_2_obstacle_room(full_factor=0.67, pos_seed=None, panic_prob=0.01, exit_size=2):
room_w = 20
room_h = 20
@fernandotenorio
fernandotenorio / CA.py
Last active December 20, 2021 04:11
Evacuation model CA implementation
from itertools import product
from math import ceil
from dijkstra import dijkstra
import numpy as np
from random import shuffle, choice, random
class CACell(object):
EMPTY_STATE = 0
PERSON_STATE = 1
@fernandotenorio
fernandotenorio / dijkstra.py
Created November 15, 2021 20:22
Dijkstra's algorithm implementation
from queue import PriorityQueue
def dijkstra(edges, start_vertex):
D = {v:float('inf') for v in edges}
D[start_vertex] = 0
pq = PriorityQueue()
pq.put((0, start_vertex))
visited = []
@fernandotenorio
fernandotenorio / n_bandit_simulation.R
Created August 2, 2013 18:29
N armed bandit simulation in R. To run it just call do.simulation()
get.testbed = function(arms = 10, plays = 500, u = 0, sdev.arm = 1, sdev.rewards = 1){
optimal = rnorm(arms, u, sdev.arm)
rewards = sapply(optimal, function(x)rnorm(plays, x, sdev.rewards))
list(optimal = optimal, rewards = rewards)
}
play.slots = function(arms = 10, plays = 500, u = 0, sdev.arm = 1, sdev.rewards = 1, eps = 0.1){
# TSP with 10 cities around a circular pattern
require(gaoptim)
op <- par(mfrow = c(2, 1))
n = 10
R = 10
angs = seq(0, 2*pi, length = n)
xp = R * cos(angs) + rnorm(n)
yp = R * sin(angs) + rnorm(n)
xp = c(xp, xp[1])
get.dist = function(x)
{
sqrt(x[1]^2 + x[2]^2)
}
get.pairs = function(N)
{
M = matrix(0, nrow = N * (N - 1)/2, ncol = 2)
x = 1:N
k = 1