65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from debuggable import Debuggable
|
|
|
|
|
|
class Pattern(Debuggable):
|
|
def __init__(self, arg):
|
|
super(Pattern, self).__init__()
|
|
for i in arg:
|
|
setattr(self, i, arg[i])
|
|
|
|
class ContentPlanner(Debuggable):
|
|
"""Planner for slots content allocation """
|
|
|
|
# def __init__(self):
|
|
|
|
# the patterns memory
|
|
# patternsList = []
|
|
|
|
# define the possible schedule contents
|
|
|
|
# min_weight
|
|
# max_weight
|
|
# score_first
|
|
# score_same
|
|
# score_different
|
|
# score_final
|
|
|
|
|
|
|
|
def plan(self,state, memory, plan):
|
|
|
|
# Read previous actions from memory
|
|
actionsMemory = memory.getActionsMemory()
|
|
finished = False
|
|
energy = state.energy()
|
|
|
|
finished = False
|
|
while not finished and energy > 0:
|
|
|
|
actionsWeightSum = sum(actionsMemory.values()) if actionsMemory else 1
|
|
print(__file__,actionsMemory, actionsWeightSum, self.weightTable)
|
|
|
|
for action in self.actions:
|
|
if energy + action.energy < 0 :
|
|
continue
|
|
action_weight = ( (actionsWeightSum - actionsMemory[action.name] ) / actionsWeightSum ) if action.name in actionsMemory else 1
|
|
print(__file__, action.name, actionsMemory,action.name in actionsMemory,action_weight)
|
|
self.weightTable[action.name] = action_weight
|
|
print( self.weightTable)
|
|
|
|
high_score = max(self.weightTable, key=self.weightTable.get)
|
|
chosen_action = self.actions[self.actionsIndex[high_score]]
|
|
|
|
energy += chosen_action.energy
|
|
plan.addAction( chosen_action )
|
|
print ( energy, chosen_action.name )
|
|
|
|
if action.name in actionsMemory:
|
|
actionsMemory[chosen_action.name] += 1
|
|
else :
|
|
actionsMemory[chosen_action.name] = 1
|
|
if energy < 0:
|
|
finished = True
|
|
|
|
|
|
return plan |