Skip to content

Commit

Permalink
feature-#1: Implement AND net.
Browse files Browse the repository at this point in the history
* Began implementation.
  • Loading branch information
w-martin committed Apr 22, 2012
1 parent 6c4403f commit 8dd9611
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ add_executable(NetTests
${NNSIM_TEST_DIR}/nets/BDN/BDNTest.cpp
${NNSIM_TEST_DIR}/nets/BDN/BDNTrainerTest.cpp
${NNSIM_TEST_DIR}/nets/BDN/LogicalArchitectureTest.cpp
${NNSIM_TEST_DIR}/nets/BDN/realisations/ANDnetTest.cpp
${NNSIM_TEST_DIR}/nets/BDN/realisations/ORnetTest.cpp)
target_link_libraries(NetTests gmock gmock_main ${NNSIM_LIB})
add_test(NetTests NetTests)
Expand Down
6 changes: 4 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Things I need to do!
# Updated 16th July 2011
# Updated 24th July 2011

# Improvements

- For ease, make Net extend Neuron

# Tests to write


# New features

- More logical realisations(and, xor, nor, xnor)
- More logical realisations(xor, nor, xnor)
- Hopfield net
- MLP
- Kohonen net
32 changes: 32 additions & 0 deletions include/nn-simulator/main/nets/BDN/realisations/ANDnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* File: ANDnet.h
* Author: will
*
* Created on July 24, 2011, 9:15 AM
*/

#ifndef ANDNET_H
#define ANDNET_H

#define NET_TYPE_ANDNET "ANDNET"

#include "nn-simulator/main/nets/BDN/LogicalArchitecture.h"
#include "nn-simulator/main/model/Net.h"
#include "nn-simulator/main/model/Input.h"

class ANDnet : public Net {
public:
ANDnet(int const noInputs);
virtual ~ANDnet();
protected:
virtual void createInputVectors();
virtual void createLayers();
virtual void createNeurons();
private:
int noInputs;
Input *expectedInput;
Input *necessaryInput;
};

#endif /* ANDNET_H */

51 changes: 51 additions & 0 deletions src/nets/BDN/realisations/ANDnet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* File: ANDnet.cpp
* Author: will
*
* Created on July 24, 2011, 9:15 AM
*/

#include "nn-simulator/main/nets/BDN/realisations/ANDnet.h"
#include "nn-simulator/main/nets/BDN/BDNTrainer.h"
#include "nn-simulator/main/nets/BDN/BDN.h"
#include "nn-simulator/main/nets/NeuronFactory.h"

using namespace std;

ANDnet::ANDnet(int const noInputs) :
Net(auto_ptr<Architecture>(new LogicalArchitecture()), 1, noInputs,
NET_TYPE_ANDNET) {
ANDnet::noInputs = noInputs;
createInputVectors();
createLayers();
createNeurons();
}

ANDnet::~ANDnet() {
}

void ANDnet::createInputVectors() {
expectedInput = new Input(noInputs);
necessaryInput = new Input(noInputs);
for (int i = 0; i < getNoInputs(); i++) {
expectedInput->setValue(i, 1.0f);
necessaryInput->setValue(i, 1.0f);
}
}

void ANDnet::createLayers() {
Layer *l = new Layer(1, false);
setLayer(0, auto_ptr<Layer > (l));
}

void ANDnet::createNeurons() {
auto_ptr<BDN> n = Factory<BDN, Architecture>::
createInstance((*getArchitecture()), getNoInputs());
n->setExpectedInput(auto_ptr<Input > (expectedInput));
n->setNecessaryInput(auto_ptr<Input > (necessaryInput));
BDNTrainer *trainer = (BDNTrainer*) getArchitecture()->getTrainer();
trainer->initWeights(n.get());
trainer->setThreshold(n.get());
auto_ptr<Neuron> neuronPointer = auto_ptr<Neuron > (n);
getLayer(0)->setNeuron(0, neuronPointer);
}
64 changes: 64 additions & 0 deletions test/nets/BDN/realisations/ANDnetTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* File: ANDnetTest.cpp
* Author: will
*
* Created on July 24, 2011, 9:25 AM
*/

#include "gtest/gtest.h"
#include "nn-simulator/main/nets/BDN/realisations/ANDnet.h"
#include "nn-simulator/main/model/Input.h"
#include "nn-simulator/main/nets/BDN/LogicalArchitecture.h"
#include "nn-simulator/main/nets/BDN/BDN.h"

namespace {

class ANDnetTest : public ::testing::Test {
protected:

ANDnetTest() {
noInputs = 1;
net = new ANDnet(noInputs);
}

virtual ~ANDnetTest() {
delete net;
}

int noInputs;
ANDnet *net;
};

/*
* Tests whether the parent constructor is called
* correctly.
*
*/
TEST_F(ANDnetTest, ConstructorTest) {
EXPECT_EQ(noInputs, net->getNoInputs());
EXPECT_EQ(1, net->getNoLayers());
EXPECT_EQ(0, strcmp(NET_TYPE_ANDNET, net->getNetType()));
LogicalArchitecture *la = (LogicalArchitecture*)
net->getArchitecture();
Layer *l = net->getLayer(0);
BDN *bdn = (BDN*) l->getNeuron(0);
}

/*
* Tests whether the function works correctly.
*
*/
TEST_F(ANDnetTest, FunctionTest) {
Input trueInput = Input(noInputs);
for (int i = 0; i < noInputs; i++) {
trueInput.setValue(i, 1.0f);
}
EXPECT_EQ(1.0f, net->processInput(trueInput).getValue());

Input falseInput = Input(noInputs);
for (int i = 0; i < noInputs; i++) {
falseInput.setValue(i, (i == 0) ? 1.0f : 0.0f);
}
EXPECT_EQ(0.0f, net->processInput(falseInput).getValue());
}
}

0 comments on commit 8dd9611

Please sign in to comment.