//////////////////////////////////////////////////////////////////////////////
//
// File: nn_link.cc
//
// Purpose: Implementation for NeuNet's Link class.
//
// Authors:
//   txe  Travis Emmitt
//
// Modifications:
//   17-APR-1998  txe  Initial creation
//   18-APR-1998  txe  Added Train(), SetWeight()
//   19-APR-1998  txe  Cleaned up, added constructor and destructor
//   20-APR-1998  txe  Purifying...
//   23-APR-1998  txe  Using static debug, changed constructor
//
//////////////////////////////////////////////////////////////////////////////

#include "common.h"
#include "debug.h"
#include "nn_arch.h"
#include "nn_link.h"
#include "nn_node.h"

//////////////////////////////////////////////////////////////////////////////

Link::Link (char *name, Arch *arch, Node *source, Node *dest)
    : Debug (name) {

  ASSERT (source != NULL);
  ASSERT (dest   != NULL);

  this->arch         = arch;
  this->source       = source;
  this->dest         = dest;
  this->weight       = 0;
  this->adjust       = 0;
}

//////////////////////////////////////////////////////////////////////////////

Link::~Link () {
  DEBUG(DEL) << "Destroying " << name << "\n";
}

//////////////////////////////////////////////////////////////////////////////

void Link::SetWeight (float weight) {
  this->weight = weight;
}

//////////////////////////////////////////////////////////////////////////////

void Link::Train () {
  DEBUG(3) << "Training " << name << "...\n";

  adjust = (arch->learning_rate * dest->delta * source->level)
         + (arch->momentum      * adjust);

  weight += adjust;
  weight = MAX (arch->min_weight, MIN (arch->max_weight, weight));

  source->AddError (weight * dest->delta);
}

//////////////////////////////////////////////////////////////////////////////

