Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Reinforcement learning and smart pointers

I am doing my Masters project on robotic's sensorimotor online learning using reinforcement learning methods (Q,sarsa,TD(λ),Actor-Critic,R,etc). I am currently designing the framework on which both higher level reinforcement learning and lower level robot API control will be using.

Since the states are robot sensor dependant and may (will) increase exponentially, I will be allocating them on the heap. Since this can create alot of problems, bugs, etc, and since parallelization (i.e. threading) is an aspect of reinforcement learning I want to explore, I am not yet sure of what kind of smart pointers to use.

Designing my own template/class for a smart pointer will take time and debugging, which I do not have. So, I am wondering, should I use STL's auto_ptr? I see they have issues being used in vectors. Should I use boost::shared_ptr? The states will have to be shared among many classes and algorithms. Or should I use boost::ptr_vector? Since the states will reside in a task container class in a vector, would this be sufficient? The states will have to be shared, copyable, referencable, serializable, non constant, thread-safe and will not be deleted. Also, memory space and computation time are important.

What do you recommend as the best smart ptr implementation for such a task ?

Thank you!


It seems like I will have to try using boost::ptr_vector with class State, and if this proves unefficient, then use std::vector < std::unique_ptr > and enable 0X. Thank you all for your answers and suggestions !

like image 237
Ælex Avatar asked Jul 06 '11 21:07

Ælex


People also ask

What is a smart pointer in C++?

In modern C++, it's smart pointers that take over. Begin this course by examining the idea behind smart pointers. You will then explore the different types of smart pointers, including unique pointers, shared pointers, and weak pointers. Discover why weak pointers are required in some cases to avoid circular references that can cause memory leaks.

Why do we use pointers in C++?

Pointers are used for accessing the resources which are external to the program – like heap memory. So, for accessing the heap memory (if anything is created inside heap memory), pointers are used. When accessing any external resource we just use a copy of the resource.

Why can’t I use smart pointers with locks?

Using locks is easy, but it makes smart pointer impossible to use in signal handlers; not much of a problem, but still annoying. A lock-free implementation would be ideal, but there is a big issue with that: lock-free implementations rely on a compare-and-swap mechanism on the wrapped pointer – here, the pointer is not wrapped.

Is a lock-free implementation of a wrapped pointer a good idea?

A lock-free implementation would be ideal, but there is a big issue with that: lock-free implementations rely on a compare-and-swap mechanism on the wrapped pointer – here, the pointer is not wrapped.


3 Answers

Single-ownership pointers are the harder to misuse, modulo the design of std::auto_ptr. You could consider using boost::scoped_ptr, which is safer yet (it can't transfer ownership though and can't be returned from a function). When it comes to containers, you could use a pointer container, but it's also fine to use e.g. std::vector without smart pointers if the types involved are not used polymorphically.

Shared ownership should remain an exceptional case; don't overuse boost::shared_ptr.

like image 141
Luc Danton Avatar answered Nov 01 '22 04:11

Luc Danton


Other suggestions missed one. boost::intrusive_ptr performs better than shared_ptr because it doesn't need a second allocation to hold the reference count. The downside of intrusive_ptr is a tiny bit of extra bookkeeping and no ability to use weak_ptr.

like image 24
Zan Lynx Avatar answered Nov 01 '22 03:11

Zan Lynx


I never found a smart ptr class that I liked when I was doing c++. I wrote my own in the end.

It had a cache feature for speed so it you kept allocating and freeing memory it could just hang on to the memory it had and reuse it. Also it had no default constructor so you had to pass it to functions/methods by reference else the compiler would show an error, this was so it would not create a temp copy of memory especially when dealing with large image files.

It would not take too long to write your own code and you can add your own bounds checking code to it as well.

like image 25
Steve Avatar answered Nov 01 '22 03:11

Steve