Results 1 to 6 of 6
  1. #1
    Nicias is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Nested loop in 2D array stores same value across rows

    Hi all,

    Iím trying to run a simulation where 2 players out of N meet at each iteration t. I use a 1D array to store some properties of these players and use another 2D array to store these properties for all N players and all T iterations, since players donít need to remember them. Problem is that I donít know how but Iím only storing in this 2D array last values of the 1D array, not the whole record. For instance, if player 1 starts with some attribute named ďbeliefĒ whose value is 0.9, one record after 5 iterations could be: 0.9, 0.45, 0.67, 0.93, 0.87. But in my 2D array I just can read 0.87 five times. However, when running the program, I can see that player 1 is really changing this value of ďbeliefĒ and how it takes 0.9, 0.45, 0.67 and so onÖ Just I canít manage to store this whole record and I only save T times the same last value of it.

    Here is the code:

    Java Code:
    public void simulate()
       {      
          Uniform uniformAgent = new Uniform( 0, N-1, seed );      
    
          for ( int t = 0; t < T; t++ )
          {
             int a1 = uniformAgent.nextInt();
             System.out.println( "Agent 1 is " + Integer.toString( a1 ) );
             int a2 = uniformAgent.nextInt();
             while ( a1 == a2 )
             {
                a2 = uniformAgent.nextInt();
             }
             System.out.println( "Agent 2 is " + Integer.toString( a2 ) );     
                     
             agents[a1].play();
             agents[a2].play();
    
             for ( int n = 0; n < N; n++ )
             {
                actions[n][t] = agents[n].getAction();
             }
    
             agents[a1].setOthersAction( agents[a2].getAction() );
             agents[a2].setOthersAction( agents[a1].getAction() );
             agents[a1].updateReturns();
             agents[a2].updateReturns();
    
             double pay1 = agents[a1].getPayoff().getPayoff();
             double pay2 = agents[a2].getPayoff().getPayoff();
             System.out.println( "Payoff of agent1 is " +
                     Double.toString( pay1 ) );
             System.out.println( "Payoff of agent2 is " +
                     Double.toString( pay2 ) );
    
             System.out.println( "Belief of agent1 is "
                     + Double.toString( agents[a1].getBelief().getBelief() ) );
             System.out.println( "Belief of agent2 is "
                     + Double.toString( agents[a2].getBelief().getBelief() ) );
    
             for ( int n = 0; n < N; n++ )
             {
                beliefs[n][t] = agents[n].getBelief();
                System.out.println( beliefs[n][t].getBelief() );
                payoffs[n][t] = agents[n].getPayoff();
             }
          }
    } // end simulate method
    Thanks in advance.
    Last edited by DarrylBurke; 11-06-2012 at 01:15 PM.

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Nested loop in 2D array stores same value across rows

    Not really following this here:
    Java Code:
    for ( int n = 0; n < N; n++ )
             {
                beliefs[n][t] = agents[n].getBelief();
                System.out.println( beliefs[n][t].getBelief() );
                payoffs[n][t] = agents[n].getPayoff();
             }
    what is beliefs? an agent? a double?
    If beliefs is a double... beliefs[n][t].getBeliefs() makes no sense.
    if beliefs is an agent... I would think you'd set beliefs[n][t].setBelief(agents[n].getBelief()) or some similar (not to mention the overhead of a 2D object array o.O )

    if beliefs is a 2D array of agents... "beliefs[n][t] =" is a pass by reference and could be very ... cumbersome
    Last edited by SJF; 11-04-2012 at 12:39 AM.

  3. #3
    Nicias is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Nested loop in 2D array stores same value across rows

    Design consists of a Belief class, which inherits directly from Object. Belief class has a double field, belief, which is accessed with getBelief() method.
    Agent class has a Belief object as a field and method getBelief() returns a Belief object.
    Finally, Breen class has a 1D array of Agent objects. Breen class has simulate() method I show here. Breen also has a 2D Belief array to store each Belief object of each Agent object in each iteration of “t”.

    So agent[n].getBelief() returns a reference to a Belief object of nth agent and line 44 here should store this reference in beliefs[n][t].

    In general, the code makes this:

    for each time “t”;
    choose two random agents which are not the same;
    update some values of these two agents through play() method;
    get those values updated for all “n” agents;
    update the rest of values of these two agents;
    store the rest of values of all “n” agents;

    The code is performing as expecting in everything. If some agent has belief value of 0.9 at iteration “t=0”, it’s possible to see how it changes while the program is running, through lines 36-39 here. agent[n].getBelief() returns correct and valid values. The matter is that I can’t get to store the whole history. I want to track each value of Belief for each agent “n” for each time “t”, but my code only stores the last value for each agent as many times as I define T, in line 5. While on the prompt I can see how Belief for some agent changes from 0.9 to 0.45 to 0.66 or whatever, later this history isn’t recorded, only the last value of it, so I finish with a row like [0.66, 0.66, 0.66] instead of [0.9, 0.45, 0.66].

    I hope this makes my code clearer. The fact is that I have made this before in Matlab, with the same design, and there it runs well and store values properly. But I wanted in Java. Running this in Matlab I can go for lunch, a walk… before it finishes. In Java it just takes seconds or a couple of minutes if I make thousands of iterations. I can’t dream with making that in Matlab.

  4. #4
    Nicias is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Nested loop in 2D array stores same value across rows

    Ok. I have got that my program stores some part of the history of "beliefs" in line 43. If it takes values 0.9, 0.37, 0.65, this line of code now stores these values, not 3 times 0.65. I have made it adding a call to getBelief() method Belief class, which gives a double back. Line 43 reads now:

    Java Code:
    beliefs[n][t] = agents[n].getBelief().getBelief();
    Of course, I have also changed definition of array beliefs, to be of double type, instead of Belief class type as before. It seems here I was passing reference of objects, instead of just value, so that every time they changed inside any Agent object, they also changed in 2D array beliefs where all of them are tracked.

    However, another problem remains still. I have said now it stores part of the history, because it doesn't store all values. I mean, I create a 2D array named "beliefs" with dimensions NxT, but the output is an array of dimensions NxN.

    Maybe it helps if I add fields declaration and constructor, where this array is created, so that someone can help me:

    Java Code:
    public class Breen {
       private int N, T, seed;
       double BELIEF, HSLASH, B, C, p;
       Uniform uniform;
       double opinions[];
       Agent agents[];
       Action actions[][];
       double beliefs[][];
       Payoff payoffs[][];
    
    // some methods here
    
    public Breen( int _N, int _T, double _p, int _seed, double _belief,
               double _hslash, double _B, double _C )
       {
          this.N = _N;
          this.T = _T;
          this.seed = _seed;
          this.BELIEF = _belief;
          this.HSLASH =_hslash;
          this.B = _B;
          this.C = _C;
          this.p = _p;
          opinions = new double[N];
          agents = new Agent[N];
          uniform = new Uniform( 0.0, 1.0, seed);
    
          try {
          for ( int i = 0; i < N; i++ )
          {
             opinions[i] = bernoulli();         
             agents[i] = new Agent( BELIEF, HSLASH, opinions[i], B, C );
             
          }
    
          } catch ( Exception e )
          {
             System.err.println( e.getMessage() + "\n" );
             e.printStackTrace();
          }
    
          actions = new Action[N][T];
          beliefs = new double[N][T];
          payoffs = new Payoff[N][T];
    
       } // end constructor method
    Thanks in advance.
    Last edited by Nicias; 11-04-2012 at 07:09 PM.

  5. #5
    Nicias is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Nested loop in 2D array stores same value across rows

    Solved. Array was size NxT because of, on the interface of the program, I defined that T took value from same JTextField as N. Now it works. Sorry for any inconveniences.

  6. #6
    Nicias is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Nested loop in 2D array stores same value across rows

    This problem is already solved, so some some moderator could mark it as such.

Similar Threads

  1. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  2. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  3. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  4. Need a loop for rows and columns
    By Ceasar in forum New To Java
    Replies: 4
    Last Post: 10-09-2009, 03:03 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •