Revision 15, 1.7 KB
(checked in by jgaeddert, 16 years ago)
|
reformatting goals, etc.; still needs work
|
Rev | Line | |
---|
[14] | 1 | // |
---|
| 2 | // Utility |
---|
| 3 | // |
---|
| 4 | |
---|
| 5 | #include <math.h> |
---|
| 6 | #include <stdlib.h> |
---|
[15] | 7 | #include <stdbool.h> |
---|
| 8 | |
---|
[14] | 9 | #include "utility.h" |
---|
| 10 | |
---|
| 11 | typedef float(*utility_function_prototype)(float _x, float _eta, float _sig); |
---|
[15] | 12 | float util_func_tanh(float _x, float _eta, float _sig); |
---|
[14] | 13 | float util_func_gauss(float _x, float _eta, float _sig); |
---|
| 14 | |
---|
| 15 | struct utility_s { |
---|
| 16 | float target; |
---|
[15] | 17 | int goal; |
---|
[14] | 18 | int scale; |
---|
[15] | 19 | bool flip; |
---|
[14] | 20 | utility_function_prototype f; |
---|
| 21 | float eta; |
---|
| 22 | float sig; |
---|
| 23 | }; |
---|
| 24 | |
---|
[15] | 25 | utility utility_create(float _target, int _goal, int _scale) |
---|
[14] | 26 | { |
---|
| 27 | utility u = (utility) malloc(sizeof(struct utility_s)); |
---|
| 28 | u->target = _target; |
---|
[15] | 29 | u->goal = _goal; |
---|
[14] | 30 | u->scale = _scale; |
---|
| 31 | |
---|
[15] | 32 | switch (u->goal) { |
---|
| 33 | case UTIL_GOAL_MIN: |
---|
| 34 | u->f = &util_func_tanh; |
---|
| 35 | u->eta = u->target; |
---|
| 36 | u->sig = 1.0f; |
---|
| 37 | u->flip = true; |
---|
| 38 | break; |
---|
| 39 | case UTIL_GOAL_MAX: |
---|
| 40 | u->f = &util_func_tanh; |
---|
| 41 | u->eta = u->target; |
---|
| 42 | u->sig = 1.0f; |
---|
| 43 | u->flip = false; |
---|
| 44 | break; |
---|
| 45 | case UTIL_GOAL_MEET: |
---|
[14] | 46 | u->f = &util_func_gauss; |
---|
| 47 | u->eta = u->target; |
---|
| 48 | u->sig = 1.0f; |
---|
[15] | 49 | u->flip = false; |
---|
[14] | 50 | break; |
---|
[15] | 51 | case UTIL_GOAL_AVOID: |
---|
| 52 | u->f = &util_func_gauss; |
---|
| 53 | u->eta = u->target; |
---|
[14] | 54 | u->sig = 1.0f; |
---|
[15] | 55 | u->flip = true; |
---|
[14] | 56 | break; |
---|
| 57 | default:; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | return u; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void utility_free(utility _u) |
---|
| 64 | { |
---|
| 65 | free(_u); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | float utility_evaluate(utility _u, float _x) |
---|
| 69 | { |
---|
[15] | 70 | float y = _x; |
---|
| 71 | if (_u->scale==UTIL_SCALE_LOG) |
---|
[14] | 72 | y = logf(y); |
---|
[15] | 73 | float v = _u->f( y, _u->eta, _u->sig); |
---|
| 74 | return (_u->flip) ? 1-v : v; |
---|
[14] | 75 | |
---|
| 76 | } |
---|
| 77 | |
---|
[15] | 78 | float util_func_tanh(float _x, float _eta, float _sig) { |
---|
[14] | 79 | return tanhf((_x-_eta)/_sig); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | float util_func_gauss(float _x, float _eta, float _sig) { |
---|
| 83 | return expf( -powf((_x-_eta),2)/(2*_sig*_sig) ); |
---|
| 84 | } |
---|
| 85 | |
---|