Skip to content

Instantly share code, notes, and snippets.

View mlabonne's full-sized avatar

Maxime Labonne mlabonne

View GitHub Profile
# Get data
minerl.data.download(directory='data', environment='MineRLTreechop-v0')
data = minerl.data.make("MineRLTreechop-v0", data_dir='data', num_workers=2)
# Model
model = CNN((3, 64, 64), 7).cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
criterion = nn.CrossEntropyLoss()
# Training loop
class CNN(nn.Module):
def __init__(self, input_shape, output_dim):
super().__init__()
n_input_channels = input_shape[0]
self.cnn = nn.Sequential(
nn.Conv2d(n_input_channels, 32, kernel_size=8, stride=4),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.BatchNorm2d(64),
script = []
script += [''] * 20
script += ['forward'] * 5
script += ['attack'] * 61
script += ['camera:[-10,0]'] * 7 # Look up
script += ['attack'] * 240
script += ['jump']
script += ['forward'] * 10 # Jump forward
script += ['camera:[-10,0]'] * 2 # Look up
script += ['attack'] * 150
# Define the sequence of actions
script = ['forward'] * 5 + [''] * 40
env = gym.make('MineRLObtainDiamond-v0')
env = Recorder(env, './video', fps=60)
env.seed(21)
obs = env.reset()
for action in script:
# Get the action space (dict of possible actions)
env = gym.make('MineRLObtainDiamond-v0')
env.seed(21)
class CountSolutions(cp_model.CpSolverSolutionCallback):
"""Count the number of solutions."""
def __init__(self):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__solution_count = 0
def on_solution_callback(self):
self.__solution_count += 1
# Solve problem
status = solver.Solve(model)
# If an optimal solution has been found, print results
if status == cp_model.OPTIMAL:
print('================= Solution =================')
print(f'Solved in {solver.WallTime():.2f} milliseconds')
print()
print(f'Optimal value = {3*solver.Value(bread)+10*solver.Value(meat)+26*solver.Value(beer)} popularity')
print('Food:')
# 3. Objective
model.Maximize(3 * bread
+ 10 * meat
+ 26 * beer)
# 2. Constraints
model.Add(1 * bread
+ 3 * meat
+ 7 * beer <= capacity)
# Instantiate model and solver
model = cp_model.CpModel()
solver = cp_model.CpSolver()
# 1. Variables
capacity = 19
bread = model.NewIntVar(0, capacity, 'bread')
meat = model.NewIntVar(0, capacity, 'meat')
beer = model.NewIntVar(0, capacity, 'beer')