Skip to content

Commit

Permalink
Merge pull request #23 from chenle02/Tetris_Domino_Le
Browse files Browse the repository at this point in the history
Tetris Domino Le
  • Loading branch information
chenle02 authored Mar 4, 2024
2 parents 4726774 + f6e9f41 commit 0989231
Show file tree
Hide file tree
Showing 80 changed files with 1,193 additions and 145 deletions.
130 changes: 130 additions & 0 deletions Experiments/exp10/SweepParameters.py
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])
17 changes: 17 additions & 0 deletions Experiments/exp10/checkstatus.sh
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
1 change: 1 addition & 0 deletions Experiments/exp10/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a small scale experiment to compute the slope.
132 changes: 11 additions & 121 deletions Experiments/exp8/SweepParameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,124 +8,14 @@
"""
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, 200, 500, 1000]
ListRandomSeeds = [10 * i for i in range(800)]
configs = [
"../../tetris_ballistic/configs/config_piece_19_sticky.yaml",
"../../tetris_ballistic/configs/config_piece_19_nonsticky.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])
from tetris_ballistic.SweepParameters import SweepParameters as SP

ListWidth = [50, 100, 200, 500, 1000, 1500]
ListRandomSeeds = [10 * i for i in range(20)]
config_patterns = ["*piece_19_sticky.yaml",
"*piece_19_nonsticky.yaml",
"*piece_0*.yaml"]
SP(list_width=ListWidth,
list_random_seeds=ListRandomSeeds,
config_patterns=config_patterns,
ratio=10)
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

setup(
name='tetris_ballistic',
version='1.2.4',
version='1.2.5',
packages=find_packages(),
# entry_points={
# 'console_scripts': [
# 'tetrisBD=tetris_ballistic.cli.main:cli',
# ],
# },
# include any other necessary setup options here
package_data={
"tetris_ballistic": ["configs/*.yaml", "data/*.png"],
},
long_description=long_description,
long_description_content_type="text/markdown",
)
1 change: 1 addition & 0 deletions tests/test_Sweep/command.vim
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 tests/test_Sweep/config_piece_0_combined_w=50_seed=10.yaml
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 tests/test_Sweep/config_piece_0_combined_w=50_seed=20.yaml
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 tests/test_Sweep/config_piece_0_combined_w=80_seed=10.yaml
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.
Loading

0 comments on commit 0989231

Please sign in to comment.