00001 /************************************************************************ 00002 * Verve * 00003 * Copyright (C) 2004-2006 * 00004 * Tyler Streeter tylerstreeter@gmail.com * 00005 * All rights reserved. * 00006 * Web: http://verve-agents.sourceforge.net * 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of EITHER: * 00010 * (1) The GNU Lesser General Public License as published by the Free * 00011 * Software Foundation; either version 2.1 of the License, or (at * 00012 * your option) any later version. The text of the GNU Lesser * 00013 * General Public License is included with this library in the * 00014 * file license-LGPL.txt. * 00015 * (2) The BSD-style license that is included with this library in * 00016 * the file license-BSD.txt. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * 00021 * license-LGPL.txt and license-BSD.txt for more details. * 00022 ************************************************************************/ 00023 00024 #include "TDConnection.h" 00025 #include "Neuron.h" 00026 00027 namespace verve 00028 { 00029 TDConnection::TDConnection(Neuron* preNeuron, Neuron* postNeuron, 00030 TDConnectionType type) 00031 : Connection(preNeuron, postNeuron) 00032 { 00033 //mType = TD; 00034 mETrace = 0; 00035 mTDConnectionType = type; 00036 mETraceDecayFactor = 0; 00037 mTDDiscountFactor = 0; 00038 mIsInActiveList = false; 00039 } 00040 00041 TDConnection::~TDConnection() 00042 { 00043 } 00044 00045 void TDConnection::resetShortTermMemory() 00046 { 00047 Connection::resetShortTermMemory(); 00048 mETrace = 0; 00049 mIsInActiveList = false; 00050 } 00051 00052 void TDConnection::setETrace(real trace) 00053 { 00054 mETrace = trace; 00055 } 00056 00057 real TDConnection::getETrace()const 00058 { 00059 return mETrace; 00060 } 00061 00062 TDConnectionType TDConnection::getTDConnectionType() 00063 { 00064 return mTDConnectionType; 00065 } 00066 00067 void TDConnection::setETraceDecayFactor(real value) 00068 { 00069 mETraceDecayFactor = value; 00070 } 00071 00072 void TDConnection::setTDDiscountFactor(real value) 00073 { 00074 mTDDiscountFactor = value; 00075 } 00076 00077 void TDConnection::increaseETrace() 00078 { 00079 // Here we use different methods to update different types of 00080 // TDConnections' eligibility traces. The authors use different 00081 // eligibility trace update methods for the value function and 00082 // policy in the following paper: 00083 // Barto, Sutton, and Anderson, "Neuronlike Adaptive Elements That 00084 // Can Solve Difficult Learning Control Problems," IEEE Trans. 00085 // Syst., Man, Cybern., Vol. SMC-13, pp. 834--846, Sept.--Oct. 1983. 00086 00087 real eligibility = 0; 00088 switch(mTDConnectionType) 00089 { 00090 case VALUE_FUNCTION_TDCONNECTION: 00092 eligibility = mPreNeuron->getFiringRate(); 00093 break; 00094 case POLICY_TDCONNECTION: 00096 eligibility = mPreNeuron->getFiringRate() * 00097 mPostNeuron->getFiringRate(); 00098 break; 00099 default: 00100 assert(false); 00101 break; 00102 } 00103 00104 if (eligibility >= mETrace) 00105 { 00106 mETrace = eligibility; 00107 } 00108 00109 // The following method increases or decreases the trace towards 00110 // the current eligibility. 00111 //mETrace = mETraceDecayFactor * mETrace + 00112 // (1 - mETraceDecayFactor) * eligibility; 00113 } 00114 00115 void TDConnection::decayETrace() 00116 { 00117 // This decreases the trace towards zero. 00118 mETrace = mTDDiscountFactor * mETraceDecayFactor * mETrace; 00119 } 00120 00121 void TDConnection::applyTDLearning(real learningFactorTimesTDError) 00122 { 00123 addToWeight(learningFactorTimesTDError * mETrace); 00124 } 00125 00126 void TDConnection::setIsInActiveList(bool inList) 00127 { 00128 mIsInActiveList = inList; 00129 } 00130 00131 bool TDConnection::isInActiveList() 00132 { 00133 return mIsInActiveList; 00134 } 00135 }