Global actions for agents

92 views
Skip to first unread message

Seong

unread,
Feb 25, 2014, 1:07:11 AM2/25/14
to gama-p...@googlegroups.com

/*
* Hi, Gama.
*
* I am wondering if I can declare a global action, and then use it for agents.
* Even though I could declare an action in the global section, I cannot use it for agents, e.g. in the entities section or the init/create section.
*
* In addition, I'd like to use the keyword 'void' instead of 'action' for actions that do not return a value.
*
* Please see the code below, especially the 'Question 1a', 'Question 1b', 'Question 2' parts.
*
* Thanks for your comments in advance,
* Seong

*/

model test001
global {
   
    int x <- 0;
    int y <- 0;
    int z <- 0;
   
   
   
    action setX(int i) {
        x <- i;
    }
   
    int getX {
        return x;
    }
   
    init {
        do setX(10);
        create dummy number: 1 {
           
            //Question 1a:
            //Can I do an action, which is declared in the global section, here?
           
            //do setX(9);
           
        }
    }
}

entities {
    species dummy {
       
        reflex basic_step {
            do setY(1);
        }
       
        action setY (int num) {
            //z <-  num; // set y from the parameter of the action
            y <-  x * 2;    //set y from the global int value 'x'
           
            //Question 1b:
            //I could declare an action called 'getX' in the global section.
            //I am wondering if I can use it in the entities section like below.
           
            //z <- getX; //set z by using an action that is declared in the global section
        }
       
        int getY {
            return y;
        }
       
        //Question 2: I can't use the keyword 'void' instead of 'action'. Maybe you need to admend this point.
       
        //void setZ (int num) { z <- num;}
    }
}

experiment test001 type: gui {
    output {
       
        display global_information refresh_every: 1 {

            chart "values" type: series background: rgb("white") size: {1,0.4} position: {0, 0.05} {
                data "x" value: x color: rgb("blue") ;
                data "y" value: y color: rgb("green") ;
                data "z" value: z color: rgb("red") ;
            }
        }
    }
}
testGlobal001.gaml

Patrick Taillandier

unread,
Feb 25, 2014, 3:50:05 AM2/25/14
to gama-p...@googlegroups.com
Hi

First, in the current version of GAMA, you can not use the keyword "void" instead of "action".

Concerning the use of a global action by an agent, you can ask the world agent to apply it:

ask  world {
   do global_action;
}

or, if your action returns something, you can directly write something like:
int val <- world.global_action();


I answer your specific questions in the following model:
            ask world { // or ask myself here
            do setX(9);
            }
            //Question 1a:
            //Can I do an action, which is declared in the global section, here?
           
            //do setX(9);
           
        }
    }
}

entities {
    species dummy {
       
        reflex basic_step {
            do setY(1);
        }
       
        action setY (int num) {
            //z <-  num; // set y from the parameter of the action
            y <-  x * 2;    //set y from the global int value 'x'
           
            z <- world.getX();
            //Question 1b:
            //I could declare an action called 'getX' in the global section.
            //I am wondering if I can use it in the entities section like below.
           
            //z <- getX; //set z by using an action that is declared in the global section
        }
       
        int getY {
            return y;
        }
       
        //Question 2: I can't use the keyword 'void' instead of 'action'. Maybe you need to admend this point.
        //maybe in the further but not for the moment
       
        //void setZ (int num) { z <- num;}
    }
}

experiment test001 type: gui {
    output {
       
        display global_information refresh_every: 1 {

            chart "values" type: series background: rgb("white") size: {1,0.4} position: {0, 0.05} {
                data "x" value: x color: rgb("blue") ;
                data "y" value: y color: rgb("green") ;
                data "z" value: z color: rgb("red") ;
            }
        }
    }
}

Cheers,

Patrick



--
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.
To post to this group, send email to gama-p...@googlegroups.com.
Visit this group at http://groups.google.com/group/gama-platform.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages