本文整理汇总了Python中parameters.Parameters.get方法的典型用法代码示例。如果您正苦于以下问题:Python Parameters.get方法的具体用法?Python Parameters.get怎么用?Python Parameters.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parameters.Parameters
的用法示例。
在下文中一共展示了Parameters.get方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from parameters import Parameters [as 别名]
# 或者: from parameters.Parameters import get [as 别名]
class Model:
# Prepare the model
def __init__(self):
print("Init Model...")
self.parameters = Parameters()
self.crossings = []
self.drivers = []
# Resets the model to initial state
def reset(self):
print ("ENTERED RESET MODEL\n")
# Spawn crossings
dim = self.parameters.get('grid_size')
self.crossings = [] # Crossings in grid orientation
row = 0
# Notice!! Location is [x, y], but in the grid, one first has to access row, then column (so [y][x])
while row < dim[1]:
cr_row = []
col = 0
while col < dim[0]:
cr_row.append(Crossing([col, row]))
col += 1
self.crossings.append(cr_row)
row += 1
# Spawn drivers
self.drivers = []
n = self.parameters.get('n_drivers')
#print ("DRIVERS SPAWNED\n")
while n > 0:
driver = Driver('driver' + str(n))
self.drivers.append(driver)
#TODO remove print
#print ("DRIVER ADDED TO LIST\n")
# Put at a grid edge
new_loc = driver.respawn(dim[0], dim[1])
self.crossings[new_loc[1]][new_loc[0]].put_spawn(driver, dim[0]-1, dim[1]-1)
n -= 1
# Handles the phase in which drivers are moved inside crossings
def transintra(self):
# Make a (nested) list of starting positions at the beginning of the cycle
state = []
crashes = 0 # Number of crashes this cycle
# Iterate over every crossing
for crossing in [crossing for row in self.crossings for crossing in row]:
# Resolve situations, make drivers decide and compute outcome
sitrep = crossing.resolve()
if sitrep:
# There is actually something to do at this crossing
actions = sitrep.distribute()
crashed = sitrep.compute_outcome(actions, self.parameters.get('reward'), crossing.loc)
# state[-1][-1] = len(crashed) # Also add crashed drivers to state
crossing.move_drivers(sitrep, crashed)
crashes += len(crashed) # Add to occurrences of crashes
# Ask for the state of a crossing and append to global state
state.append(crossing.occupied())
# Give global state to the simulation (GUI)
# NOTE!! Division of crashes by 2 is under the assumption always two agents are involved
return state, crashes/2
# Phase in which drivers are moved between crossings
def transinter(self):
dim = self.parameters.get('grid_size') # Load in the grid size
# Iterate over every crossing
for crossing in [crossing for row in self.crossings for crossing in row]:
# Get the drivers that want to move elsewhere
translations, crashed = crossing.translate_drivers()
# Actually move drivers to the correct destinations
for driver, n_cr, n_dr in translations:
# Check whether a driver wants to move outside the grid
if n_cr[0] < 0 or n_cr[0] > dim[0]-1 or n_cr[1] < 0 or n_cr[1] > dim[1]-1:
# That means that the driver has reached its destination
driver.status = 'finished'
# Also make this driver respawn
new_loc = driver.respawn(dim[0], dim[1])
self.crossings[new_loc[1]][new_loc[0]].put_spawn(driver, dim[0]-1, dim[1]-1)
else:
# Just move to next crossing
self.crossings[n_cr[1]][n_cr[0]].enqueue(driver, n_dr)
# Respawn crashed drivers
# NOTE: do this after moving within crossing,
# because they still have to go to the middle, generating a transition
for driver in crashed:
new_loc = driver.respawn(dim[0], dim[1])
self.crossings[new_loc[1]][new_loc[0]].put_spawn(driver, dim[0]-1, dim[1]-1)
# DEPRECATED PROCEDURE SINCE SEGREGATION
# A procedure to update the model to the next cycle
def update(self):
# Iterate over every crossing
for crossing in [crossing for row in self.crossings for crossing in row]:
# Resolve situations, make drivers decide and compute outcome
#.........这里部分代码省略.........