JAS-mine has a specific library that performs matching between two collections of agents, based on some specific criterion. The matching methods are called from outside the agents to be matched, for instance by the Model. The simplest algorithm is a one-way matching procedure (the agents in one collection - say females - choose, while the agents in the other collection - say males - remain passive) implemented in the class SimpleMatching:
This is invoked as
The method requires 7 arguments:
The use of closures, which are relatively new to the Java language, allows a great simplification of the code. A closure is a function written by another function. Closures are so called because they enclose the environment of the parent function, and can access all variables and parameters in that function. This is useful because it allows us to have two levels of parameters. One level of parameters (the parent) controls how the function works. The other level (the child) does the work. While it is not required that the user knows about closures, it is interesting to understand why they are so useful. In the example, suppose that the females in the population are sorted according to some criterion, say beauty: the prettiest woman is the first to choose a partner, the second prettiest comes second, etc. The matchingScoreClosure sorts all possible mates according to some other criterion, say wealth. Hence, the prettiest woman gets the richest man, the second prettiest gets the second richest, etc. In such a case, a comparator would suffice to order the males in the population, as the ranking is the same irrespective of the female who is evaluating them. But suppose now that the attractiveness of a man depends on the age differential between himself and the potential partner: in such a case, the ranking is specific to each woman in the population. A simple comparator would still do the job, but the comparator should be able to access the identity of the woman who is making the evaluation as an argument, which requires a lot of not-so-straightforward coding. Closures allow to bypass this technical requirement – that has very little to do with modelling issues – because they can pass a functionality as an argument to another method; in other words, they treat functionality as method argument, or code as data. Technically, a closure is a function that refers to free variables in its lexical context. A free variable is an identifier (a name, the identity of the woman who is evaluating the men in the population, for instance) that has a definition outside the closure; it is not defined by the closure, but it is used by the closure. In other words, these free variables inside the closure have the same meaning they would have had outside the closure.
Closures in the matching() method are easier used than explained. An example is found in the Demo07 sample model. The 7 arguments are:
Hence, the whole method looks like: