-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_model_Experiment_0.py
67 lines (50 loc) · 1.77 KB
/
random_model_Experiment_0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
from itertools import count
import gymnasium as gym
import numpy as np
from skimage import transform
random.seed(42)
# Function to preprocess the game state
def preprocess_state(state):
gray = state.mean(axis=2)
cropped_frame = gray[8:-12, 4:-12]
normalized_frame = cropped_frame / 255.0
preprocessed_frame = transform.resize(normalized_frame, [84, 84])
return preprocessed_frame
RENDER = False
if RENDER:
env = gym.make("ALE/SpaceInvaders-v5", difficulty=1, render_mode='human')
else:
env = gym.make("ALE/SpaceInvaders-v5", difficulty=1)
env = GameWrapper(env)
num_games = 100
max_score = 0
min_score = float('inf')
total_scores = []
for game in range(num_games):
state, info = env.reset()
state = preprocess_state(state)
total_score = 0
for t in count():
if RENDER:
env.render()
action = random.randint(0, 5)
next_state, reward, done, info, score = env.step(action)
next_state = preprocess_state(next_state)
state = next_state
total_score += reward
if done:
print(f'Game {game + 1} Over! Total Score: {total_score}')
total_scores.append(total_score)
if total_score > max_score:
max_score = total_score
if total_score < min_score:
min_score = total_score
break
env.close()
average_score = sum(total_scores) / len(total_scores)
median_score = np.median(total_scores)
print(f'Min Score over {num_games} games: {min_score}')
print(f'Max Score over {num_games} games: {max_score}')
print(f'Median Score over {num_games} games: {median_score}')
print(f'Average Score over {num_games} games: {average_score}')