python:如何在对象之间创建交互 - 对对象的引用似乎不起作用
python: how to create interactions between objects- reference to object doesn't seem to work
我正在尝试创建一堆以不同方式交互(神经群体模拟)的对象 - 因此一个对象可能连接到其他 2 个对象和一些外部变量,另一个对象将连接到一个其他对象及其自身等
我对面向对象的东西很陌生。我的方法是让我的 class 有一个列表 self.connections=[]
它与其他对象的所有连接(我想在某个时候添加所有连接)和方法:
def addNewConnection(self,strength,source,name):
newConnection=Connection(strength,source,name) # named tuple or class
self.connections.append(newConnection)
然后在模拟的每个时间步上,我可以计算来自所有不同来源的净输入:
netInput+=[c.source * c.weight for c in self.connections]
但我不知道如何传入源作为参考,而不是值!因为随着模拟的进行,这些值很可能会发生变化。我的猜测是做一些关于:可变类型的事情,比如使 source 的值成为一个 1 元素列表……但这在计算上不是很昂贵吗?编辑:也不起作用。
听起来您要构建的是一个图表。查看图形库,例如 Python-Graph or graph-tool 来处理您的数据结构。
在此设置中,您将有两个数据结构:
一个图形,其中您的每个对象都由图形中的一个节点表示,您的连接是图形的边。每个节点都有一个字符串名称来标识它所代表的对象。
将节点名称映射到对象的字典。
此设置将允许您(相当)轻松地操作您的复杂数据结构,而无需重新实现某处已经存在的代码。看起来您已经在处理与对象之间的这些连接相关联的 "weights",因此加权图似乎是组织它们的自然方式。
这种方法也将完全绕过您当前遇到的问题,因为它会从您当前的对象中抽象出数据结构。您当前正在编写的 classes 不必知道它们包含哪种数据结构。您可以编写第二个 class 包含字符串到对象字典和图表,这将为您的应用程序的其余部分提供整个数据结构的单一接口,而不会使您当前的 classes 过于复杂。
from collections import namedtuple
Connection=namedtuple("Connection",["source","strength"])
class Unit(object):
# these first two methods just let me set up units and connections:
def __init__(self,par=1, response=[0], connections=[]):
self.par=par
self.response=response
self.connections=dict()#[]
def receiveConnection(self,source,strength,label):
# source is some other variable, strength is probably fixed but
# i'd like it to be able to change, too
#cx = Connection(label,source,strength)
#self.connections.append(cx)
self.connections[label]=Connection(source,strength)
# stuff that gets done iteratively through the sim
def update(self):
netInput=0.
# based on the value of all the sources and weights right now,
for item in self.connections.itervalues():
netInput+= item.source[0] * item.strength
self.response[0] += self.par * netInput
if __name__=="__main__":
Unit1 = Unit()
Unit2 = Unit(response=[1])
Unit1.receiveConnection(source=Unit2.response,strength=.5,label="U2toU1")
Unit2.receiveConnection(source=Unit2.response,strength=-.5,label="negativeFeedback")
for t in xrange(10):
Unit1.update()
print 'Unit1: ' + str(Unit1.response)
Unit2.update()
print 'Unit2: ' + str(Unit2.response)
这似乎确实有效,它实在是太笨拙了(而且它甚至没有合法的功能):
Unit1: [0.5]
Unit2: [0.5]
Unit1: [0.75]
Unit2: [0.25]
Unit1: [0.875]
Unit2: [0.125]
Unit1: [0.9375]
Unit2: [0.0625]
Unit1: [0.96875]
Unit2: [0.03125]
Unit1: [0.984375]
Unit2: [0.015625]
Unit1: [0.9921875]
Unit2: [0.0078125]
Unit1: [0.99609375]
Unit2: [0.00390625]
Unit1: [0.998046875]
Unit2: [0.001953125]
Unit1: [0.9990234375]
Unit2: [0.0009765625]
我正在尝试创建一堆以不同方式交互(神经群体模拟)的对象 - 因此一个对象可能连接到其他 2 个对象和一些外部变量,另一个对象将连接到一个其他对象及其自身等
我对面向对象的东西很陌生。我的方法是让我的 class 有一个列表 self.connections=[]
它与其他对象的所有连接(我想在某个时候添加所有连接)和方法:
def addNewConnection(self,strength,source,name):
newConnection=Connection(strength,source,name) # named tuple or class
self.connections.append(newConnection)
然后在模拟的每个时间步上,我可以计算来自所有不同来源的净输入:
netInput+=[c.source * c.weight for c in self.connections]
但我不知道如何传入源作为参考,而不是值!因为随着模拟的进行,这些值很可能会发生变化。我的猜测是做一些关于:可变类型的事情,比如使 source 的值成为一个 1 元素列表……但这在计算上不是很昂贵吗?编辑:也不起作用。
听起来您要构建的是一个图表。查看图形库,例如 Python-Graph or graph-tool 来处理您的数据结构。
在此设置中,您将有两个数据结构:
一个图形,其中您的每个对象都由图形中的一个节点表示,您的连接是图形的边。每个节点都有一个字符串名称来标识它所代表的对象。
将节点名称映射到对象的字典。
此设置将允许您(相当)轻松地操作您的复杂数据结构,而无需重新实现某处已经存在的代码。看起来您已经在处理与对象之间的这些连接相关联的 "weights",因此加权图似乎是组织它们的自然方式。
这种方法也将完全绕过您当前遇到的问题,因为它会从您当前的对象中抽象出数据结构。您当前正在编写的 classes 不必知道它们包含哪种数据结构。您可以编写第二个 class 包含字符串到对象字典和图表,这将为您的应用程序的其余部分提供整个数据结构的单一接口,而不会使您当前的 classes 过于复杂。
from collections import namedtuple
Connection=namedtuple("Connection",["source","strength"])
class Unit(object):
# these first two methods just let me set up units and connections:
def __init__(self,par=1, response=[0], connections=[]):
self.par=par
self.response=response
self.connections=dict()#[]
def receiveConnection(self,source,strength,label):
# source is some other variable, strength is probably fixed but
# i'd like it to be able to change, too
#cx = Connection(label,source,strength)
#self.connections.append(cx)
self.connections[label]=Connection(source,strength)
# stuff that gets done iteratively through the sim
def update(self):
netInput=0.
# based on the value of all the sources and weights right now,
for item in self.connections.itervalues():
netInput+= item.source[0] * item.strength
self.response[0] += self.par * netInput
if __name__=="__main__":
Unit1 = Unit()
Unit2 = Unit(response=[1])
Unit1.receiveConnection(source=Unit2.response,strength=.5,label="U2toU1")
Unit2.receiveConnection(source=Unit2.response,strength=-.5,label="negativeFeedback")
for t in xrange(10):
Unit1.update()
print 'Unit1: ' + str(Unit1.response)
Unit2.update()
print 'Unit2: ' + str(Unit2.response)
这似乎确实有效,它实在是太笨拙了(而且它甚至没有合法的功能):
Unit1: [0.5]
Unit2: [0.5]
Unit1: [0.75]
Unit2: [0.25]
Unit1: [0.875]
Unit2: [0.125]
Unit1: [0.9375]
Unit2: [0.0625]
Unit1: [0.96875]
Unit2: [0.03125]
Unit1: [0.984375]
Unit2: [0.015625]
Unit1: [0.9921875]
Unit2: [0.0078125]
Unit1: [0.99609375]
Unit2: [0.00390625]
Unit1: [0.998046875]
Unit2: [0.001953125]
Unit1: [0.9990234375]
Unit2: [0.0009765625]