swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handling equations as objects (CODE)


From: Miles Parker
Subject: Re: Handling equations as objects (CODE)
Date: Fri, 21 Aug 1998 11:13:57 -0400

Jayshree-

It seems to me that you're on the right track here...I say this because I've 
implemented something very similar <g>. Unfortunatly, this is in Java for our 
agent modeling framework, but I've included the code to give you some idea of 
how you might procede; of course, the basic idea is pretty simple. This code in 
particular provides a method for optimization of a unimodal one-dimensional 
function, which should give you an idea of the kind of thing this abstraction 
lets you accomplish. (Please excuse the formatting, courtesy of my email 
client.)

I would suggest that the parameters if they are agent specific get read into 
the agents not the functions,and that the agents set the parameters when they 
use the functions. Alternatively, I suppose you could instantiate a function 
for each agent.

Hopes this helps,

-Miles

*******

package mypackage;

import ascape.util.*;

public class MyFunction extends Function {

    //Some fixed (or agent specific, etc..) equation factor.
    protected double sigma = 1;

    public double solveFor(double e) {
        //Some function using e and sigma
   }

    public double getSigma() {
        return sigma;
    }

    public void setSigma(double sigma) {
        this.sigma = sigma;
    }
}

*******

package ascape.util;

import java.util.*;

/*
 * A one-dimensional function.
 */
public abstract class Function {

        /**
         * Tau "magic" number. ~1.61803
         **/
        public static final double tau = (1 + Math.sqrt(5)) / 2;

        /**
         * Large end of golden section. ~.61803
         **/
        public static final double sectLarge = tau - 1;

        /**
         * Small end of golden section. ~.38197
         **/
        public static final double sectSmall = 1 - sectLarge;

        /**
         * Desired resolution of maximization.
         **/
        public static final double resolution = 0.01;

        /**
         * The first measurement of the current interval.
         * For minor performance reasons, this and other measurements are not 
initialized in the
         * body of the maximization functions. This can easily be changed if 
desired.
         * Also, please note that this class is _not_ thread safe; in order to 
make it
         * so, simply initialize the following variables within a contructor 
(or in the method
         * body), make them non-static, and synchronize the methods as 
appropriate. 
         **/
        protected static double x1 = 0.0;

        /**
         * The second measurement of the current interval.
         **/
        protected static double x2 = 0.0;

        /**
         * The third measurement of the current interval.
         **/
        protected static double x3 = 0.0;

        /**
         * The fourth measurement of the current interval.
         **/
        protected static double x4 = 0.0;
    
        /**
         * The result value for the second measurement.
         **/
        protected static double f2 = 0.0;

        /**
         * The result value for the third measurement.
         **/
        protected static double f3 = 0.0;
        
    /**
     * The X axis gap between the first and second measurements
     **/
    protected double gap1 = 0;
    
    /**
     * The X axis gap between the second and third measurements 
     * (after one measurement has been dropped, leaving three total.)
     **/
    protected double gap2 = 0;
    
    /**
     * Maximize the output of this function, assuming function is unimodal,
     * using a golden section search strategy.
     * See Press, Flannery, Teukolsky, Vetterling  _Numerical Recipes in *_ 10.1
     * for general guidelines, but not neccesarily implementation.
     * @returns the optimal input variable
     */
    public double maximize() {
        x1 = 0.0;
        x2 = sectSmall;
        x3 = sectLarge;
        x4 = 1.0;
        double gap = 1.0;
        //For exploring number of iterations.
        //int iter = 0;
        while (gap > resolution) {
                f2 = solveFor(x2);
                f3 = solveFor(x3);
                if (f2 < f3) {
                        gap1 = x3 - x2;
                        gap2 = x4 - x3;
                                x1 = x2;
                        if (gap1 > gap2) {
                                x2 = x3 - gap1 * sectSmall;
                                f2 = solveFor(x2);
                                gap = gap1;
                        }
                        else {  //gap[1] <= gap1
                                x2 = x3;
                                x3 = x2 + gap2 * sectSmall;
                                f2 = f3;
                                f3 = solveFor(x2);
                                gap = gap2;
                        }
                }
                else {  //y[3] <= y[0]
                        gap1 = x2 - x1;
                        gap2 = x3 - x2;
                                x4 = x3;
                        if (gap1 > gap2) {
                                x3 = x2;
                                x2 = x3 - gap1 * sectSmall;
                                f2 = solveFor(x2);
                                gap = gap1;
                        }
                        else {  //gap[1] <= gap1
                                x3 = x2 + gap2 * sectSmall;
                                f2 = f3;
                                f3 = solveFor(x2);
                                gap = gap2;
                        }
                }
            }
                return f2 > f3 ? x2 : x3;
    }
    
    /**
     * Solve this (single-variable) function. Override to define your own 
function.
     * @param x the variable input parameter
     * @returns the output value
     */
    public double solveFor(double x) {
        return x;
    }
}


Miles T. Parker                 mailto:address@hidden 
Research Software Engineer                              202.797.6136
The Brookings Institution                     fax 202.797.6319
1775 Massachusettes Avenue, N.W., Washington, D.C. 20036


>>> Jayshree Sarma <address@hidden> 08/19 6:19 PM >>>

Hi,

I need some help/ideas.  Here is a description of what I am trying to do:

There are 10 consumers and 15 producers and one product that is being
traded.  Each consumer has an equation (the equation is different for
each consumer) to calculate the demand for the product, and the
producers have another set of equations to calculate their intended
supply of the product for each time period.

The problem I have is that equations change and the terms in the
equation can either increase or decrease.  Hard coding the equation in
the program means that I have to change the code every time I run the
simulation.  I would like to define equation as an object and have
equations for each consumer/producer read in from a file at the start
of the simulation.

Has anyone in the hive community done something like this?  

Any thoughts/code would be greatly appreciated.  I am using swarm
1.0.5.

Thanks,

Jayshree



                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================

                  =================================   Swarm-Support is for 
discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ================================

reply via email to

[Prev in Thread] Current Thread [Next in Thread]