23#include <yarp/os/Vocab.h>
30namespace learningmachine {
38 TrainEvent te(sample.head, sample.body, prediction);
45 }
catch(
const std::exception&
e) {
46 std::cerr <<
"Error: " <<
e.what() << std::endl;
54void TrainModule::printOptions(std::string
error) {
56 std::cout <<
"Error: " <<
error << std::endl;
58 std::cout <<
"Available options for training module" << std::endl;
59 std::cout <<
"--help Display this help message" << std::endl;
60 std::cout <<
"--list Print a list of available algorithms" << std::endl;
61 std::cout <<
"--load file Load serialized machine from a file" << std::endl;
62 std::cout <<
"--machine type Desired type of learning machine" << std::endl;
63 std::cout <<
"--port pfx Prefix for registering the ports" << std::endl;
64 std::cout <<
"--commands file Load configuration commands from a file" << std::endl;
68void TrainModule::printMachineList() {
70 std::cout <<
"Available Machine Learners:" << std::endl;
71 for(
unsigned int i = 0; i < keys.size(); i++) {
72 std::cout <<
" " << keys[i] << std::endl;
77void TrainModule::registerAllPorts() {
86 this->train_in.setStrict();
89void TrainModule::unregisterAllPorts() {
91 this->train_in.close();
92 this->model_out.close();
109 yarp::os::Value* val;
110 std::string machineName;
116 if(opt.check(
"help")) {
117 this->printOptions();
122 if(opt.check(
"list")) {
123 this->printMachineList();
128 if(opt.check(
"port", val)) {
133 if(opt.check(
"load", val)) {
137 if(opt.check(
"machine", val)) {
138 machineName = val->asString().c_str();
140 this->printOptions(
"No machine type specified");
156 this->train_in.useCallback(trainProcessor);
159 this->registerAllPorts();
162 if(opt.check(
"commands", val)) {
168 this->attachTerminal();
177 bool success =
false;
180 switch(
cmd.get(0).asVocab32()) {
181 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
182 reply.add(yarp::os::Value::makeVocab32(
"help"));
184 reply.addString(
"Training module configuration options");
185 reply.addString(
" help Displays this message");
186 reply.addString(
" train Trains the machine and sends the model");
187 reply.addString(
" model Sends the model to the prediction module");
188 reply.addString(
" reset Resets the machine to its current state");
189 reply.addString(
" info Outputs information about the machine");
190 reply.addString(
" pause Disable passing the samples to the machine");
191 reply.addString(
" continue Enable passing the samples to the machine");
192 reply.addString(
" set key val Sets a configuration option for the machine");
193 reply.addString(
" load fname Loads a machine from a file");
194 reply.addString(
" save fname Saves the current machine to a file");
195 reply.addString(
" event [cmd ...] Sends commands to event dispatcher (see: event help)");
196 reply.addString(
" cmd fname Loads commands from a file");
197 reply.addString(this->
getMachine().getConfigHelp().c_str());
201 case yarp::os::createVocab32(
't',
'r',
'a',
'i'):
203 reply.addString(
"Training completed.");
205 case yarp::os::createVocab32(
'm',
'o',
'd',
'e'):
207 reply.addString(
"The model has been written to the port.");
211 case yarp::os::createVocab32(
'c',
'l',
'e',
'a'):
212 case yarp::os::createVocab32(
'c',
'l',
'r'):
213 case yarp::os::createVocab32(
'r',
'e',
's',
'e'):
214 case yarp::os::createVocab32(
'r',
's',
't'):
216 reply.addString(
"Machine cleared.");
220 case yarp::os::createVocab32(
'p',
'a',
'u',
's'):
221 case yarp::os::createVocab32(
'd',
'i',
's',
'a'):
223 reply.addString(
"Sample stream to machine disabled.");
227 case yarp::os::createVocab32(
'c',
'o',
'n',
't'):
228 case yarp::os::createVocab32(
'e',
'n',
'a',
'b'):
230 reply.addString(
"Sample stream to machine enabled.");
234 case yarp::os::createVocab32(
'i',
'n',
'f',
'o'):
235 case yarp::os::createVocab32(
's',
't',
'a',
't'):
237 reply.add(yarp::os::Value::makeVocab32(
"help"));
238 reply.addString(
"Machine Information: ");
239 reply.addString(this->
getMachine().getInfo().c_str());
244 case yarp::os::createVocab32(
'l',
'o',
'a',
'd'):
246 reply.add(yarp::os::Value::makeVocab32(
"help"));
247 std::string replymsg = std::string(
"Loading machine from '") +
248 cmd.get(1).asString().c_str() +
"'... " ;
249 if(!
cmd.get(1).isString()) {
250 replymsg +=
"failed";
253 replymsg +=
"succeeded";
255 reply.addString(replymsg.c_str());
260 case yarp::os::createVocab32(
's',
'a',
'v',
'e'):
262 reply.add(yarp::os::Value::makeVocab32(
"help"));
263 std::string replymsg = std::string(
"Saving machine to '") +
264 cmd.get(1).asString().c_str() +
"'... " ;
265 if(!
cmd.get(1).isString()) {
266 replymsg +=
"failed";
269 replymsg +=
"succeeded";
271 reply.addString(replymsg.c_str());
276 case yarp::os::createVocab32(
's',
'e',
't'):
278 yarp::os::Bottle property;
285 property.addList() =
cmd.tail();
286 std::string replymsg =
"Setting configuration option ";
288 replymsg +=
ok ?
"succeeded" :
289 "failed; please check key and value type.";
290 reply.addString(replymsg.c_str());
295 case yarp::os::createVocab32(
'e',
'v',
'e',
'n'):
301 case yarp::os::createVocab32(
'c',
'm',
'd'):
302 case yarp::os::createVocab32(
'c',
'o',
'm',
'm'):
304 reply.add(yarp::os::Value::makeVocab32(
"help"));
305 std::string replymsg;
306 if(!
cmd.get(1).isString()) {
307 replymsg =
"Please supply a valid filename.";
309 std::string full_fname = this->
findFile(cmd.get(1).asString().c_str());
310 replymsg = std::string(
"Loading commands from '") +
311 full_fname +
"'... " ;
313 replymsg +=
"succeeded";
315 reply.addString(replymsg.c_str());
324 }
catch(
const std::exception&
e) {
325 std::string msg = std::string(
"Error: ") +
e.what();
326 reply.addString(msg.c_str());
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
Respond to a command or configuration message.
virtual void raise(IEvent &event)
Raises an IEvent, causing it to be dispatched to each registered IEventListener.
static EventDispatcher & instance()
An instance retrieval method that follows the Singleton pattern.
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
DispatcherManager dmanager
An instance of the DispatchManager to configure event listeners.
std::string portPrefix
A prefix path for the ports that will be registered.
virtual void setResourceFinder(yarp::os::ResourceFinder *rf)
Mutator for the locally stored ResourceFinder.
std::string findFile(std::string fname)
Finds the full path to a specified filename using the ResourceFinder.
void registerPort(yarp::os::Contactable &port, std::string name)
Register a port with a given name.
yarp::os::Port cmd_in
An input port for commands.
virtual void loadCommandFile(std::string fname, yarp::os::Bottle *out=(yarp::os::Bottle *) 0)
Reads bottles from a file and sends these one by one to the respond method.
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
virtual void reset()=0
Forget everything and start over.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual Prediction predict(const yarp::sig::Vector &input)=0
Ask the learning machine to predict the output for a given input.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)=0
Provide the learning machine with an example of the desired mapping.
virtual MachinePortable & getMachinePortable()
Retrieve the machine portable machine wrapper.
virtual IMachineLearner & getMachine()
Convenience function to quickly retrieve the machine that is wrapped in the portable machine wrapper.
void setWrapped(T *w, bool wipe=true)
The mutator for the wrapped object.
bool writeToFile(std::string filename)
Writes a wrapped object to a file.
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
virtual bool interruptModule()
void unregisterAllPorts()
Unregisters all ports used by this module.
yarp::os::BufferedPort< yarp::sig::Vector > predict_inout
Buffered port for the incoming samples and corresponding replies.
MachinePortable machinePortable
A concrete wrapper around a learning machine.
virtual IMachineLearner & getMachine()
Retrieve the machine that is wrapped in the portable machine wrapper.
virtual MachinePortable & getMachinePortable()
Retrieve the machine portable.
PredictProcessor predictProcessor
The processor handling prediction requests.
A class that represents a prediction result.
A TrainEvent is raised when the machine handles a training sample.
virtual bool configure(yarp::os::ResourceFinder &opt)
virtual bool interruptModule()
virtual bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
virtual void setEnabled(bool val)
Enables or disables processing of training samples.
virtual void onRead(yarp::os::PortablePair< yarp::sig::Vector, yarp::sig::Vector > &sample)
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.