开发者

modelling points on a surface

My simulation model contains agents on a surface. Agent's are represented by class Agent, their locations by class Point, and the surface itself by class Surface. Each point is really just a pair of numbers.

To m开发者_JS百科odel an agent's movement, I need to know which surface he's on (e.g., on a torus, he would never hit a land's end, but on a cylinder, he will.)

My question is whether I should add to class Point, as an instance attribute, a reference to the Surface object.

If I do, class Point becomes more complicated, and efficiency suffers (instead of dealing with pairs of numbers, I'll be dealing with pairs of numbers plus a reference). This is especially annoying since there's only one instance of class Surface ever instantiated in a single program run.

If I don't, I would not be able to give class Agent a move method. Instead I'd have to model agents' movement from an outer class that is aware of both the surface and individual agents. This approach seems logically less appealing.


I'm not sure why adding an extra attribute is an efficiency problem - it won't affect the speed and it'll only increase the size of the object by 10 bytes or so. So unless you are dealing with millions of Agents (Missterrr Annnderssssonnn) I wouldn't worry about it.

You could alternatively have a single Surface which is created in a module as a local and accessed via a module method (a bit cleaner than a global). So something like:

 import TheSurface
 class Agent:
  ...
   def Move(self,x,y):
     surface = TheSurface.getSurface()
     surface.canIMoveTo(x,y)
     ....

TheSurface.py would have some kind of initialisation method that created the surface, which would need to be called at program start. @DesignPattern people: is this a 'singleton' pattern?

Creating an Agent with a Surface and storing it as an attribute is a better way. I don't see why you'd add it to a Point though.

The structure is something like:

  • An Agent is on a Surface
  • An Agent is at a Point

Hence you can deduce that the Point is on the Surface, and there's no need to model that separately, unless perhaps you might be dealing with Points that aren't involved in some way to Agents, but then you will have the context of a Surface anyway. It'll always be "Right, now I have to put some Trees on this surface - createTree(surface,point)" etc. Anything on the surface has a surface and a point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜