0

私はSimpyの初心者です。私はマニュアルの最初のステップ ( http://simpy.sourceforge.net/old/SimPy_Manual/Manuals/Manual.html ) に従って、自分のやりたいことに適応させていました。私のコード

# -*- coding: utf-8 -*-
#from mpl_toolkits.mplot3d import Axes3D
#from matplotlib import cm
#import matplotlib.pyplot as plt
import numpy as np
import random as rd
import math
from SimPy.Simulation import Process, activate, hold, initialize, simulate
import sys

class Agent(Process):
    def __init__(self,i,x,y,u,v,state):
        Process.__init__(self, name='Agent' + str(i))
        self.i = i
        self.x = x
        self.y = y
        self.u = u
        self.v = v
        self.st = state

    def go(self):
        dt=1.0/math.sqrt(self.u**2+self.v**2)
        print('%s starts at %s' %(self.i,now())
        yield hold, self, dt
        print('%s changed place at %s' %(self.i,now())

initialize()
a = Agent('nano',9,0,0,1,0)
#b = Agent(1,9,0,0,1,0)
activate(a,a.go(),at=0.0)
#activate(b,b.go(),at=5.0)
simulate(until=100.0)

次のエラーが発生します。

hcecilia@helcecil:~/BRL$ python agent_sim.py 
  File "agent_sim.py", line 24
    yield hold, self, dt
        ^
SyntaxError: invalid syntax

しかし、マニュアルの正確なコードを試すと、次のようになります。

from SimPy.Simulation import Process, activate, initialize, hold, now, simulate


class Message(Process):
    """A simple Process"""
    def __init__(self, i, len):
        Process.__init__(self, name='Message' + str(i))
        self.i = i
        self.len = len

    def go(self):
        print('%s %s %s' % (now(), self.i, 'Starting'))
        yield hold, self, 100.0
        print('%s %s %s' % (now(), self.i, 'Arrived'))


initialize()
p1 = Message(1, 203)   # new message
activate(p1, p1.go())  # activate it
p2 = Message(2, 33)
activate(p2, p2.go(), at=6.0)
simulate(until=200)
print('Current time is %s' % now())  # will print 106.0

それは正常に動作します。両者の違いがわかりません。何かアイデアがあれば...

4

1 に答える 1

1

)前の行の右括弧が抜けています:

print('%s starts at %s' %(self.i,now())
#    1                   2          332?

括弧のペア番号1は閉じていません。

Python では、論理行は、すべての括弧、角括弧、および中括弧が閉じられたときにのみ終了します。閉じ括弧yieldがないと、関数呼び出しの一部と見print()なされ、有効な構文ではありません。

于 2015-03-18T11:44:19.483 に答える