Returning more than one value from a function

I want to return multiple values (p0, p1, p2, p3) rather than one. Thank you in advance.

int probabilities(int newstate) {
int p0, p1, p2, p3;
float max = q_table[newstate][0];
int optimal_action = 0;
for (int i = 1; i<4; i++) {
if (q_table[newstate][i] > max) {
max = q_table[newstate][i];
optimal_action = i;
}
}
p0 = exploration_rate / 4;
p1 = exploration_rate / 4;
p2 = exploration_rate / 4;
p3 = exploration_rate / 4;

if (optimal_action == 0) {
p0 += 1 - exploration_rate;}
else if (optimal_action == 1) {
p1 += 1 - exploration_rate;}
else if (optimal_action == 2) {
p2 += 1 - exploration_rate;}
else if (optimal_action == 3) {
p3 += 1 - exploration_rate;}


return p0, p1, p2, p3;

}
They are of the same type. Just make them the elements of a std::vector<double> and return that.
The latter half of your code could also be massively reduced:
1
2
3
vector<double> p(4,exploration_rate/4);
p[optimal_action] += 1 - exploration_rate;
return p;



It would probably be better if q_table[][], optimal _action and exploration_rate weren't global variables either.

You don't appear to be doing anything with max.
Last edited on
This is what std::tuple (C++17) is for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <tuple>

auto f( int x, int y, int z )
{
  return std::make_tuple( x + 1, y + 2, z + 3 );
}

int main()
{
  auto [a, b, c] = f( 0, 0, 0 );
  std::cout << a << "\t" << b << "\t" << c << "\n";

  std::tie(a, b, c) = f( a, b, c );
  std::cout << a << "\t" << b << "\t" << c << "\n";
}

Hope this helps.
std::tuple is part of C++11
you can only ever return one thing via the return statement. That one thing can be a container of many things, including:
<tuple>
struct or class
pointer (static array is similar)
vector, string, map, pair, and the other containers
and anything else you can think of that wraps up many items behind one variable.

alternately, pass by reference will let you return many things:
void foo( int &i, double &d) //i and d can be modified to return new value inside foo. I do not care for this approach for many reasons, but many, many functions out there use this and its valid.

Last edited on
std::tuple is C++11 but the cool syntax to decouple stuff is C++17
Hello grkanklcsln,

Since no one else has mentioned it yet.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
lastchance wrote:
It would probably be better if q_table[][], optimal _action and exploration_rate weren't global variables either.

You don't appear to be doing anything with max.
1
2
3
4
5
6
7
8
float max = q_table[newstate][0];
int optimal_action = 0;
for ( int i = 1; i<4; i++ ) {
    if ( q_table[newstate][i] > max ) {
        max = q_table[newstate][i];
        optimal_action = i;
    }
}

The max is used in the condition. However, one can rewrite that without the variable:
1
2
3
4
5
6
int optimal_action = 0;
for ( int i = 1; i<4; i++ ) {
    if ( q_table[newstate][i] > q_table[newstate][optimal_action] ) {
        optimal_action = i;
    }
}

One can go one step further and use library algorithms:
1
2
auto max = std::max_element( q_table[newstate], q_table[newstate] + 4 );
int optimal_action = std::distance( q_table[newstate], max );



Whether the q_table[][] is global or not, and whatever the return type is, what will probably happen with this call:
auto result = probabilities( 999999999 );

In other words, should we really trust the user to give a valid index to the array?
It's probably Q-learning anyway, in which case I thought the idea was to update the Q-table, not return a set of probabilities.

Over to the OP to inform us.
Topic archived. No new replies allowed.