creating xy chart using datalists

80 views
Skip to first unread message

Chris Harvey

unread,
Jan 4, 2019, 3:30:52 PM1/4/19
to GAMA
Hi,

I am trying to create a chart that plots the  'Number of prey consumed' against 'Number of prey available' for several agents - as a line graph.  I am getting an Out of Bounds error and I'm hoping for advice on how to form this chart. 


The problem started when I added the second data set into the chart.  I'm not certain about how I'm handling this second data.  I created a list for the purpose of using the datalist type.  I've tried both the xy type and the series type - and didn't see any difference.  So, I suspect that I'm not handling the preyDensity variable appropriately.  Any advice?


Thank you,


Chris



action functional_response {
        float search_area <- catch_buffer_area;
        float TotalTime <- 2592000;                                     
        float T_h_1_prey <- 50;                                   
        float HandlingTime;
        float SearchTime;
        float attack_Rate;
       
       
   
        ask environment overlapping self
        {
         myself.preyDensity <- self.ShrimpDensity_m;           
        }
        add preyDensity to: Prey_density_list_for_chart;                   //Add prey Density to global list for chart

        if (preyDensity != 0)
        {       
            HandlingTime <- num_prey_captured*T_h_1_prey;
           
            SearchTime <- TotalTime-HandlingTime ;
            if (SearchTime = 0)
            {
                SearchTime <- 0.01;
            }
           
            attack_Rate <- (search_area/SearchTime)*0.3;
           
           
            write "Handling   "  + HandlingTime   + "      SearchTime      " +  SearchTime;
            num_prey_captured <- attack_Rate*preyDensity*TotalTime/(1+attack_Rate*preyDensity*T_h_1_prey);
       
           
        }
        else
        {
            num_prey_captured <- 0.0;
        }
           
 
       
    }   



display "Functional Response"  type: java2D synchronized:true
        {
            chart "Tarpon Functional Response" type: series x_label: "Number of Prey Available" y_label: "Number of Prey Consumed"

            {
           
                datalist value: [(Tarpon collect each.num_prey_captured), Tarpon collect each.Prey_density_list_for_chart] legend:(Tarpon collect each.name) style: line color: # green;
               
            }

        }

Chris Harvey

unread,
Jan 5, 2019, 3:05:12 PM1/5/19
to GAMA
I have modified my code somewhat, but I am still getting an error on about the 3 cycle stating java error: nil value detected.  Anyone have any ideas?


display "Functional Response" type: java2D synchronized: true
{
    chart "Tarpon Functional Response" type: series x_label: "Number of Prey" y_label: "Number of Prey Consumed"
       {
           datalist value: [[Tarpon collect each.num_prey_captured],[environment collect(each.ShrimpDensity_m)]] style: line color: #green legend: Tarpon collect each.name;

Srirama Bhamidipati

unread,
Jan 5, 2019, 3:48:54 PM1/5/19
to GAMA
Briefly, if you want "number of some_agents" .. then you should rather use count

list count each.value some_condition

eg., data people count each.age>20


Also please see, you should "extract" one value per series per step for a series chart. I notice that you are extracting one list per series per step. (collect gives you a list). 


Srirama

Chris Harvey

unread,
Jan 7, 2019, 11:51:50 AM1/7/19
to GAMA
Hi Srirama,

sorry it took so long to respond, I've been ill for the last few days. I want to plot the attribute value for each agent.  So, I want a line plot that shows the relationship between the number of prey consumed and the number of prey available.

Srirama Bhamidipati

unread,
Jan 8, 2019, 7:53:28 AM1/8/19
to GAMA
Hi

But then do you mean a scatter plot? because you are saying value of y depends on value of x?

To be honest, I do not understand your question. Maybe you can show a hand-drawn chart with all variables or agent attributes on x and y axes. If it becomes clear, others can also respond.


Srirama

Chris Harvey

unread,
Jan 8, 2019, 2:15:14 PM1/8/19
to gama-p...@googlegroups.com
So here I've made a chart each line represents a different agent.  At each timestep a new data point is created showing the relationship between the available prey and the prey actually consumed.  So my X axis does not represent time, but the variable Prey Consumed.   Both of these values are attributes of the tarpon agent. The dependency between the variables is in the calculation itself.  The chart should just show the relationship between the variables at each timestep.

image.png

--
You received this message because you are subscribed to a topic in the Google Groups "GAMA" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gama-platform/ovzJIpR0bWY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gama-platfor...@googlegroups.com.
To post to this group, send email to gama-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/gama-platform.
For more options, visit https://groups.google.com/d/optout.

Srirama Bhamidipati

unread,
Jan 8, 2019, 5:15:47 PM1/8/19
to GAMA
May be this helps (random data)

/***
* Name: chart
* Author: Srirama Bhamidipati
* Description: 
* Tags: Tag1, Tag2, TagN
***/
model charts

global {
/** Insert the global definitions, variables and actions here */
list<int> prey_available <- [0]; // just to start with 0
list<int> prey_finished <- [0];

reflex randomvalues {
add rnd(10) to: prey_available; //add random number to a list
add rnd(10) to: prey_finished; //add random number to a list
}

}

experiment charts type: gui {
/** Insert here the definition of the input and output of the model */
output {
display "style_cumulative_style_chart" type: java2D {
chart "series chart" type: scatter {
data "scatter" value: {sum(prey_available), prey_finished[cycle]} color: #green thickness: 2 marker: true marker_shape: marker_circle;
}

}

}

}

To unsubscribe from this group and all its topics, send an email to gama-platform+unsubscribe@googlegroups.com.

Chris Harvey

unread,
Jan 8, 2019, 10:26:30 PM1/8/19
to gama-p...@googlegroups.com
I'm running into a Java error: index out of bounds  with this.  I changed my display type  to opengl because I've run into this issue  before and that solved it.  It didn't solve it this time.I set both preyDensity and num_prey_captured as global lists of type float.  Other than that, my code is identical to yours.  It makes me think there's a default setting that differs.  So, I haven't been able to run it yet.

To unsubscribe from this group and all its topics, send an email to gama-platfor...@googlegroups.com.

To post to this group, send email to gama-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/gama-platform.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "GAMA" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gama-platform/ovzJIpR0bWY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gama-platfor...@googlegroups.com.

Philippe Caillou

unread,
Jan 9, 2019, 5:03:04 AM1/9/19
to gama-p...@googlegroups.com

Hi Chris,

 

I am not sure why you get an error with Srirama code, it is working for me…

 

However, I can try to answer one of your previous questions : have a scatter plot with datalist. I think I may know where the error was : 

If you have for example 2 variables (x and y) and 3 agents (1, 2, 3), the value of the datalist should be :

Datalist value :[[x1,y1],[x2,y2],[x3,y3]]

 

And here is what you are currently giving with your collect statement:

Datalist value :[[x1,x2,x3],[y1,y2,y3]]

 

Here is an ugly way to correct the error :

Datalist value:rows_list(matrix([[x1,x2,x3],[y1,y2,y3]]));

eg : Datalist value:rows_list(matrix([your x collection, your y collection]))

 

(does a transposition of the data)

Or whatever other reflex that builds the correct shape.

 

A better way would be a reverse_data_shape option in the statement… And a good chart documentation :) Soon !

 

I hope this helps,

 

Philippe

 

 

 

De : Chris Harvey
Envoyé le :mercredi 9 janvier 2019 04:26
À : gama-p...@googlegroups.com
Objet :Re: [gama (8328)] Re: creating xy chart using datalists

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.

Chris Harvey

unread,
Jan 9, 2019, 6:00:21 PM1/9/19
to gama-p...@googlegroups.com
Hi Philippe,

I'm definitely still getting an out of bounds error with the scatter plot.  The plot isn't showing up when I try to run it.  I will try your fix and see if I have better luck with it.

Philippe Caillou

unread,
Jan 10, 2019, 4:47:38 AM1/10/19
to gama-p...@googlegroups.com

Hi Chris,

 

Thanks for the model.

The only thing you need for the chart to work now is legends :)

Here it is :

 

 

                                          chart "Tarpon Functional Response" type: scatter x_label: "Number of Prey" y_label: "Number of Prey Consumed"

                                          {

                                                         datalist legend:Tarpon collect each.name value:rows_list(matrix([Tarpon collect each.num_prey_captured, Tarpon collect each.preyDensity]));

                                          }

 

And the result :

 

If this is not what you wanted as a result, tell me !

 

Hope this helps,

 

Philippe

 

 

De : Chris Harvey
Envoyé le :jeudi 10 janvier 2019 00:00
À : gama-p...@googlegroups.com
Objet :Re: [gama (8330)] Re: creating xy chart using datalists

Chris Harvey

unread,
Jan 10, 2019, 10:03:28 PM1/10/19
to gama-p...@googlegroups.com
Hello Philippe,

It works!  However, there are two things.  First I plan on giving a presentation with my simulation and I would like to be able to show the charts and the simulation all at the same time. The chart doesn't let me do it. It splits the screen, but the chart doesn't show up.  The second thing, your chart is doing exactly what it's supposed to be doing, it's just not what I was thinking it would do.  I'm looking for a curve like the green one in this image.  I would like to plot a separate curve for each agent so that I have a bunch of the green curves.  The curve shape will vary based on my calculations.  I tried a series chart using your code...and it looks more like what I was after.

Thank you for your help though, I wasn't making any progress on my own. 

Chris


image.png
T3_model_display_Functional Response_size_1288x700_cycle_2_time_1547175322467.png








Arnaud Grignard

unread,
Jan 23, 2019, 9:59:18 AM1/23/19
to gama-platform
For the first question, if you want to display the chart beside your simulation (in the same display) if you use java2D you will be able to display a chart as soon as it's in the environment bounds, using opengl you can display chart outside of the bounds.

Like this
https://github.com/CityScope/CS_Simulation_GAMA/wiki/Game-IT (you can have a look in the code to see how to do it but it basically consist of decalring yoru charts in the sae display and playing with size and position)

Arnaud

Chris Harvey

unread,
Jan 23, 2019, 8:46:17 PM1/23/19
to GAMA
I didn't realize this.  Thank you. 
Reply all
Reply to author
Forward
0 new messages