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 "ActiveTDConnectionList.h" 00025 #include "TDConnection.h" 00026 00027 namespace verve 00028 { 00029 ActiveTDConnectionList::ActiveTDConnectionList() 00030 { 00031 } 00032 00033 ActiveTDConnectionList::~ActiveTDConnectionList() 00034 { 00035 mConnections.clear(); 00036 } 00037 00038 void ActiveTDConnectionList::clearList() 00039 { 00040 while (!mConnections.empty()) 00041 { 00042 mConnections.back()->setIsInActiveList(false); 00043 mConnections.pop_back(); 00044 } 00045 } 00046 00047 void ActiveTDConnectionList::addNewActiveConnection(TDConnection* c) 00048 { 00049 if (c->isInActiveList()) 00050 { 00051 return; 00052 } 00053 00054 // Insert the new element at the end of the list (because new 00055 // TDConnections will probably have high eligibility traces, so 00056 // this should help with sorting later). The function 00057 // std::list::insert(iterator pos, const T& x) inserts x before 00058 // pos in constant time. 00059 mConnections.insert(mConnections.end(), c); 00060 00061 c->setIsInActiveList(true); 00062 } 00063 00064 void ActiveTDConnectionList::increaseETraces() 00065 { 00066 std::list<TDConnection*>::iterator iter; 00067 for (iter = mConnections.begin(); iter != mConnections.end(); ++iter) 00068 { 00069 (*iter)->increaseETrace(); 00070 } 00071 } 00072 00073 void ActiveTDConnectionList::decayETraces() 00074 { 00075 // After we decay each TDConnection's eligibility trace, we need 00076 // to remove any TDConnection whose eligibility trace has fallen 00077 // below the active level. 00078 00079 std::list<TDConnection*>::iterator iter; 00080 for (iter = mConnections.begin(); iter != mConnections.end(); ) 00081 { 00082 (*iter)->decayETrace(); 00083 00084 // If the TDConnection is no longer active, remove it. 00085 if ((*iter)->getETrace() < defaults::activeETraceThreshold) 00086 { 00087 // Let the TDConnection know that it is no longer in 00088 // the active list. 00089 (*iter)->setIsInActiveList(false); 00090 00091 // Reset the inactive TDConnection's eligibility 00092 // trace since it is probably not exactly zero. 00093 (*iter)->setETrace(0); 00094 00095 // This returns an iterator to the element after the 00096 // one erased. This operates in constant time. 00097 iter = mConnections.erase(iter); 00098 } 00099 else 00100 { 00101 // The TDConnection must still be active. 00102 ++iter; 00103 } 00104 } 00105 } 00106 00107 void ActiveTDConnectionList::trainConnections( 00108 real learningFactorTimesTDError) 00109 { 00110 std::list<TDConnection*>::iterator iter; 00111 for (iter = mConnections.begin(); iter != mConnections.end(); ++iter) 00112 { 00113 (*iter)->applyTDLearning(learningFactorTimesTDError); 00114 } 00115 } 00116 }