Skip to content

Instantly share code, notes, and snippets.

@delor
Created April 5, 2010 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delor/356502 to your computer and use it in GitHub Desktop.
Save delor/356502 to your computer and use it in GitHub Desktop.
#include <QtGui/QApplication>
#include <QFile>
#include <QTextStream>
#include <QChar>
#include "mainwindow.h"
#include "neuron.h"
#include "outputstream.h"
QTextStream qout(stdout);
Neuron* createNeuron(QVector<int>& weights)
{
QFile file("neuron.cfg");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 0;
Neuron* neuron = new Neuron();
int i;
QTextStream in(&file);
if (!in.atEnd()) {
in >> i;
qout << i << endl;
neuron->setThreshold(i);
}
while (!in.atEnd()) {
in >> i;
qout << i << endl;
weights.append(i);
}
return neuron;
}
QVector<QBitArray>* readInputFile()
{
QVector<QBitArray>* input = new QVector<QBitArray>;
QFile file("neuron.in");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return input;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QBitArray bits(line.size());
for (int i = 0; i < line.size(); ++i) {
qout << line.at(i);
bits[i] = (line.at(i) == '0') ? false : true;
}
qout << endl;
input->append(bits);
}
return input;
}
int decideLongest(QVector<QBitArray>* input)
{
int longest = 0;
foreach (QBitArray array, *input) {
if (array.size() > longest)
longest = array.size();
}
return longest;
}
void readFiles()
{
QVector<int> weights;
Neuron* neuron = createNeuron(weights);
QVector<QBitArray>* input = readInputFile();
OutputStream* os = new OutputStream;
QObject::connect(neuron, SIGNAL(output(int)), os, SLOT(addData(int)));
int longest = decideLongest(input);
for (int i = 0; i < input->size(); ++i) {
for (int j = 0; j < longest; ++j) {
if (j < input->at(i).size())
neuron->input((input->at(i)[j]) ? 1 : 0 * weights[i]);
else
neuron->input(0);
}
}
qout << "wynik:" << os->getString();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
readFiles();
a.exit();
return 0;
MainWindow w;
#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
w.showMaximized();
#else
w.show();
#endif
return a.exec();
}
#include "neuron.h"
#include "QDebug"
Neuron::Neuron(QObject* parent)
: QObject(parent),
threshold(0),
inputAmount(0),
current(0)
{
}
void Neuron::addInput(Neuron* inputNeuron)
{
connect(inputNeuron, SIGNAL(output(int)), this, SLOT(input(int)));
inputAmount++;
}
void Neuron::input(int i)
{
qDebug() << "Neuron::input" << i;
state += i;
current++;
if (current == inputAmount) {
if (state >= threshold) {
emit output(1);
} else {
emit output(0);
}
current = 0;
}
}
#ifndef NEURON_H
#define NEURON_H
#include <QVector>
#include <QBitArray>
#include <QPair>
class Neuron : public QObject
{
Q_OBJECT
public:
Neuron(QObject* parent = 0);
void addInput(Neuron* inputNeuron);
void removeInput(Neuron* inputNeuron);
inline void setThreshold(int arg);
inline int getThreshold() const;
protected:
int threshold; /// próg pobudzenia
int state; /// aktóalny stan wzbódzenia
int inputAmount; /// ilość wejść
int current; /// numer aktualnie obliczanego wejścia
QVector<int> weights; /// wektor wag poszczególnych wejść
public slots:
void input(int i);
signals:
void output(int i);
};
void Neuron::setThreshold(int arg) { threshold = arg; }
int Neuron::getThreshold() const { return threshold; }
#endif // NEURON_H
#include "outputstream.h"
#include <QDebug>
OutputStream::OutputStream(QObject *parent) :
QObject(parent)
{
}
QString OutputStream::getString()
{
return string;
}
void OutputStream::addData(int arg)
{
qDebug() << "OutputStream::addData" << arg;
string.append(arg);
}
#ifndef OUTPUTSTREAM_H
#define OUTPUTSTREAM_H
#include <QObject>
#include <QTextStream>
class OutputStream : public QObject
{
Q_OBJECT
public:
explicit OutputStream(QObject *parent = 0);
QString getString();
public slots:
void addData(int arg);
private:
QString string;
};
#endif // OUTPUTSTREAM_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment