XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
env_mux.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
31#include "env_mux.h"
32#include "param.h"
33#include "utils.h"
34
35#define MAX_PAYOFF (1.)
36
43void
44env_mux_init(struct XCSF *xcsf, const int bits)
45{
46 struct EnvMux *env = malloc(sizeof(struct EnvMux));
47 env->pos_bits = 1;
48 while (env->pos_bits + pow(2, env->pos_bits) <= bits) {
49 ++(env->pos_bits);
50 }
51 --(env->pos_bits);
52 const int n = env->pos_bits + (int) pow(2, env->pos_bits);
53 env->state = malloc(sizeof(double) * n);
54 xcsf->env = env;
55 param_init(xcsf, n, 1, 2);
56}
57
62void
63env_mux_free(const struct XCSF *xcsf)
64{
65 struct EnvMux *env = xcsf->env;
66 free(env->state);
67 free(env);
68}
69
75const double *
77{
78 const struct EnvMux *env = xcsf->env;
79 for (int i = 0; i < xcsf->x_dim; ++i) {
80 env->state[i] = rand_uniform(0, 1);
81 }
82 return env->state;
83}
84
91double
92env_mux_execute(const struct XCSF *xcsf, const int action)
93{
94 const struct EnvMux *env = xcsf->env;
95 int pos = env->pos_bits;
96 for (int i = 0; i < env->pos_bits; ++i) {
97 if (env->state[i] > 0.5) {
98 pos += (int) pow(2, (double) (env->pos_bits - 1 - i));
99 }
100 }
101 const int answer = (env->state[pos] > 0.5) ? 1 : 0;
102 return (action == answer) ? MAX_PAYOFF : 0;
103}
104
109void
110env_mux_reset(const struct XCSF *xcsf)
111{
112 (void) xcsf;
113}
114
120bool
122{
123 (void) xcsf;
124 return true;
125}
126
132double
134{
135 (void) xcsf;
136 return MAX_PAYOFF;
137}
138
144bool
146{
147 (void) xcsf;
148 return false;
149}
void env_mux_free(const struct XCSF *xcsf)
Frees the multiplexer environment.
Definition env_mux.c:63
double env_mux_execute(const struct XCSF *xcsf, const int action)
Returns the reward for executing a multiplexer action.
Definition env_mux.c:92
const double * env_mux_get_state(const struct XCSF *xcsf)
Returns a random multiplexer problem instance.
Definition env_mux.c:76
void env_mux_reset(const struct XCSF *xcsf)
Dummy method since no multiplexer reset is necessary.
Definition env_mux.c:110
bool env_mux_is_done(const struct XCSF *xcsf)
Returns whether the multiplexer is in a terminal state.
Definition env_mux.c:121
bool env_mux_multistep(const struct XCSF *xcsf)
Returns whether the multiplexer is a multistep problem.
Definition env_mux.c:145
double env_mux_maxpayoff(const struct XCSF *xcsf)
Returns the maximum payoff value possible in the multiplexer.
Definition env_mux.c:133
#define MAX_PAYOFF
Payoff provided for making a correct classification.
Definition env_mux.c:35
void env_mux_init(struct XCSF *xcsf, const int bits)
Initialises a real multiplexer environment of specified length.
Definition env_mux.c:44
The real multiplexer problem environment.
void param_init(struct XCSF *xcsf, const int x_dim, const int y_dim, const int n_actions)
Initialises default XCSF parameters.
Definition param.c:45
Functions for setting and printing parameters.
Real multiplexer environment data structure.
Definition env_mux.h:32
int pos_bits
Number of position bits.
Definition env_mux.h:34
double * state
Current state.
Definition env_mux.h:33
XCSF data structure.
Definition xcsf.h:85
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition utils.c:62
Utility functions for random number handling, etc.