From 83043180fd76d9ee31312591e9dcbc849431abc0 Mon Sep 17 00:00:00 2001 From: sersang Date: Mon, 16 Oct 2023 13:16:12 +0800 Subject: [PATCH 1/3] failed attempt at lvl 2 --- Rulebased.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/Rulebased.py b/Rulebased.py index 5dff514..380fe6a 100644 --- a/Rulebased.py +++ b/Rulebased.py @@ -22,6 +22,7 @@ # ignore sky blue colour when matching templates MASK_COLOUR = np.array([252, 136, 104]) +# MASK_COLOUR = np.array([0, 0, 0]) # (these numbers are [BLUE, GREEN, RED] because opencv uses BGR colour format by default) # You can add more images to improve the object locator, so that it can locate @@ -34,17 +35,21 @@ # filenames for object templates image_files = { "mario": { - "small": ["marioA.png", "marioB.png", "marioC.png", "marioD.png", - "marioE.png", "marioF.png", "marioG.png"], + "small": ["marioA2copy.png", "marioB2copy.png", "marioC2copy.png", "marioD2copy.png", + "marioE2copy.png", "marioF2copy.png", "marioG.png"], + # "small": ["mario-small-right.png", "mario-small-left.png"], "tall": ["tall_marioA.png", "tall_marioB.png", "tall_marioC.png"], }, "enemy": { - "goomba": ["goomba.png"], + # "goomba": ["goomba.png"], + "goomba2": ["goomba1.png", "goomba2.png"], "koopa": ["koopaA.png", "koopaB.png"], + # "koopa2": ["koopa1.png", "koopa2.png"], }, "block": { "block": ["block1.png", "block3.png"], - "floorblock":["block2.png"], + "floorblock":["rock.png"], + # "floorblock":["block2.png"], "stair":["block4.png"], "question_block": ["questionA.png", "questionB.png", "questionC.png"], "pipe": ["pipe_upper_section.png", "pipe_lower_section.png"], @@ -177,6 +182,9 @@ def locate_objects(screen, mario_status): def make_action(screen, info, step, env, prev_action): mario_status = info["status"] + stage = info["stage"] + + ### For stage 1: object_locations = locate_objects(screen, mario_status) # List of locations of Mario: @@ -188,6 +196,10 @@ def make_action(screen, info, step, env, prev_action): # List of locations of blocks, pipes, etc: block_locations = object_locations["block"] + + ##For stage 2 + + ################Above code was written by Lauren Gee and modified by us########################## ################Below code is our rule based agent########################## @@ -198,11 +210,13 @@ def make_action(screen, info, step, env, prev_action): ##Code for jumping according to enemy locations if len(enemy_locations)>0: for enemy in enemy_locations: - if enemy[0][0]>mario_locations[0][0][0]: + print("ENEMY DETECTED!!", enemy) + print("MARIO", mario_locations) + if enemy[0][0]>mario_locations[0][0][0]: ##dont need this if(enemy[0][0]-mario_locations[0][0][0]<40) and (enemy[0][1]>=180) and mario_locations[0][0][0] <= enemy[0][0]: - print(enemy) + print("ACTION!!", enemy) print(mario_locations) - action=4 + action=2 #-------------------PIPE CODE-------------------# pipeindex = 0 @@ -245,7 +259,15 @@ def make_action(screen, info, step, env, prev_action): #-------------------DEALING WITH STAIRS CODE-------------------# if len(stairs) > 0: - action = 4 + for stair in stairs: + if stair[0][1] == 192 and stair[0][0]>mario_locations[0][0][0]: + if(stair[0][0]-mario_locations[0][0][0]<40) and mario_locations[0][0][0] <= stair[0][0]: + print("ACTION!!", stair) + print(mario_locations) + action=2 + # print(stairs) + # print("STAIRS") + # action = 4 if step % 20 == 0: if prev_action == 4: action = 0 @@ -264,11 +286,14 @@ def make_action(screen, info, step, env, prev_action): if mario_locations[0][0][1] <= 66: action = 3 print(action) + + ####Stage 2 code##### + return action ####################Implementing the rule based agent################################# -env = gym.make("SuperMarioBros-v0", apply_api_compatibility=True, render_mode="human") +env = gym.make("SuperMarioBros-1-2-v0", apply_api_compatibility=True, render_mode="human") env = JoypadSpace(env, SIMPLE_MOVEMENT) state = env.reset() From a80f81c82b251be6408c1e35383133137f1528a6 Mon Sep 17 00:00:00 2001 From: sersang Date: Mon, 16 Oct 2023 23:32:16 +0800 Subject: [PATCH 2/3] FINISHED RULE BASED --- Rulebased.py | 72 ++++++++++------------------------------------------ 1 file changed, 13 insertions(+), 59 deletions(-) diff --git a/Rulebased.py b/Rulebased.py index 380fe6a..e1b2ad2 100644 --- a/Rulebased.py +++ b/Rulebased.py @@ -4,59 +4,32 @@ import cv2 as cv import numpy as np -# code for locating objects on the screen in super mario bros -# by Lauren Gee - -# Template matching is based on this tutorial: -# https://docs.opencv.org/4.x/d4/dc6/tutorial_py_template_matching.html - -################################################################################ - #Screen Height, width and match threshold constants SCREEN_HEIGHT = 240 SCREEN_WIDTH = 256 MATCH_THRESHOLD = 0.9 -################################################################################ # TEMPLATES FOR LOCATING OBJECTS # ignore sky blue colour when matching templates MASK_COLOUR = np.array([252, 136, 104]) -# MASK_COLOUR = np.array([0, 0, 0]) -# (these numbers are [BLUE, GREEN, RED] because opencv uses BGR colour format by default) - -# You can add more images to improve the object locator, so that it can locate -# more things. For best results, paint around the object with the exact shade of -# blue as the sky colour. (see the given images as examples) -# -# Put your image filenames in image_files below, following the same format, and -# it should work fine. # filenames for object templates image_files = { "mario": { - "small": ["marioA2copy.png", "marioB2copy.png", "marioC2copy.png", "marioD2copy.png", - "marioE2copy.png", "marioF2copy.png", "marioG.png"], - # "small": ["mario-small-right.png", "mario-small-left.png"], - "tall": ["tall_marioA.png", "tall_marioB.png", "tall_marioC.png"], + "small": ["marioA.png", "marioB.png", "marioC.png", "marioD.png", + "marioE.png", "marioF.png", "marioG.png"], }, "enemy": { - # "goomba": ["goomba.png"], - "goomba2": ["goomba1.png", "goomba2.png"], + "goomba": ["goomba.png"], "koopa": ["koopaA.png", "koopaB.png"], - # "koopa2": ["koopa1.png", "koopa2.png"], }, "block": { "block": ["block1.png", "block3.png"], - "floorblock":["rock.png"], - # "floorblock":["block2.png"], + "floorblock":["block2.png"], "stair":["block4.png"], - "question_block": ["questionA.png", "questionB.png", "questionC.png"], "pipe": ["pipe_upper_section.png", "pipe_lower_section.png"], }, - "item": { - "mushroom": ["mushroom_red.png"], - } } def _get_template(filename): @@ -114,7 +87,6 @@ def _locate_object(screen, templates, stop_early=False, threshold=MATCH_THRESHOL if stop_early and locations: break - # [((x,y), (width,height))] return [( loc, locations[loc]) for loc in locations] def _locate_pipe(screen, threshold=MATCH_THRESHOLD): @@ -182,9 +154,6 @@ def locate_objects(screen, mario_status): def make_action(screen, info, step, env, prev_action): mario_status = info["status"] - stage = info["stage"] - - ### For stage 1: object_locations = locate_objects(screen, mario_status) # List of locations of Mario: @@ -195,13 +164,12 @@ def make_action(screen, info, step, env, prev_action): # List of locations of blocks, pipes, etc: block_locations = object_locations["block"] - - - ##For stage 2 ################Above code was written by Lauren Gee and modified by us########################## -################Below code is our rule based agent########################## + + +################ Below code is our rule based agent ########################## #DEFAULT ACTION action = 3 @@ -210,15 +178,11 @@ def make_action(screen, info, step, env, prev_action): ##Code for jumping according to enemy locations if len(enemy_locations)>0: for enemy in enemy_locations: - print("ENEMY DETECTED!!", enemy) - print("MARIO", mario_locations) if enemy[0][0]>mario_locations[0][0][0]: ##dont need this if(enemy[0][0]-mario_locations[0][0][0]<40) and (enemy[0][1]>=180) and mario_locations[0][0][0] <= enemy[0][0]: - print("ACTION!!", enemy) - print(mario_locations) - action=2 + action=4 - #-------------------PIPE CODE-------------------# + #-------------------PIPE CODE------------------------------# pipeindex = 0 pipelist = [] pipe = False @@ -230,11 +194,9 @@ def make_action(screen, info, step, env, prev_action): #Code for jumping with pipe if pipe: for pipeindex in pipelist: - # print("pipe", pipeindex, block_locations[pipeindex]) if block_locations[pipeindex][0][0]mario_locations[0][0][0]: if(stair[0][0]-mario_locations[0][0][0]<40) and mario_locations[0][0][0] <= stair[0][0]: - print("ACTION!!", stair) - print(mario_locations) - action=2 - # print(stairs) - # print("STAIRS") - # action = 4 + action=4 + if step % 20 == 0: if prev_action == 4: action = 0 @@ -285,15 +242,12 @@ def make_action(screen, info, step, env, prev_action): #Stops mario from flying off screen when he's at the top of the stairs if mario_locations[0][0][1] <= 66: action = 3 - print(action) - - ####Stage 2 code##### return action ####################Implementing the rule based agent################################# -env = gym.make("SuperMarioBros-1-2-v0", apply_api_compatibility=True, render_mode="human") +env = gym.make("SuperMarioBros-v0", apply_api_compatibility=True, render_mode="human") env = JoypadSpace(env, SIMPLE_MOVEMENT) state = env.reset() From 00bbdf023d1b5f30a638ea131dbf33bfd2b8dc4a Mon Sep 17 00:00:00 2001 From: sersang Date: Mon, 16 Oct 2023 23:46:38 +0800 Subject: [PATCH 3/3] FINAL FINAL RULE BASED --- Rulebased.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Rulebased.py b/Rulebased.py index e1b2ad2..06820f0 100644 --- a/Rulebased.py +++ b/Rulebased.py @@ -4,6 +4,14 @@ import cv2 as cv import numpy as np +# Rule-Based Agent implementation for Gym Super Mario Bros World 1 Level 1 + +# 23599356 Sersang Ngedup +# 22957747 Kirsty Straiton + +# Template matching is based on this tutorial: +# https://docs.opencv.org/4.x/d4/dc6/tutorial_py_template_matching.html + #Screen Height, width and match threshold constants SCREEN_HEIGHT = 240 SCREEN_WIDTH = 256