Permalink
Browse files

Added autoconnect

  • Loading branch information...
sofian committed Apr 23, 2012
1 parent d83bfa7 commit 099288119a5fe3ed091bfed867b2fbc301799a7b
@@ -22,19 +22,27 @@
#include <unistd.h>
#include <stdio.h>
-Prototype2Environment::Prototype2Environment(int observationDim_, int actionDim_, const char *namePrefix, int initialPort)
- : devNamePrefix(namePrefix), devInitialPort(initialPort), currentObservation(observationDim_), observationDim(observationDim_), actionDim(actionDim_) {
+Prototype2Environment::Prototype2Environment(int observationDim_, int actionDim_, const char *namePrefix, bool autoConnect_, int initialPort)
+ : devNamePrefix(namePrefix), devInitialPort(initialPort), autoConnect(autoConnect_), currentObservation(observationDim_), observationDim(observationDim_), actionDim(actionDim_) {
+}
+
+Prototype2Environment::~Prototype2Environment() {
+ autoDisconnectDevice();
+ if (dev)
+ mdev_free(dev);
}
void Prototype2Environment::init() {
- printf("Initializing\n");
dev = mdev_new(devNamePrefix, devInitialPort, 0);
mdev_add_input(dev, "/observation", observationDim + 1, 'f', 0, 0, 0,
(mapper_signal_handler*)updateInput, this);
// TODO: Actions range (min/max) should be known in advance somehow (this is a limitation of the current implemenation)
outsig = mdev_add_output(dev, "/action", actionDim, 'i', 0, 0, 0);
+
+ if (autoConnect)
+ autoConnectDevice(dev);
}
Observation* Prototype2Environment::start() {
@@ -44,18 +52,18 @@ Observation* Prototype2Environment::start() {
}
Observation* Prototype2Environment::step(const Action* action) {
-// printf("Stepping env\n");
-// printf("--> sending %d ...\n", action->actions[0]);
+ printf("Stepping env\n");
+ printf("--> sending %d ...\n", action->actions[0]);
msig_update(outsig, action->actions);
while (!mdev_poll(dev, 1) );
-// printf("--> receiving reward = %f, data = %f ...\n", currentObservation.reward, currentObservation[0]);
+ printf("--> receiving reward = %f, data = %f ...\n", currentObservation.reward, currentObservation[0]);
//usleep(100);
return &currentObservation;
}
void Prototype2Environment::updateInput(mapper_signal sig, mapper_db_signal props,
mapper_timetag_t *timetag, float *value) {
-// printf("update input called %f\n", *value);
+ printf("update input called %f\n", *value);
RLObservation& obs = ((Prototype2Environment*)props->user_data)->currentObservation;
int k;
for (k=0; k<obs.dim; k++)
@@ -22,18 +22,21 @@
#include "Environment.h"
#include "RLObservation.h"
+#include "AutoConnect.h"
#include <mapper/mapper.h>
class Prototype2Environment : public Environment {
public:
mapper_device dev;
const char* devNamePrefix;
+ bool autoConnect;
int devInitialPort;
mapper_signal outsig;
RLObservation currentObservation;
int observationDim, actionDim;
- Prototype2Environment(int observationDim, int actionDim, const char *namePrefix, int initialPort = 9000);
+ Prototype2Environment(int observationDim, int actionDim, const char *namePrefix, bool autoConnect = false, int initialPort = 9000);
+ virtual ~Prototype2Environment();
virtual void init();
virtual Observation* start();
@@ -52,8 +52,8 @@ const unsigned int N_ACTIONS[] = { 2 };
//unsigned char buffer[STATIC_ALLOCATOR_SIZE];
//StaticAllocator myAlloc(buffer, STATIC_ALLOCATOR_SIZE);
int main(int argc, char** argv) {
- if (argc > 6 || (argc > 1 && strcmp(argv[1], "-h") == 0)) {
- printf("Usage: %s [n_hidden=%d] [learning_rate=%f] [epsilon=%f] [lambda=%f] [gamma=%f] [dim_observations=%d]\n",
+ if (argc > 8 || (argc > 1 && strcmp(argv[1], "-h") == 0)) {
+ printf("Usage: %s [n_hidden=%d] [learning_rate=%f] [epsilon=%f] [lambda=%f] [gamma=%f] [dim_observations=%d] [autoconnect=0]\n",
argv[0], N_HIDDEN, LEARNING_RATE, EPSILON, LAMBDA, GAMMA, DIM_OBSERVATIONS);
exit(-1);
}
@@ -65,6 +65,7 @@ int main(int argc, char** argv) {
float lambda = (++arg < argc ? atof(argv[arg]) : LAMBDA);
float gamma = (++arg < argc ? atof(argv[arg]) : GAMMA);
int dimObservations = (++arg < argc ? atoi(argv[arg]) : DIM_OBSERVATIONS);
+ bool autoConnect = (++arg < argc ? atoi(argv[arg]) : false);
printf("N hidden: %d\n", nHidden);
printf("Learning rate: %f\n", learningRate);
@@ -78,7 +79,7 @@ int main(int argc, char** argv) {
NeuralNetwork net(dimObservations + DIM_ACTIONS, nHidden, 1, learningRate);
QLearningAgent agent(&net, dimObservations, DIM_ACTIONS, N_ACTIONS,
lambda, gamma, &egreedy, false); // lambda = 1.0 => no history
- Prototype2Environment env(dimObservations, DIM_ACTIONS, "prototype2", 9000);
+ Prototype2Environment env(dimObservations, DIM_ACTIONS, "prototype2", autoConnect, 9000);
RLQualia qualia(&agent, &env);
qualia.init();

0 comments on commit 0992881

Please sign in to comment.