-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from chenle02/Tetris_Domino_Le
Tetris Domino Le
- Loading branch information
Showing
80 changed files
with
1,193 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# By Le Chen and Chatgpt | ||
# chenle02@gmail.com / le.chen@auburn.edu | ||
# Created at Mon Feb 19 10:30:05 PM EST 2024 | ||
# | ||
|
||
""" | ||
This script is used to sweep the parameters of the Tetris Ballistic simultaions. | ||
""" | ||
|
||
import os | ||
import sys | ||
from multiprocessing import Pool | ||
from joblib import dump | ||
from tetris_ballistic.tetris_ballistic import Tetris_Ballistic, load_density_from_config | ||
|
||
|
||
class DualLogger: | ||
def __init__(self, filepath, mode='a'): | ||
self.terminal = sys.stdout | ||
self.log = open(filepath, mode) | ||
|
||
def write(self, message): | ||
self.terminal.write(message) | ||
self.log.write(message) | ||
|
||
def flush(self): # This flush method is needed for python 3 compatibility. | ||
# This flushes the stream to the file, but not the terminal | ||
self.terminal.flush() | ||
self.log.flush() | ||
|
||
def close(self): | ||
self.log.close() | ||
|
||
|
||
def simulate(params, total_iterations): | ||
# Extract parameters | ||
w, seed, config_name, density, current_iteration = params | ||
|
||
basename = os.path.basename(config_name).replace(".yaml", "") | ||
joblib_filename = f'{basename}_w={w}_seed={seed}.joblib' | ||
config_filename = f'{basename}_w={w}_seed={seed}.yaml' | ||
fig_filename = f'{basename}_w={w}_seed={seed}.png' | ||
log_file_path = f'{basename}_w={w}_seed={seed}.log' | ||
|
||
sys.stdout = DualLogger(log_file_path, mode='a') | ||
|
||
# Check if this simulation has already been completed | ||
if os.path.exists(joblib_filename): | ||
print(f"Skipping completed simulation: {joblib_filename}") | ||
return | ||
|
||
print(f"Running simulation: {joblib_filename}") | ||
|
||
TB = Tetris_Ballistic(width=w, | ||
height=w * 10, | ||
steps=10 * w * w, | ||
seed=seed, | ||
density=density) | ||
|
||
# Check if this config file has already been saved | ||
if not os.path.exists(config_filename): | ||
print(f"Save the config file: {config_filename}") | ||
TB.save_config(config_filename) | ||
|
||
TB.Simulate() | ||
# TB.ComputeSlope() | ||
title = basename.replace("_", " ") | ||
title = title.replace("config", "Config: ") | ||
list_images = TB.list_tetromino_images() | ||
if len(list_images) > 10: | ||
print("Too many images to display: ", len(list_images)) | ||
list_images = None | ||
else: | ||
print("List Images: ", list_images) | ||
|
||
TB.ShowData(fig_filename=fig_filename, | ||
custom_text=title, | ||
images=list_images) | ||
|
||
dump(TB, joblib_filename) | ||
|
||
print(f"Finished simulation: {joblib_filename}") | ||
|
||
# Log progress | ||
progress = (current_iteration / total_iterations) * 100 | ||
progress_message = f"Progress: {progress:.2f}% Completed simulation: {joblib_filename}" | ||
log_progress(progress_message) | ||
|
||
sys.stdout.close() # Assuming sys.stdout was set to an instance of DualLogger | ||
sys.stdout = sys.__stdout__ | ||
|
||
|
||
def log_progress(progress_message): | ||
with open("simulation_progress.log", "a") as log_file: | ||
log_file.write(progress_message + "\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
ListWidth = [50, 100, 150] | ||
ListRandomSeeds = [10 * i for i in range(10)] | ||
configs = [ | ||
"../../tetris_ballistic/configs/config_piece_19_sticky.yaml", | ||
"../../tetris_ballistic/configs/config_piece_0_sticky.yaml", | ||
"../../tetris_ballistic/configs/config_piece_0_nonsticky.yaml", | ||
"../../tetris_ballistic/configs/config_piece_0_combined.yaml", | ||
] | ||
|
||
# Generate all combinations of parameters | ||
param_combinations = [ | ||
(w, | ||
seed, | ||
os.path.basename(config), | ||
load_density_from_config(f"../../tetris_ballistic/configs/{config}")) | ||
for w in ListWidth | ||
for seed in ListRandomSeeds | ||
for config in configs | ||
] | ||
|
||
total_iterations = len(param_combinations) | ||
param_combinations_with_progress = [ | ||
(w, seed, config_name, density, idx + 1) | ||
for idx, (w, seed, config_name, density) in enumerate(param_combinations) | ||
] | ||
|
||
# Use multiprocessing Pool to run simulations in parallel | ||
with Pool() as pool: | ||
# Note: Modify the simulate function to accept the total_iterations parameter if needed | ||
pool.starmap(simulate, [(params, total_iterations) for params in param_combinations_with_progress]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
# Define an array of file patterns | ||
declare -a patterns=("*w=50_*.joblib" "*w=100_*.joblib" "*w=150_*.joblib") | ||
|
||
# Header of the table | ||
printf "%-40s %-20s\n" "Pattern" "Numbers of files" | ||
printf "%-40s %-20s\n" "------------------" "-------" | ||
|
||
# Loop through each pattern | ||
for pattern in "${patterns[@]}"; do | ||
# Count the number of files matching the pattern | ||
num_files=$(ls $pattern 2> /dev/null | wc -l) | ||
|
||
# Print the pattern and the number of files | ||
printf "%-40s %-20d\n" "$pattern" "$num_files" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a small scale experiment to compute the slope. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
noremap <leader><leader> :w<CR>:!pytest %<CR> |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
tests/test_Sweep/config_piece_0_combined_w=50_seed=10.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
steps: 25000 | ||
width: 50 | ||
height: 500 | ||
seed: 10 | ||
Piece-0: [1, 1] | ||
Piece-1: [0, 0] | ||
Piece-2: [0, 0] | ||
Piece-3: [0, 0] | ||
Piece-4: [0, 0] | ||
Piece-5: [0, 0] | ||
Piece-6: [0, 0] | ||
Piece-7: [0, 0] | ||
Piece-8: [0, 0] | ||
Piece-9: [0, 0] | ||
Piece-10: [0, 0] | ||
Piece-11: [0, 0] | ||
Piece-12: [0, 0] | ||
Piece-13: [0, 0] | ||
Piece-14: [0, 0] | ||
Piece-15: [0, 0] | ||
Piece-16: [0, 0] | ||
Piece-17: [0, 0] | ||
Piece-18: [0, 0] | ||
Piece-19: [0, 0] |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
tests/test_Sweep/config_piece_0_combined_w=50_seed=20.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
steps: 25000 | ||
width: 50 | ||
height: 500 | ||
seed: 20 | ||
Piece-0: [1, 1] | ||
Piece-1: [0, 0] | ||
Piece-2: [0, 0] | ||
Piece-3: [0, 0] | ||
Piece-4: [0, 0] | ||
Piece-5: [0, 0] | ||
Piece-6: [0, 0] | ||
Piece-7: [0, 0] | ||
Piece-8: [0, 0] | ||
Piece-9: [0, 0] | ||
Piece-10: [0, 0] | ||
Piece-11: [0, 0] | ||
Piece-12: [0, 0] | ||
Piece-13: [0, 0] | ||
Piece-14: [0, 0] | ||
Piece-15: [0, 0] | ||
Piece-16: [0, 0] | ||
Piece-17: [0, 0] | ||
Piece-18: [0, 0] | ||
Piece-19: [0, 0] |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
tests/test_Sweep/config_piece_0_combined_w=80_seed=10.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
steps: 64000 | ||
width: 80 | ||
height: 800 | ||
seed: 10 | ||
Piece-0: [1, 1] | ||
Piece-1: [0, 0] | ||
Piece-2: [0, 0] | ||
Piece-3: [0, 0] | ||
Piece-4: [0, 0] | ||
Piece-5: [0, 0] | ||
Piece-6: [0, 0] | ||
Piece-7: [0, 0] | ||
Piece-8: [0, 0] | ||
Piece-9: [0, 0] | ||
Piece-10: [0, 0] | ||
Piece-11: [0, 0] | ||
Piece-12: [0, 0] | ||
Piece-13: [0, 0] | ||
Piece-14: [0, 0] | ||
Piece-15: [0, 0] | ||
Piece-16: [0, 0] | ||
Piece-17: [0, 0] | ||
Piece-18: [0, 0] | ||
Piece-19: [0, 0] |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.