//*******************************************************************************
// CSC 143 Computer Programming II Spring 1999 Instructor: Keith Hughes
//
// Homework 3
// File : \\Venus\katy\CIS143\Homework3\AdalineNetwork\base.h
//
// Purpose : Header file for Neuron and Link classes
//
// Author : Hsin-yi F. Berg
// Date : 5/9/1999 Sun.
// Last Update: 5/28/1999 Fri.
// Update Note: Error has been moved form Link class to Neuron class
//                Replace the dynamically allocated arrays of pointers to Links in
//                the Neuron class with singly-linked lists of pointers to Links
//*******************************************************************************

#ifndef BASE_H
#define BASE_H

#include <iostream.h>
#include <fstream.h>

typedef int ID;

// the weight of the link
const double WEIGHT = 0.5;

struct LinkNode;
class Link;

class Neuron
{
public:
    /*
        Neuron::Neuron()

        Default constructor for an Neuron. Not connected to any link.
    */
    Neuron();

    /*
        Neuron::~Neuron()

        Destructor for the Neuron.
    */
    ~Neuron();

    /*
        void Neuron::Print(ostream &out) const

        Print the internal state of the Neuron to the client_supplied ostream.
    */
    void Print(ostream &out) const;

    /*
        bool Neuron::InputRoom(void) const

        Returns true if there is room for a new input link, false otherwise.
    */
    bool InputRoom(void) const;

    /*
        void Neuron::AddInput(Link &newLink)

        Add an input Link to the Neuron.
        There should be room in the Neuron for another input Link.
    */
    void AddInput(Link &newLink);

    /*
        bool Neuron::OutputRoom(void) const

        Returns true if there is room for a new output link, false otherwise.
    */
    bool OutputRoom(void) const;

    /*
        void Neuron::AddOutput(Link &newLink)

        Add an output Link to the Neuron.
        There should be room in the Neuron for another output Link.
    */
    void AddOutput(Link &newLink);

    /*
        LinkNode *&Neuron::GetInputLink(void)

        Return the reference of the collection of input links
    */
    LinkNode *&Neuron::GetInputLink(void);

    /*
        LinkNode *&Neuron::GetOutputLink(void)

        Return the reference of the collection of output links
    */
    LinkNode *&Neuron::GetOutputLink(void);

    /*
        ID Neuron::GetID(void) const

        Return the ID of this neuron
    */
    ID GetID(void) const;

    /*
        void Neuron::SetValue(double newValue)

        Set the value of the Neuron to some new value.
    */
    void SetValue(double newValue);

    /*
        void Neuron::GetValue(void) const

        Return the value of the neuron.
    */
    double GetValue(void) const;

    /*
        void Neuron::SetError(double newError)

        Set the error of the Neuron to new error value.
    */
    void Neuron::SetError(double newError);

    /*
        void Neuron::GetError(void) const

        Get the error of this Neuron.
    */
    double Neuron::GetError(void) const;

    /*
        void Neuron::Fire(void)

        Calculating the weighted sum from the input links, and applying
        the threshold function to the resulting sum and storing the
        chosen threshold output value as the neuron's value.
    */
    void Fire(void);

    /*
        void Neuron::ReadFromFile(istream &in)

        Read data into this Neuron instance from file specified by "in"
    */
    void Neuron::ReadFromFile(istream &in);
    
    /*
        void Neuron::WriteToFile(ostream &out)

        Write data of this Neuron instance to file specified by "out"
    */
    void Neuron::WriteToFile(ostream &out);

    /*
        void Neuron::Learn(double learning_rate)

        Learn will recalculate the weight of the input Link
        according to the learning rate, its own error and its input
        Neuron value using generalized delta rule
    */
    void Learn(double learning_rate);

private:
    // id_count keeps track of how many Neurons that have been created
    static int id_count;            
    // each Neuron automatically gets an unique id when it's created
    ID id;
    // input_link stores all the input Links that are connected to the Neuron
    LinkNode *input_link;
    // size_input_link keeps track of how many input Links we have right now
    int size_input_link;
    // output_link stores all the output Links that are connected to the Neuron
    LinkNode *output_link;
    // size_output_link keeps track of how many output Links we have right now
    int size_output_link;
    // threshold value of the Neuron
    double threshold;
    // high thresholded value of the Neuron, if(sum > threshold), value = high
    double high;
    // low thresholded value of the Neuron, if(sum <= threshold), value = low
    double low;
    // the current value of this Neuron
    double value;
    // the current error of this Neuron
    double error;
};

class Link
{
public:
    /*
        Link::Link()

        Default constructor for a Link. Does not point to any Neuron.
    */
    Link();

    /*
        Link::Link(const Neuron *Input, const Neuron *Output, double Weight)

        Constructor for a Link.
        Takes Input Neuron, OutputNeuron, and Weight.
    */
    Link(Neuron *Input,Neuron *Output, double Weight);

    /*
        Link::~Link()

        Destructor for a Link.
    */
    ~Link();

    /*
        void Link::Print(ostream &out) const

        Print the input Neuron, output Neuron, and the Weight on
        the client_supplied ostream.
    */
    void Print(ostream &out) const;

    /*
        ID Link::GetID(void) const

        Return the ID of this Link.
    */
    ID GetID(void) const;
    
    /*
        void Link::SetNeurons(Neuron &input_Neuron,Neuron &output_Neuron)

        Set the input and output Neurons of this Link.
    */
    void SetNeurons(Neuron &input_Neuron, Neuron &output_Neuron);

    /*
        Neuron & Link::GetInputNeuron(void) const

        Return the reference of the input Neruon of this Link.
    */
    Neuron &GetInputNeuron(void) const;

    /*
        Neuron & Link::GetOutputNeuron(void) const

        Return the reference of the output Neruon of this Link.
    */
    Neuron &GetOutputNeuron(void) const;

    /*
        void Link::SetWeight(double Weight)

        Set the weight of this link to the client-supplied weight.
    */
    void SetWeight(double Weight);

    /*
        void Link::GetWeight(void) const

        Get the current weight of this link
    */
    double GetWeight(void) const;

    /*
        double Link::GetWeightedInputValue() const

        Return the value of the input node times the weight of the link.
    */
    double GetWeightedInputValue() const;

    /*
        void Link::ReadFromFile(istream &in)

        Read weight into this Link instance from file specified by "in"
    */
    void Link::ReadFromFile(istream &in);
    
    /*
        void Link::WriteToFile(ostream &out)

        Write weight of this Link instance to file specified by "out"
    */
    void Link::WriteToFile(ostream &out);

private:
    // id_count keeps track of how many Links that have been created
    static int id_count;
    // each Link automatically gets an unique id when it's created
    ID id;
    // points to the input Neuron
    Neuron *input;
    // points to the output Neuron
    Neuron *output;
    // the weight of the Link
    double weight;
};

#endif
Back    Top