PLearn 0.1
NetworkConnection.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // NetworkConnection.cc
00004 //
00005 // Copyright (C) 2007 Olivier Delalleau
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Olivier Delalleau
00036 
00040 #include "NetworkConnection.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00045 PLEARN_IMPLEMENT_OBJECT(
00046     NetworkConnection,
00047     "A connexion between modules in a LearningNetwork.",
00048     "A connexion indicates links between ports between two modules\n"
00049     "inheriting from OnlineLearningModule.\n"
00050 );
00051 
00053 // NetworkConnection //
00055 NetworkConnection::NetworkConnection():
00056     propagate_gradient(true)
00057 {}
00058 
00060 // NetworkConnection //
00062 NetworkConnection::NetworkConnection(const string& the_source,
00063                                      const string& the_destination,
00064                                      bool the_propagate_gradient,
00065                                      bool call_build_):
00066     inherited(call_build_),
00067     source(the_source),
00068     destination(the_destination),
00069     propagate_gradient(the_propagate_gradient)
00070 {
00071     if (call_build_)
00072         build_();
00073 }
00074 
00075 NetworkConnection::NetworkConnection(
00076                       PP<OnlineLearningModule> the_src_module,
00077                       const string& the_src_port,
00078                       PP<OnlineLearningModule> the_dst_module,
00079                       const string& the_dst_port,
00080                       bool the_propagate_gradient, bool call_build_):
00081     inherited(call_build_),
00082     propagate_gradient(the_propagate_gradient),
00083     src_module(the_src_module),
00084     src_port(the_src_port),
00085     dst_module(the_dst_module),
00086     dst_port(the_dst_port)
00087 {
00088     PLASSERT( the_src_module && the_dst_module );
00089     source = the_src_module->name + "." + the_src_port;
00090     destination = the_dst_module->name + "." + the_dst_port;
00091     if (call_build_)
00092         build_();
00093 }
00094 
00096 // build //
00098 void NetworkConnection::build()
00099 {
00100     inherited::build();
00101     build_();
00102 }
00103 
00105 // initialize //
00107 void NetworkConnection::initialize(map<string, PP<OnlineLearningModule> >& modules)
00108 {
00109     TVec<string> specs;
00110     specs.append(source);
00111     specs.append(destination);
00112     for (int i = 0; i < specs.length(); i++) {
00113         const string& spec = specs[i];
00114         size_t dot_pos = spec.find('.');
00115         if (dot_pos == string::npos)
00116             PLERROR("In NetworkConnection::initialize - Could not find a dot "
00117                     "in the port specification '%s'", spec.c_str());
00118         string module_name = spec.substr(0, dot_pos);
00119         if (modules.find(module_name) == modules.end())
00120             PLERROR("In NetworkConnection::initialize - Could not find a "
00121                     "module named '%s'", module_name.c_str());
00122         PP<OnlineLearningModule> module = modules[module_name];
00123         string port = spec.substr(dot_pos + 1);
00124         if (i == 0) {
00125             src_module = module;
00126             src_port = port;
00127         } else {
00128             PLASSERT( i == 1 );
00129             dst_module = module;
00130             dst_port = port;
00131         }
00132     }
00133 
00134 }
00135 
00137 // makeDeepCopyFromShallowCopy //
00139 void NetworkConnection::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00140 {
00141     inherited::makeDeepCopyFromShallowCopy(copies);
00142 
00143     deepCopyField(src_module, copies);
00144     deepCopyField(dst_module, copies);
00145 }
00146 
00148 // declareOptions //
00150 void NetworkConnection::declareOptions(OptionList& ol)
00151 {
00152     declareOption(ol, "source", &NetworkConnection::source,
00153                   OptionBase::buildoption,
00154         "Source of the connection (of the form 'module.port').");
00155 
00156     declareOption(ol, "destination", &NetworkConnection::destination,
00157                   OptionBase::buildoption,
00158         "Destination of the connection (of the form 'module.port').");
00159 
00160     declareOption(ol, "propagate_gradient",
00161                   &NetworkConnection::propagate_gradient,
00162                   OptionBase::buildoption,
00163         "Whether or not the destination should propagate its gradient to the\n"
00164         "source.");
00165 
00166     // Now call the parent class' declareOptions
00167     inherited::declareOptions(ol);
00168 }
00169 
00171 // build_ //
00173 void NetworkConnection::build_()
00174 {
00175 }
00176 
00178 // getDestinationModule //
00180 PP<OnlineLearningModule> NetworkConnection::getDestinationModule()
00181 {
00182     PLASSERT_MSG( dst_module, "getDestinationModule() cannot be called before "
00183             "the connection is initialized");
00184     return dst_module;
00185 }
00186 
00188 // getDestinationPort //
00190 const string& NetworkConnection::getDestinationPort()
00191 {
00192     PLASSERT_MSG( !dst_port.empty(), "getDestinationPort() cannot be called "
00193             "before the connection is initialized");
00194     return dst_port;
00195 }
00196 
00198 // getSourceModule //
00200 PP<OnlineLearningModule> NetworkConnection::getSourceModule()
00201 {
00202     PLASSERT_MSG( src_module, "getSourceModule() cannot be called before the "
00203             "connection is initialized");
00204     return src_module;
00205 }
00206 
00208 // getSourcePort //
00210 const string& NetworkConnection::getSourcePort()
00211 {
00212     PLASSERT_MSG( !src_port.empty(), "getSourcePort() cannot be called before "
00213             "the connection is initialized");
00214     return src_port;
00215 }
00216 
00217 } // end of namespace PLearn
00218 
00219 
00220 /*
00221   Local Variables:
00222   mode:c++
00223   c-basic-offset:4
00224   c-file-style:"stroustrup"
00225   c-file-offsets:((innamespace . 0)(inline-open . 0))
00226   indent-tabs-mode:nil
00227   fill-column:79
00228   End:
00229 */
00230 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines