0

In the given code, lists x and y are randomly assigned numbers between 0-N with some probability 0.5. I am randomly choosing an agent and removing it using func1. I am adding one agent using func2. Now I have defined two class objects x1 and x2. The input argument 'state1' of x2 is obtained from x1, and similarly 'state2' of x1 is obtained from x2.

I am not able to find a proper way for passing arguments 'state2' and 'state1' in x1 and x2. As you can see from last four lines of my code, state1 and state2 should be defined before x1 and x2 but I can't do that as class objects x1 and x2 should be defined first as I did.

What I am trying to achieve here is following: I have two populations N1 and N2. I am randomly choosing one agent(say state1) from N1 and adding it(preserving its state=state1) to N2. At the same time, I am removing one agent from N2(say state2) and adding it(preserving its state=state2) to N1 population. This process is being repeated over time with fixed time interval.

Can somebody tell me a proper way to do it and make code run ?

import random

class func():
    def __init__(self, N, state):
        self.N = N
        self.x = []
        self.y = []

        agents = range(self.N)

        for i in range(self.N):
            if random.random() < 0.5:
                self.x.append(i)
            else:
                self.y.append(i)

        agent = random.choice(agents)

    def func1(self, agent):
        if self.agent in self.x:
            self.x.remove(agent)
            return 1
        elif self.agent in self.y:
            self.y.remove(agent)
            return 2

    def func2(self, state):
        if state == 1:
            self.x.append(N)
            return self.x
        elif state == 2:
            self.y.append(N)
            return self.y

if __name__=='__main__':

    N1 = 100
    N2 = 100
    x1 = state(N1, state2)
    x2 = state(N2, state1)

    state1 = func.func1()
    state2 = func.func1()
4
  • You just can't. If x1 needs state from x2 which needs state from x1 which needs... how do you ever get started? Commented Aug 28, 2014 at 6:47
  • I know what you are saying is correct. But I am stuck with a similar problem. If I assign some initial values for state1 and state2 say (=1), will not my code then take the same values for state1 and state2 each time. I know my approach might not be correct, that's why I am asking for suggestions. Commented Aug 28, 2014 at 7:03
  • @iajay I think you indentation is wrong as well. As it looks like here, much of the code runs not at instance creation time, but at class creation time when there is no self yet. Commented Aug 28, 2014 at 7:23
  • It is not clear what you are trying to achieve, not least because func is a terribly uninformative class name and you have no docstrings. What's the bigger picture, here? Commented Aug 28, 2014 at 8:05

1 Answer 1

1

You can remove the second argument from the state constructor and pass it later, in a separate method like set_state(state).

E.g.:

x1 = state(N1)
x2 = state(N2)

state1 = func.func1()
state2 = func.func2()

setState(x1, state2)
setState(x2, state1)

In the state function you have to strip the state argument and related code to the new function setState. I do not see that code so I cannot show that here.

Sign up to request clarification or add additional context in comments.

5 Comments

@Dijkgraaf It provides an answer if you replace the terrible "Why don't you" with "A solution would be to".
@glglgl If you agree with that answer and think it is the solution, edit it so that it does read as a solution, and maybe even add some details :-) As it is it reads like a suggestion without a lot of detail.
@Dijkgraaf I think I'll leave that to Michel. I just tried to edit it, but the sentence is incomplete by itself...
@Michel Keijzers, can you please see my code pastebin.com/PdmJTUhs. and point me out where I am making mistake. My problem is where to define oAgent and how to use set_state function in my pastebin code.
I would say: add oAgent in the class for agents (however, it looks like: getRandomAgent is a better name). I don't understand fully the set_state function, it does not set something, only returns something. You also might consider using a base class Agent, and derive the other 3 sub agent classes from that to prevent a lot of almost duplicate code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.