2016-08-25 4 views
0

間違ったリストを使用しています。パイソン - オブジェクトの呼び出しが私のpythonに新たなんだと、以下のような問題を持っている

すべてのクラスのニューロンは、「私は他のニューロンからのIDを保存する二つのリスト(map_dendrites、map_axons)を持っています。

私の問題は、 'map_synapses'が 'add_axon'を呼び出すと、呼び出されたNeuronのマップではなく、呼び出すNeuron-Objectから 'map_dendrites'リストに与えられたIDを書き込みます。意外にも、コードは同じですが(最初に実行されますが)、 'add_dendrites'が呼び出されたときはうまくいきません。

これを修正する方法を知っている人はいますか? ご協力いただきありがとうございます。

# coding=utf-8 

class Neuron(object): 
    "Klasse Neuron" 

    c_dendrites = 1 # Inputs 
    c_axons = 1 # Outputs 

    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites 
    c_unmap_axons = c_axons # same here but axons 

    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites 
    map_axons = [0 for i in range(10)] # Verkabelung Axons 

    def __init__(self, id, x, y, z): # 3D-Koordinaten 
     "Konstruktor Neuron" 

     self.id = id # Jedes Neuron besitzt eine eindeutige ID 

     print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z) 


    def add_dendrite(self, id): # Aus Sicht des Neurons (add_dendrite fügt an ein self.axon ein dendrite des anfragenden Neurons) 

     success = False 

     if(self.c_unmap_axons != 0): 

      #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons) 
      print 'Übergebene ID: %d' % id 
      #print 'Self ID: %d' % self.id 
      #print len(self.map_axons) 


      self.map_axons[self.c_axons - self.c_unmap_axons] = id # Auffüllen von 0 bis self.c_axons 
      print 'testX: %d' % self.map_axons[0] 
      self.c_unmap_axons -= 1 
      success = True 

     return success 


    def add_axon(self, id): # Aus Sicht des Neurons (add_axon fügt an ein self.dendrite ein Axon des anfragenden Neurons) 

     success = False 

     if (self.c_unmap_dendrites != 0): 

      #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons) 
      print 'Übergebene ID: %d' % id 
      #print 'Self ID: %d' % self.id 
      #print len(self.map_axons) 
      print 'test5: %d' % self.map_dendrites[0] 
      self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = id # Auffüllen von 0 bis self.c_dendrites 
      print 'test6: %d' % self.map_dendrites[0] 
# Er nimmt hier die falsche Map 
      self.c_unmap_dendrites -= 1 
      success = True 

     return success 


    def map_synapses(self, anzahl_neuronen, ar_neurons): 

     import Tools 

     print 'map_synapses: Mappe Dendrites' 

     # 1. Dendrites verkabeln aus self-Perspektive 

     while (0 != self.c_unmap_dendrites): 

      # print control1.anzahl_neuronen 

      ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1) # Neuron würfeln 

      while (self.id == ran_neuron): # Gewürfeltes Neuron darf nicht das neuron selbst sein 

       ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1) # Neuron würfeln 

      if(ar_neurons[ran_neuron].add_dendrite(self.id)): 

       #print 'range %d - %d' % (self.c_dendrites, self.c_unmap_dendrites) 
       print 'Speichere %d in map_dendrites an Stelle %d' % (ran_neuron, self.c_dendrites - self.c_unmap_dendrites) 
       self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = ran_neuron 
       print 'test1: %d' % self.map_dendrites[0] 
       self.c_unmap_dendrites -= 1 
       print 'Dendrite mapped' 

     print 'map_synapses: Mappe Dendrites abgeschlossen' 

     # 2. Axons verkabeln aus self-Perspektive 

     print 'test2: %d' % self.map_dendrites[0] 
     print 'map_synapses: Mappe Axons' 

     while (0 != self.c_unmap_axons): 

      ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1) # Neuron würfeln 

      while (self.id == ran_neuron): # Gewürfeltes Neuron darf nicht das neuron selbst sein 

       ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1) # Neuron würfeln 


      print 'test3: %d' % self.map_dendrites[0] 

      # 1. Dendrites verkabeln aus self-Perspektive 

      print 'ran %d' % ran_neuron 

      if (ar_neurons[ran_neuron].add_axon(self.id)): ## add_axon fehlerhaft !!!!!!!!!!!!!!!!! 
       print 'test4: %d' % self.map_dendrites[0] 
       print 'Speichere %d in map_axons an Stelle %d' % (ran_neuron, self.c_axons - self.c_unmap_axons) 
       self.map_axons[self.c_axons - self.c_unmap_axons] = ran_neuron 
       self.c_unmap_axons -= 1 
       print 'Axon mapped' 


     print 'map_synapses: Mappe Axons abgeschlossen' 
     print 'map_synpases: ID %d abgeschlossen' % self.id 

     self.getStatus() 

     return ar_neurons 

    def getStatus(self): 

     print 'getStatus: Neuron ID %d' % self.id 
     print 'getStatus: Dendrites_Map:' 
     print ''.join(map(str, self.map_dendrites)) 
     print 'getStatus: Axons_Map:' 
     print ''.join(map(str, self.map_axons)) 
+0

これは、あなたが(http://stackoverflow.com/help/mcve)[MCVE]でより多くを遵守することを短縮することができれば、...本当に長い一例である私たちは、はるかに簡単にあなたを助けることに役立つだろう。 – Aaron

+0

私はあなたが必要と思うのは、クラスプロパティではないオブジェクトプロパティです。 Classプロパティは、すべてのインスタンスで共有されます。 – kxxoling

答えて

2

あなたの変数の範囲に問題があると思われます。

class Neuron(object): 
    c_dendrites = 1 # Inputs 
    c_axons = 1 # Outputs 
    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites 
    c_unmap_axons = c_axons # same here but axons 
    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites 
    map_axons = [0 for i in range(10)] # Verkabelung Axons 

しかし、それはクラス自体のためだけに、どのインスタンスにも固有ではありません。静的?

init関数を使用してselfを使用して変数を定義する必要があります。つまり、インスタンスを使用する必要があるときはいつでも、selfを使用する必要があります。

def __init__(self, id, x, y, z): # 3D-Koordinaten 
    "Konstruktor Neuron" 
    self.c_dendrites = 1 # Inputs 
    self.c_axons = 1 # Outputs 
    self.c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites 
    self.c_unmap_axons = c_axons # same here but axons 
    self.map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites 
    self.map_axons = [0 for i in range(10)] # Verkabelung Axons 
    self.id = id # Jedes Neuron besitzt eine eindeutige ID 
    print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z) 
+0

ありがとうたくさんの男:) –

関連する問題