Sample3_MoreServiceFunctions.java
Created with JBuilder

import mads.core.*;
import mads.servicefunctions.*;

/**
 * <p><H1>More service functions</H1></p>
 *
 * <p><B>Sample project no. 3 </B><BR\> </p>
 * In sample project no. 2 the basics of creating and working with service functions were shown. In this sample,
 * we go one step further and show how one can create special types of service functions, not from scratch but
 * using templates. In real simulations, this is how almost all service functions will be created. Another way
 * would be from remote agents, but this topic will not be covered here.<BR\>
 * The user should notice the following things:<BR\>
 * <UL>
 *    <LI>In most cases, there is no need to create the service functions from scratch (point by point). One can use templates
 *        instead.
 *    <LI>There is a very simple way of obtaining the average of a service functions. This will be very useful where dealing with
 *        simulations that must be run for a number of times and their results averaged
 *
 * </UL>
 * <p>This simulation will use the most important service function templates by first creating a service function using that particular
 *    template and then plotting it. Afterwards, a vector of constant service functions will be created and their average found and
 *    plotted
 * </p>
 */

public class Sample3_MoreServiceFunctions extends ASimulationAgent
{

  public void doWork(){
    /**
     * We will begin by creating a constant service function. In MADS, all parameters requested
     * by service providing agents are not simple numbers but service functions. When a parameter
     * is known to be constant (or one can do no better than assume that it is a constant), the
     * user cannot give a simple constant as parameter; a constant service function will be
     * used instead.
     * Note that in order to have access to the service function templates, the package
     * mads.servicefunctions must be included (using the "import mads.servicefunctions.*;" function)
     */

    // Create and plot a constant service function, with the value 0.5.
    // A constant service function has only one parameter, which specifies the value
    // the service function will take
    ServiceFunction sf1 = new ConstantServiceFunction(0.5);
    sf1.plot("", "Constant function");

    /**
     * Besides constant service functions, other functions can be created as well. Other templates
     * present in the framework can create: linear, logistic, gaussian and discount.
     */

    // Create and plot a linear service function
    // A linear service function has two parameters, a and b. The returned values are
    // computed using the formula f(t) = at + b
    ServiceFunction sf2 = new LinearServiceFunction(1, 1);
    sf2.plot("", "Linear service function");

    // Create and plot a logistic service function
    // A logistic service function has four parameters: a1, a2, x0 and dx, where
    // a1 represents the initial value, a2 the final value, x0 the center (the point of
    // inflection) and dx represetents the width.
    // The returned values are computed using the following formula:
    // f(x) = a2 + (a1 - a2)/(1 +e^((t-x0)/dx)))
    ServiceFunction sf3 = new LogisticServiceFunction(12, 1, 2006, 1);
    sf3.plot(2000, 2030, "", "Logistic service function");

    // Create and plot a Gaussian service function
    // A gaussian service function has four parameters: y0, x0, w and a, where
    // y0 represents the baseline offset, x0 the mean, w the double of the standard
    // deviation and a represents the total area under the curve from the baseline
    // The returned values are computed using the following formula:
    // f(x) = y0 + (a / (w * sqrt(PI*0.5))) * e^(-2*(t - x0)^2)/(w^2))
    ServiceFunction sf4 = new GaussianServiceFunction(0, 5000, 2000, 50);
    sf4.plot("", "Gaussian service function");

    // Create and plot a discount (decay) service function. Note how this function takes a service
    // function as parameter. The other parameter is the base year, the year when the decay starts
    // The returned values are computed using the following formula:
    // f(x) = e^(-rate*t)
    ServiceFunction sf5 = new DiscountServiceFunction(sf1, 2000);
    sf5.plot(2000, 2030, "", "Discount service function");


    /**
     * Usually, when doing a simulation, there is a certain degree of randomness involved
     * in the initial conditions, network structure, decision strategy, etc. In such cases, the
     * simulation is run for a number of times and the results are averaged. As the result of a
     * simulation is given as a service function, from a programming point of view averaging
     * results actually means averaging service functions.
     * To show to do this, in the following a vector of service functions will be generated and
     * their average will be obtained
     */

    // Define a vector of 10 service functions
    ServiceFunction v[] = new ServiceFunction[10];

    // Make the elements be constant service functions with values from 0 to 9
    for (int i = 0; i < 10; i++)
      v[i] = new ConstantServiceFunction(i);

    // Now take the average
    ServiceFunction avg = new AverageServiceFunction(v);
    avg.plot("", "Average function");
    }

}



Sample3_MoreServiceFunctions.java
Created with JBuilder