From d99d5ef6586365e040c3ee76b8a9a85cc1086b56 Mon Sep 17 00:00:00 2001 From: Daniel Axtens Date: Sat, 31 Jul 2010 15:51:41 +0800 Subject: [PATCH] First attempt at a matlab link. --- src/arenas/MatlabSampleAgents.py | 12 ++ src/link/MATLAB/NOTES | 1 + src/link/MATLAB/__init__.py | 0 src/link/MATLAB/agents/__init__.py | 0 src/link/MATLAB/agents/ml_angel.m | 74 +++++++++ src/link/MATLAB/agents/ml_angel.py | 14 ++ src/link/MATLAB/matlab-consts.m | 58 +++++++ src/link/MATLAB/matlab-link.m | 49 ++++++ .../matlab-link.xcodeproj/project.pbxproj | 145 ++++++++++++++++++ src/link/MATLAB/ml_agent_template.m | 67 ++++++++ src/link/externAgent.py | 8 +- 11 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 src/arenas/MatlabSampleAgents.py create mode 100644 src/link/MATLAB/NOTES create mode 100644 src/link/MATLAB/__init__.py create mode 100644 src/link/MATLAB/agents/__init__.py create mode 100644 src/link/MATLAB/agents/ml_angel.m create mode 100644 src/link/MATLAB/agents/ml_angel.py create mode 100644 src/link/MATLAB/matlab-consts.m create mode 100644 src/link/MATLAB/matlab-link.m create mode 100644 src/link/MATLAB/matlab-link.xcodeproj/project.pbxproj create mode 100644 src/link/MATLAB/ml_agent_template.m diff --git a/src/arenas/MatlabSampleAgents.py b/src/arenas/MatlabSampleAgents.py new file mode 100644 index 0000000..eaf0a50 --- /dev/null +++ b/src/arenas/MatlabSampleAgents.py @@ -0,0 +1,12 @@ +'''MatlabSampleAgents.py +An areana that runs the sample bots in Matlab. +Written by Daniel Axtens for the UCC Programming Competition in 2010. + +Licensed under an MIT-style license: see the LICENSE file for details. +''' + +from link.MATLAB.agents.ml_angel import ml_angel +from SampleAgents import Angel, Lucifer, Dummy, Frenchie, Streetfighter + +class arena: + Agents = [ml_angel,Frenchie] diff --git a/src/link/MATLAB/NOTES b/src/link/MATLAB/NOTES new file mode 100644 index 0000000..d926981 --- /dev/null +++ b/src/link/MATLAB/NOTES @@ -0,0 +1 @@ +Start octave with -q, otherwise you'll get a massive message. diff --git a/src/link/MATLAB/__init__.py b/src/link/MATLAB/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/link/MATLAB/agents/__init__.py b/src/link/MATLAB/agents/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/link/MATLAB/agents/ml_angel.m b/src/link/MATLAB/agents/ml_angel.m new file mode 100644 index 0000000..cde7837 --- /dev/null +++ b/src/link/MATLAB/agents/ml_angel.m @@ -0,0 +1,74 @@ +# +# ml_angel.m +# matlab-link +# +# Created by Daniel Axtens on 28/07/10. +# Licensed under an MIT-style license: see the LICENSE file for details. +# + +# don't allow this file to be mistaken for a function file. +1; + +# import various constants and utilities +source( "link/MATLAB/matlab-consts.m" ) + +# defence = Defend( foeName , foePromisedAttack ); +# +# foeName: string - the name of your foe; +# foePromisedAttack : string - the item your foe promised to use +# defence: string - the item you wish to use to defend; +# +# Called when your agent needs to defend itself. + +function defence = Defend( foeName, foePromisedAttack ) + + global rps; + + defence = foePromisedAttack; + +endfunction + + +# [attack, bluff] = Attack( foeName ) +# +# foeName: string - the name of your foe +# attack: string - the actual attack you want to use +# bluff: string - the bluff attack that you say you will use. +# +# Called when your agent needs to attack another agent. + + +function [attack, bluff] = Attack( foeName ) + + global rps; + + attack = RandomAttack(); + bluff = attack; + +endfunction + +# Results( foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, +# pointDelta ) +# +# foeName: string - the name of your foe; +# isInstigatedByYou : 0=you defended/1=you attacked; +# winner : RESULTTYPE - who won +# attItem : ITEMTYPE - the item used to attack; +# defItem : ITEMTYPE - the item used to defend; +# bluffItem : ITEMTYPE - the item that was promised +# pointDelta : integer - how your points were affected. +# +# +#Called after your agent battles another agent, to tell you how the battle goes. + +function Results( foeName, isInstigatedByYou, winner, attItem, defItem, + bluffItem, pointDelta ) + + # do nothing + 1; + +endfunction + +# This invokes the matlab-link library, which allows your code to talk +# to the judging software +source( "link/MATLAB/matlab-link.m" ); \ No newline at end of file diff --git a/src/link/MATLAB/agents/ml_angel.py b/src/link/MATLAB/agents/ml_angel.py new file mode 100644 index 0000000..a5f4b5b --- /dev/null +++ b/src/link/MATLAB/agents/ml_angel.py @@ -0,0 +1,14 @@ +# ml_angel.py - a custom bot shell generated by bundle-agent.sh for UCC::ProgComp 2010 +# calls "/Applications/Octave/Octave.app/Contents/Resources/bin/octave -qf link/MATLAB/agents/ml_angel.m" to invoke the bot + +from link.externAgent import externAgent + +class ml_angel (externAgent): + def __init__ (self): + externAgent.__init__(self, ["/Applications/Octave/Octave.app/Contents/Resources/bin/octave", "-qf", "link/MATLAB/agents/ml_angel.m"]) + + def __del__(self): + try: + self.process.kill() + except: + None \ No newline at end of file diff --git a/src/link/MATLAB/matlab-consts.m b/src/link/MATLAB/matlab-consts.m new file mode 100644 index 0000000..5b3b3a6 --- /dev/null +++ b/src/link/MATLAB/matlab-consts.m @@ -0,0 +1,58 @@ +# +# matlab-consts.m +# matlab-link +# +# Created by Daniel Axtens on 28/07/10. +# Licensed under an MIT-style license: see the LICENSE file for details. +# + +# A collection of useful functions and constants for use in your agent-writing. +# Please use the symbolic values (see the sample agents) and don't use hard-coded +# values: if we have to change how things are represented internally, for example +# to make things run faster, we don't want to suddenly have your agent break. +# +# For example, use rps.rock to get a rock, don't use 1. + + +# Define some constants +# access these with rps., see the sample agents for examples + +global rps = struct ( + "rock", 1, + "paper", 2, + "scissors", 3, + + "itemToString", {{ "Rock", "Paper", "Scissors" }} +); + +# Random Item/Attack +function item = RandomAttack() + global rps + + switch (floor(rand(1)*3)) + case 0 + item = rps.rock; + case 1 + item = rps.paper; + otherwise + item = rps.scissors; + endswitch +endfunction + + +function item = stringToItem( itemString ) + global rps; + + switch itemString + case "Rock" + item = rps.rock; + case "Scissors" + item = rps.scissors; + case "Paper" + item = rps.paper; + otherwise + # this shouldn't happen; this is passed by the supervisor, not a user + error( "tried to convert invalid string into Rock/Paper/Scissors!" ) + endswitch + +endfunction diff --git a/src/link/MATLAB/matlab-link.m b/src/link/MATLAB/matlab-link.m new file mode 100644 index 0000000..46cb2ba --- /dev/null +++ b/src/link/MATLAB/matlab-link.m @@ -0,0 +1,49 @@ +# +# matlab-link.m +# matlab-link +# +# Created by Daniel Axtens on 28/07/10. +# Licensed under an MIT-style license: see the LICENSE file for details. +# + +# You don't need to read this file. All you need to do is take agent-template.m +# and fill it in with your own functions. This file does all the I/O for you, +# and matlab-consts.m defines useful utilities and constants. +# +# Do not depend on the contents of this file. It may be changed for speed, security +# or other reasons and your bot may break if you depend on it. You have been warned. + +in = input("","s"); + +while ( !strcmp( "BYE", in ) ) + + splitIn = strsplit( in, " " ); + + switch splitIn{1} + + case "ATTACK" + [attack, bluff] = Attack( splitIn{2} ); + + disp( [ "ATTACKING ", rps.itemToString{attack}, " ", \ + rps.itemToString{bluff} ] ); + + case "DEFEND" + defence = Defend( splitIn{2}, stringToItem(splitIn{3}) ); + disp( [ "DEFENDING ", rps.itemToString{defence} ] ); + + case "RESULTS" + Results( splitIn{2}, strcmp( splitIn{3}, "True" ), splitIn{4}, \ + stringToItem(splitIn{5}), stringToItem(splitIn{6}), \ + stringToItem(splitIn{7}), str2num(splitIn{8}) ); + + disp( "OK" ); + + endswitch + + fflush(stdout); + fflush(stderr); + + in = input("","s"); + + +endwhile \ No newline at end of file diff --git a/src/link/MATLAB/matlab-link.xcodeproj/project.pbxproj b/src/link/MATLAB/matlab-link.xcodeproj/project.pbxproj new file mode 100644 index 0000000..ffdc8fb --- /dev/null +++ b/src/link/MATLAB/matlab-link.xcodeproj/project.pbxproj @@ -0,0 +1,145 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 45; + objects = { + +/* Begin PBXFileReference section */ + 22286D4F11FFD7ED00C5AE4D /* matlab-link.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "matlab-link.m"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.simpleColoring; }; + 22286D6A11FFE54C00C5AE4D /* ml_angel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ml_angel.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.simpleColoring; }; + 22286D6D11FFEC5A00C5AE4D /* matlab-consts.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "matlab-consts.m"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.simpleColoring; }; + 227E50DE12040D3E00A6CBB8 /* ml_agent_template.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ml_agent_template.m; sourceTree = ""; }; + 227E50DF12040D4500A6CBB8 /* NOTES */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = NOTES; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXGroup section */ + 08FB7794FE84155DC02AAC07 /* matlab-link */ = { + isa = PBXGroup; + children = ( + 22286D6D11FFEC5A00C5AE4D /* matlab-consts.m */, + 22286D4F11FFD7ED00C5AE4D /* matlab-link.m */, + 227E50DE12040D3E00A6CBB8 /* ml_agent_template.m */, + 227E50DF12040D4500A6CBB8 /* NOTES */, + 22286D6911FFE53300C5AE4D /* agents */, + ); + name = "matlab-link"; + sourceTree = ""; + }; + 22286D6911FFE53300C5AE4D /* agents */ = { + isa = PBXGroup; + children = ( + 22286D6A11FFE54C00C5AE4D /* ml_angel.m */, + ); + path = agents; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXLegacyTarget section */ + D28A88AD04BDD90700651E21 /* matlab-link */ = { + isa = PBXLegacyTarget; + buildArgumentsString = "$(ACTION)"; + buildConfigurationList = 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "matlab-link" */; + buildPhases = ( + ); + buildToolPath = /usr/bin/make; + dependencies = ( + ); + name = "matlab-link"; + passBuildSettingsInEnvironment = 1; + productName = "matlab-link"; + }; +/* End PBXLegacyTarget section */ + +/* Begin PBXProject section */ + 08FB7793FE84155DC02AAC07 /* Project object */ = { + isa = PBXProject; + buildConfigurationList = 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "matlab-link" */; + compatibilityVersion = "Xcode 3.1"; + hasScannedForEncodings = 1; + mainGroup = 08FB7794FE84155DC02AAC07 /* matlab-link */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + D28A88AD04BDD90700651E21 /* matlab-link */, + ); + }; +/* End PBXProject section */ + +/* Begin XCBuildConfiguration section */ + 1DEB919008733D9F0010E9CD /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_PHASE_STRIP = NO; + DEBUGGING_SYMBOLS = YES; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_GENERATE_DEBUGGING_SYMBOLS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + OTHER_CFLAGS = ""; + OTHER_LDFLAGS = ""; + PRODUCT_NAME = "matlab-link"; + }; + name = Debug; + }; + 1DEB919108733D9F0010E9CD /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_PHASE_STRIP = YES; + GCC_ENABLE_FIX_AND_CONTINUE = NO; + OTHER_CFLAGS = ""; + OTHER_LDFLAGS = ""; + PRODUCT_NAME = "matlab-link"; + }; + name = Release; + }; + 1DEB919408733D9F0010E9CD /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + ONLY_ACTIVE_ARCH = YES; + PREBINDING = NO; + SDKROOT = macosx10.6; + }; + name = Debug; + }; + 1DEB919508733D9F0010E9CD /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + PREBINDING = NO; + SDKROOT = macosx10.6; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "matlab-link" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1DEB919008733D9F0010E9CD /* Debug */, + 1DEB919108733D9F0010E9CD /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "matlab-link" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1DEB919408733D9F0010E9CD /* Debug */, + 1DEB919508733D9F0010E9CD /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 08FB7793FE84155DC02AAC07 /* Project object */; +} diff --git a/src/link/MATLAB/ml_agent_template.m b/src/link/MATLAB/ml_agent_template.m new file mode 100644 index 0000000..01d6d58 --- /dev/null +++ b/src/link/MATLAB/ml_agent_template.m @@ -0,0 +1,67 @@ +# +# your-agent-name-here.m +# Your Name Here +# +# Created by Daniel Axtens on 28/07/10. +# Licensed under an MIT-style license: see the LICENSE file for details. +# + +# import various constants and utilities +source( "link/MATLAB/matlab-consts.m" ) + +# defence = Defend( foeName , foePromisedAttack ); +# +# foeName: string - the name of your foe; +# foePromisedAttack : string - the item your foe promised to use +# defence: string - the item you wish to use to defend; +# +# Called when your agent needs to defend itself. + +function defence = Defend( foeName, foePromisedAttack ) + + defence = foePromisedAttack; + +endfunction + + +# [attack, bluff] = Attack( foeName ) +# +# foeName: string - the name of your foe +# attack: string - the actual attack you want to use +# bluff: string - the bluff attack that you say you will use. +# +# Called when your agent needs to attack another agent. + + +function [attack, bluff] = Attack( foeName ) + + attack = RandomAttack(); + bluff = attack; + +endfunction + +# Results( foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, +# pointDelta ) +# +# foeName: string - the name of your foe; +# isInstigatedByYou : 0=you defended/1=you attacked; +# winner : RESULTTYPE - who won +# attItem : ITEMTYPE - the item used to attack; +# defItem : ITEMTYPE - the item used to defend; +# bluffItem : ITEMTYPE - the item that was promised +# pointDelta : integer - how your points were affected. +# +# +# Called after your agent battles another agent, to tell you how the battle goes. + +function Results( foeName, isInstigatedByYou, winner, attItem, defItem, + bluffItem, pointDelta ) + + # do nothing + 1; + +endfunction + +# This invokes the matlab-link library, which allows your code to talk +# to the judging software. Do not remove this line. +source( "link/MATLAB/matlab-link.m" ); \ No newline at end of file diff --git a/src/link/externAgent.py b/src/link/externAgent.py index 6112482..a25b878 100644 --- a/src/link/externAgent.py +++ b/src/link/externAgent.py @@ -68,6 +68,7 @@ class externAgent (BaseAgent): self.process.stdin.write ( ' '.join( ["ATTACK", repr(foe), "\r\n"] ) ) #print >>sys.stderr, self.process.stderr.readlines() result = self.process.stdout.readline().split() + #print result try: attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] ) return attack, bluff @@ -76,16 +77,17 @@ class externAgent (BaseAgent): print "Agent is insane:", self pass - def Defend (self, foe, bluff ): + def Defend (self, foe, bluff): self.process.stdin.write ( ' '.join( ["DEFEND", repr(foe), self.itemToString( bluff ), "\r\n"] ) ) #print >>sys.stderr, self.process.stderr.readlines() result = self.process.stdout.readline().split() + #print result try: defence = self.stringToItem( result[1] ) return defence - except: + except Exception, e: #agent is insane - print "Agent is insane:", self + print "Agent is insane:", self, ":", e pass def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta): -- 2.20.1