Dear Ptolemy Hackers,

I'm trying to use Ptolemy to build ANNs, the class (together with test
main) which you find included should simply result in the output
converging to zero. It converges to -0.5 instead, why? Is it a problem
with the director? Which other embedded director can I use? What am I
missing?

To run the main:

javac Node.java
java Node

click on <go>

Thank you,
    Alessandro

 
      _/  _/    _/  _/ Alessandro Usseglio Viretta     / Tel +41 1
6353066
     _/  _/_/  _/  _/  Institute of Neuroinformatics  / Fax +41 1
6353053
    _/  _/ _/ _/  _/   University/ETH Zurich         / 
   _/  _/  _/_/  _/    Winterthurerstr. 190         /  
  _/  _/    _/  _/     CH-8057 Zurich              /
www.ini.unizh.ch/~ale
/*
 * Node.java
 *
 * Created on August 14, 2000, 1:46 PM
 */



/**
 *
 * @author  ale
 * @version
 */

import ptolemy.kernel.util.*;
import ptolemy.graph.*;
import ptolemy.data.*;
import ptolemy.data.type.*;
import ptolemy.data.expr.*;
import ptolemy.actor.*;
import ptolemy.actor.gui.*;
import ptolemy.actor.lib.*;
import ptolemy.domains.ct.kernel.*;
import ptolemy.domains.ct.lib.*;

import javax.swing.*;

public class Node extends TypedCompositeActor implements Constants {

    /** Create new Node */
    public Node(TypedCompositeActor container, String name, int timeConstantMultiplier)
    throws IllegalActionException, NameDuplicationException {
        super(container, name);
        sumPort = (TypedIOPort)newPort("sumPort");
        sumPort.setInput(true);
        sumPort.setMultiport(true);
        productPort = (TypedIOPort)newPort("productPort");
        productPort.setInput(true);
        productPort.setMultiport(true);
        outputPort = (TypedIOPort)newPort("outputPort");
        outputPort.setOutput(true);
        outputPort.setMultiport(false);
        inputPort = (TypedIOPort)newPort("inputPort");
        inputPort.setInput(true);
        inputPort.setMultiport(false);
        tau = new Parameter(this, "tau", new DoubleToken(TAU * 
timeConstantMultiplier));
        director = new CTEmbeddedDirector(this, "director");

        AddSubtract as1 = new AddSubtract(this, "as1");
        connect(sumPort, as1.plus);
        MultiplyDivide md1 = new MultiplyDivide(this, "md1");
        connect(productPort, md1.multiply);
        connect(md1.output, as1.plus);
        MultiplyDivide md2 = new MultiplyDivide(this, "md2");
        Const c1 = new Const(this, "c1");
        c1.value.setToken(tau.getToken());
        connect(c1.output, md2.divide);
        connect(as1.output, md2.multiply);
        Integrator i1 = new Integrator(this, "i1");
        i1.InitialState.setToken(new DoubleToken(1));
        connect(md2.output, i1.input);
        MultiplyDivide md3 = new MultiplyDivide(this, "md3");
        IORelation r1 = (IORelation)connect(i1.output, as1.minus);
        connect(inputPort, md3.multiply);
        md3.multiply.link(r1);
        connect(md3.output, outputPort);
    }

    public Node(TypedCompositeActor container, String name)
    throws IllegalActionException, NameDuplicationException {
        this(container, name, 1);
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    /** Input for tokens to be summed in the left-hand side of the
     *  differential equation of the node.  This is a multiport, and its
     *  type is inferred from the connections.
     */
    public TypedIOPort sumPort;

    /** Input for tokens to be multiplied and then summed in the
     *  left-hand side of the differential equation of the node.
     *  This is a multiport, and its type is inferred from the
     *  connections.
     */
    public TypedIOPort productPort;

    /** Output port.  The type is inferred from the connections.
     */
    public TypedIOPort outputPort;

    /** Input port.  The type is inferred from the connections.
     */
    public TypedIOPort inputPort;

    /** Time constant
     */
    public Parameter tau;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    /** Clone the actor into the specified workspace. This calls the
     *  base class and sets the public variables to point to the new ports.
     *  @param ws The workspace for the new object.
     *  @return A new actor.
     *  @throws CloneNotSupportedException If a derived class contains
     *   an attribute that cannot be cloned.
     */
    public Object clone(Workspace ws)
    throws CloneNotSupportedException {
        try {
            Node newobj = (Node)super.clone(ws);
            newobj.sumPort = (TypedIOPort)newobj.getPort("sumPort");
            newobj.productPort = (TypedIOPort)newobj.getPort("productPort");
            newobj.outputPort = (TypedIOPort)newobj.getPort("output");
            newobj.inputPort = (TypedIOPort)newobj.getPort("input");
            return newobj;
        } catch (CloneNotSupportedException ex) {
            // Errors should not occur here...
            throw new InternalErrorException(
            "Clone failed: " + ex.getMessage());
        }
    }

    public void fire() throws IllegalActionException {
        director.transferInputs(sumPort);
        director.transferInputs(inputPort);
        director.transferInputs(productPort);
        director.fire();
        this.getExecutiveDirector().transferOutputs(outputPort);

        // debugging //
        //System.out.println(director.getCurrentStepSize());
    }

    public static void main(String[] args)
    throws IllegalActionException, NameDuplicationException {
        class Test extends JFrame {
            public Test()
            throws IllegalActionException, NameDuplicationException {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                ///// Workspace /////
                Workspace w = new Workspace("w");
                ///// Top-Level Actor /////
                TypedCompositeActor toplevel = new TypedCompositeActor(w);
                toplevel.setName("toplevel");
                ///// Director /////
                CTMultiSolverDirector director = new CTMultiSolverDirector(toplevel, 
"director");
                director.StopTime.setToken(new DoubleToken(10.0));
                director.STAT = true;
                ///// Manager /////
                Manager manager = new Manager(w, "manager");
                toplevel.setManager(manager);
                ////////////////////
                Node n1 = new Node(toplevel, "n1");
                Const c1 = new Const(toplevel, "c1");
                c1.value.setToken(new DoubleToken(0.0));
                Const c2 = new Const(toplevel, "c2");
                c2.value.setToken(new DoubleToken(0.0));
                Const c3 = new Const(toplevel, "c3");
                c3.value.setToken(new DoubleToken(1.0));
                IORelation r1 = (IORelation)toplevel.connect(c1.output, n1.sumPort);
                IORelation r2 = (IORelation)toplevel.connect(c2.output, 
n1.productPort);
                IORelation r3 = (IORelation)toplevel.connect(c3.output, n1.inputPort);
                TimedPlotter tp1 = new TimedPlotter(toplevel, "Plotter");
                toplevel.connect(n1.outputPort, tp1.input);
                ModelPane mp = new ModelPane(toplevel);
                getContentPane().add(mp);
                pack();
                show();
            }
        }
        Test t1 = new Test();
    }


    ///////////////////////////////////////////////////////////////////
    ////                       private variables                   ////

    /** Recorder used to store internal state
     */
    //private SingleTokenBuffer stb1;

    /** Director of this container
     */
    private CTEmbeddedDirector director;

}

Reply via email to