|
| 1 | +#EMERY: Thresholded lexicographic multi objective RL agents |
| 2 | + |
| 3 | + |
| 4 | +from game import * |
| 5 | +from learningAgents import ReinforcementAgent |
| 6 | +from featureExtractors import * |
| 7 | + |
| 8 | +import random,util,math |
| 9 | + |
| 10 | +class SubIdealAgent(ReinforcementAgent): |
| 11 | + def __init__(self, **args): |
| 12 | + ReinforcementAgent.__init__(self, **args) |
| 13 | + self.QValues1 = util.Counter() |
| 14 | + self.QValues2 = util.Counter() |
| 15 | + self.QValues3 = util.Counter() |
| 16 | + self.legalActions = [] |
| 17 | + |
| 18 | + def getQValue1(self, state, action): |
| 19 | + return self.QValues1[(state, action)] |
| 20 | + |
| 21 | + def getQValue2(self, state, action): |
| 22 | + return min(0, self.QValues2[(state, action)]) |
| 23 | + |
| 24 | + def getQValue3(self, state, action): |
| 25 | + return min(0, self.QValues3[(state, action)]) |
| 26 | + |
| 27 | + |
| 28 | + def getAction(self, state, filter=None, train=False, supervise=False): |
| 29 | + # Pick Action |
| 30 | + self.legalActions = self.getLegalActions(state, filter, train, supervise) |
| 31 | + action = None |
| 32 | + if not self.legalActions: |
| 33 | + return action |
| 34 | + randomize = util.flipCoin(self.epsilon) |
| 35 | + if randomize: |
| 36 | + action = random.choice(self.legalActions) |
| 37 | + else: |
| 38 | + action = self.getPolicy(state) |
| 39 | + return action |
| 40 | + |
| 41 | + def update(self, state, action, nextState, reward): |
| 42 | + curQ = self.QValues1[(state, action)] |
| 43 | + self.QValues1[(state, action)] = (1 - self.alpha) * curQ + self.alpha * ( |
| 44 | + reward + self.discount * self.getValue1(nextState)) |
| 45 | + |
| 46 | + def update2(self, state, action, nextState, reward): |
| 47 | + curQ = self.QValues2[(state, action)] |
| 48 | + self.QValues2[(state, action)] = (1 - self.alpha) * curQ + self.alpha * ( |
| 49 | + reward + self.discount * self.getValue2(nextState)) |
| 50 | + |
| 51 | + def update3(self, state, action, nextState, reward): |
| 52 | + curQ = self.QValues3[(state, action)] |
| 53 | + self.QValues3[(state, action)] = (1 - self.alpha) * curQ + self.alpha * ( |
| 54 | + reward + self.discount * self.getValue3(nextState)) |
| 55 | + |
| 56 | + def getPolicy(self, state): |
| 57 | + actions1 = [] |
| 58 | + actions2 = [] |
| 59 | + if not self.legalActions: |
| 60 | + return None |
| 61 | + val2 = self.getValue2(state) |
| 62 | + for action in self.legalActions: |
| 63 | + if val2 == self.getQValue2(state, action): |
| 64 | + actions1.append(action) |
| 65 | + qvals1 = [self.getQValue3(state, act) for act in actions1] |
| 66 | + val3 = max(qvals1) |
| 67 | + for action in actions1: |
| 68 | + if val3 == self.getQValue3(state, action): |
| 69 | + actions2.append(action) |
| 70 | + qvals2 = [self.getQValue1(state, act) for act in actions2] |
| 71 | + val1 = max(qvals2) |
| 72 | + actions3 = [] |
| 73 | + for a in actions2: |
| 74 | + if val1 == self.getQValue1(state, a): |
| 75 | + actions3.append(a) |
| 76 | + return random.choice(actions3) |
| 77 | + |
| 78 | + def getValue1(self, state, filter=None, train=False): |
| 79 | + qvals = [self.getQValue1(state, action) for action in self.legalActions] |
| 80 | + if not qvals: |
| 81 | + return 0.0 |
| 82 | + return max(qvals) |
| 83 | + |
| 84 | + def getValue2(self, state, filter=None, train=False): |
| 85 | + qvals = [self.getQValue2(state, action) for action in self.legalActions] |
| 86 | + if not qvals: |
| 87 | + return 0.0 |
| 88 | + return max(qvals) |
| 89 | + |
| 90 | + def getValue3(self, state, filter=None, train=False): |
| 91 | + qvals = [self.getQValue3(state, action) for action in self.legalActions] |
| 92 | + if not qvals: |
| 93 | + return 0.0 |
| 94 | + return max(qvals) |
| 95 | + |
| 96 | +class PacmanSubIdealAgent(SubIdealAgent): |
| 97 | + def __init__(self, epsilon=0.05,gamma=0.8,alpha=0.2, numTraining=0, **args): |
| 98 | + args['epsilon'] = epsilon |
| 99 | + args['gamma'] = gamma |
| 100 | + args['alpha'] = alpha |
| 101 | + args['numTraining'] = numTraining |
| 102 | + self.index = 0 # This is always Pacman |
| 103 | + SubIdealAgent.__init__(self, **args) |
| 104 | + |
| 105 | + |
| 106 | + def getAction(self, state, filter=None, train=False, supervise=False): |
| 107 | + action = SubIdealAgent.getAction(self,state, filter, train, supervise) |
| 108 | + self.doAction(state,action) |
| 109 | + return action |
| 110 | + |
| 111 | + |
| 112 | +class ApproximateSubIdealAgent(PacmanSubIdealAgent): |
| 113 | + def __init__(self, extractor='IdentityExtractor', **args): |
| 114 | + self.featExtractor = util.lookup(extractor, globals())() |
| 115 | + PacmanSubIdealAgent.__init__(self, **args) |
| 116 | + self.weights1 = util.Counter() |
| 117 | + self.weights2 = util.Counter() |
| 118 | + self.weights3 = util.Counter() |
| 119 | + |
| 120 | + def getWeights(self): |
| 121 | + return self.weights1, self.weights2, self.weights3 |
| 122 | + |
| 123 | + def getQValue1(self, state, action): |
| 124 | + qval1 = 0.0 |
| 125 | + features = self.featExtractor.getFeatures(state, action) |
| 126 | + for feature in features: |
| 127 | + qval1 += features[feature] * self.weights1[feature] |
| 128 | + return qval1 |
| 129 | + |
| 130 | + def getQValue2(self, state, action): |
| 131 | + qval2 = 0.0 |
| 132 | + features = self.featExtractor.getFeatures(state, action) |
| 133 | + for feature in features: |
| 134 | + qval2 += features[feature] * self.weights2[feature] |
| 135 | + return min(-0.1, qval2) |
| 136 | + |
| 137 | + def getQValue3(self, state, action): |
| 138 | + qval3 = 0.0 |
| 139 | + features = self.featExtractor.getFeatures(state, action) |
| 140 | + for feature in features: |
| 141 | + qval3 += features[feature] * self.weights3[feature] |
| 142 | + return min(-0.1, qval3) |
| 143 | + |
| 144 | + def update(self, state, action, nextState, reward): |
| 145 | + features = self.featExtractor.getFeatures(state, action) |
| 146 | + difference = reward + self.discount * self.getValue1(nextState) - self.getQValue1(state, action) |
| 147 | + for feature in features: |
| 148 | + self.weights1[feature] += self.alpha * difference * features[feature] |
| 149 | + |
| 150 | + def update2(self, state, action, nextState, reward): |
| 151 | + features = self.featExtractor.getFeatures(state, action) |
| 152 | + difference = reward + self.discount * self.getValue2(nextState) - self.getQValue2(state, action) |
| 153 | + for feature in features: |
| 154 | + self.weights2[feature] += self.alpha * difference * features[feature] |
| 155 | + |
| 156 | + def update3(self, state, action, nextState, reward): |
| 157 | + features = self.featExtractor.getFeatures(state, action) |
| 158 | + difference = reward + self.discount * self.getValue3(nextState) - self.getQValue3(state, action) |
| 159 | + for feature in features: |
| 160 | + self.weights3[feature] += self.alpha * difference * features[feature] |
| 161 | + |
| 162 | + def final(self, state): |
| 163 | + # call the super-class final method |
| 164 | + PacmanSubIdealAgent.final(self, state) |
| 165 | + |
| 166 | + # did we finish training? |
| 167 | + if self.episodesSoFar == self.numTraining: |
| 168 | + # you might want to print your weights here for debugging |
| 169 | + #print('self.weights',self.weights) |
| 170 | + pass |
0 commit comments