using tensor in vehicle

This commit is contained in:
2024-06-14 15:59:47 +02:00
parent a6dd800986
commit 474af56377
4 changed files with 71 additions and 60 deletions
@@ -149,7 +149,7 @@ void train_qlearning(struct RL_agent * rlAgent,
struct game_status * car_status = rlAgent->car->status; struct game_status * car_status = rlAgent->car->status;
if( copy_tensor_TYPE_FLOAT(experimental_values, action_value) == 0 /* done */){ if( copy_tensor_TYPE_FLOAT(experimental_values, action_value) == 0 /* done */){
if(status->done){ if(car_status->done){
} }
} }
+64 -54
View File
@@ -22,10 +22,14 @@ struct game_status * create_game_status(){
status->done = false; status->done = false;
} }
struct coordinate * create_coordinate(size_t dim_size){ coordinate * create_coordinate(size_t dim_size){
struct coordinate * ret_coord = malloc(sizeof(struct coordinate)); dimension * dim = create_dim(1);
ret_coord->dimension_size = dim_size; dim->perm[0] = dim_size;
ret_coord->x = malloc(dim_size * sizeof(float)); updateRankDim(dim);
coordinate * ret_coord = CREATE_TENSOR_TYPE_FLOAT(dim);
//struct coordinate * ret_coord = malloc(sizeof(struct coordinate));
//ret_coord->dimension_size = dim_size;
//ret_coord->x = malloc(dim_size * sizeof(float));
return ret_coord; return ret_coord;
} }
@@ -33,8 +37,8 @@ struct blocks * create_blocks(size_t nb_blocks, size_t dim_size){
struct blocks * ret_blocks = malloc(sizeof(struct blocks)); struct blocks * ret_blocks = malloc(sizeof(struct blocks));
ret_blocks->nb_blocks = nb_blocks; ret_blocks->nb_blocks = nb_blocks;
ret_blocks->dimension_size = dim_size; ret_blocks->dimension_size = dim_size;
ret_blocks->lower_bound_block = malloc(nb_blocks * sizeof(struct coordinate *)); ret_blocks->lower_bound_block = malloc(nb_blocks * sizeof(coordinate *));
ret_blocks->upper_bound_block = malloc(nb_blocks * sizeof(struct coordinate *)); ret_blocks->upper_bound_block = malloc(nb_blocks * sizeof(coordinate *));
ret_blocks->bounds_all_blocks = NULL; ret_blocks->bounds_all_blocks = NULL;
ret_blocks->all_updated = false; ret_blocks->all_updated = false;
ret_blocks->marker = malloc(nb_blocks * sizeof(bool)); ret_blocks->marker = malloc(nb_blocks * sizeof(bool));
@@ -48,10 +52,12 @@ struct blocks * create_blocks(size_t nb_blocks, size_t dim_size){
return ret_blocks; return ret_blocks;
} }
struct sensors * create_sensors(size_t nb_values){ sensors * create_sensors(size_t nb_xs){
struct sensors * ret_sensors = malloc(sizeof(struct sensors)); dimension * dim = create_dim(1);
ret_sensors->nb_values = nb_values; dim->perm[0] = nb_xs;
ret_sensors->value = malloc(nb_values * sizeof(float)); sensors * ret_sensors = CREATE_TENSOR_TYPE_FLOAT(dim);//malloc(sizeof(struct sensors));
//ret_sensors->nb_xs = nb_xs;
//ret_sensors->x = malloc(nb_xs * sizeof(float));
return ret_sensors; return ret_sensors;
} }
@@ -75,9 +81,10 @@ void free_game_status(struct game_status *status){
free(status); free(status);
} }
void free_coordinate(struct coordinate *coord){ void free_coordinate(coordinate *coord){
free(coord->x); //free(coord->x);
free(coord); //free(coord);
free_tensor_TYPE_FLOAT(coord);
} }
void free_blocks(struct blocks *blk){ void free_blocks(struct blocks *blk){
@@ -96,9 +103,10 @@ void free_blocks(struct blocks *blk){
free(blk); free(blk);
} }
void free_sensors(struct sensors *snsr){ void free_sensors(sensors *snsr){
free(snsr->value); //free(snsr->x);
free(snsr); //free(snsr);
free_tensor_TYPE_FLOAT(snsr);
} }
void free_vehicle(struct vehicle * vhcl){ void free_vehicle(struct vehicle * vhcl){
@@ -111,8 +119,8 @@ void free_vehicle(struct vehicle * vhcl){
} }
float scalar_product(struct coordinate *coord1, struct coordinate *coord2){ float scalar_product(coordinate *coord1, coordinate *coord2){
size_t dimension_size = coord1->dimension_size; /* size_t dimension_size = coord1->dimension_size;
if(coord1->dimension_size > coord2->dimension_size ) if(coord1->dimension_size > coord2->dimension_size )
dimension_size = coord2->dimension_size; dimension_size = coord2->dimension_size;
@@ -122,15 +130,17 @@ float scalar_product(struct coordinate *coord1, struct coordinate *coord2){
} }
return scalar; return scalar;
*/
return scalarProduct_0_TYPE_FLOAT(coord1, coord2);
} }
float vector_norm2(struct coordinate * coord){ float vector_norm2(coordinate * coord){
float scalar = scalar_product(coord, coord); float scalar = scalar_product(coord, coord);
return (float)sqrt(scalar); return (float)sqrt(scalar);
} }
bool is_in_block_index(struct blocks *blk, size_t index, struct coordinate *coord){ bool is_in_block_index(struct blocks *blk, size_t index, coordinate *coord){
if(blk->dimension_size != coord->dimension_size) return false; if(blk->dimension_size != coord->dim->rank) return false;
for(size_t j=0; j<blk->dimension_size; ++j){ for(size_t j=0; j<blk->dimension_size; ++j){
if( ((blk->lower_bound_block[index])->x[j] > coord->x[j] ) || if( ((blk->lower_bound_block[index])->x[j] > coord->x[j] ) ||
((blk->upper_bound_block[index])->x[j] < coord->x[j] ) ){ ((blk->upper_bound_block[index])->x[j] < coord->x[j] ) ){
@@ -142,8 +152,8 @@ bool is_in_block_index(struct blocks *blk, size_t index, struct coordinate *coor
} }
int is_in_blocks(struct blocks *blk, struct coordinate *coord){ int is_in_blocks(struct blocks *blk, coordinate *coord){
if(blk->dimension_size != coord->dimension_size) return 0; if(blk->dimension_size != coord->dim->rank) return 0;
size_t count_in = 0; size_t count_in = 0;
for(size_t i=0; i<blk->nb_blocks; ++i){ for(size_t i=0; i<blk->nb_blocks; ++i){
count_in = 0; count_in = 0;
@@ -159,22 +169,22 @@ int is_in_blocks(struct blocks *blk, struct coordinate *coord){
return 0; return 0;
} }
void printCoordinate(struct coordinate *coord, char *msg){ void printCoordinate(coordinate *coord, char *msg){
for(size_t i=0; i<coord->dimension_size; ++i){ for(size_t i=0; i<coord->dim->rank; ++i){
printf(" %f,", coord->x[i]); printf(" %f,", coord->x[i]);
} }
printf("{%s} [ %ld ]\n", msg, coord->dimension_size); printf("{%s} [ %ld ]\n", msg, coord->dim->rank);
} }
void copy_coordinate(struct coordinate *coord, float *x){ void copy_coordinate(coordinate *coord, float *x){
for(size_t i=0; i<coord->dimension_size; ++i){ for(size_t i=0; i<coord->dim->rank; ++i){
coord->x[i] = x[i]; coord->x[i] = x[i];
} }
} }
struct coordinate ** bounds_limits_blocks(struct blocks *blk){ /* min x , max y */ coordinate ** bounds_limits_blocks(struct blocks *blk){ /* min x , max y */
struct coordinate **bounds_coord = malloc(2 * sizeof(struct coordinate*)) ; coordinate **bounds_coord = malloc(2 * sizeof( coordinate*)) ;
bounds_coord[0] = create_coordinate(blk->dimension_size); bounds_coord[0] = create_coordinate(blk->dimension_size);
bounds_coord[1] = create_coordinate(blk->dimension_size); bounds_coord[1] = create_coordinate(blk->dimension_size);
@@ -213,13 +223,13 @@ void print2D_blocks(struct blocks *blk, float scale_x, float scale_y, char pad){
if(blk->dimension_size == 2){ if(blk->dimension_size == 2){
//struct coordinate ** bounds_coord = bounds_limits_blocks(blk); //struct coordinate ** bounds_coord = bounds_limits_blocks(blk);
update_bounds_limits_blocks(blk); update_bounds_limits_blocks(blk);
struct coordinate ** bounds_coord = blk->bounds_all_blocks; coordinate ** bounds_coord = blk->bounds_all_blocks;
for(int i=0; i<(bounds_coord[0]->dimension_size); ++i){ for(int i=0; i<(blk->dimension_size); ++i){
printf(" x[%d]: %f <= %f \n",i, bounds_coord[0]->x[i],bounds_coord[1]->x[i]); printf(" x[%d]: %f <= %f \n",i, bounds_coord[0]->x[i],bounds_coord[1]->x[i]);
} }
struct coordinate * coord = create_coordinate(2); coordinate * coord = create_coordinate(2);
// int offset_space = 0; // int offset_space = 0;
// int offset = 2; // int offset = 2;
//for(coord->x[1] = bounds_coord[0]->x[1]; coord->x[1] < bounds_coord[1]->x[1]; coord->x[1]+=scale_y ) //for(coord->x[1] = bounds_coord[0]->x[1]; coord->x[1] < bounds_coord[1]->x[1]; coord->x[1]+=scale_y )
@@ -253,8 +263,8 @@ void print2D_blocks(struct blocks *blk, float scale_x, float scale_y, char pad){
} }
struct blocks * block_neighbord_Point(struct coordinate *coord, float *radius ){ struct blocks * block_neighbord_Point(coordinate *coord, float *radius ){
struct blocks * blk = create_blocks(1, coord->dimension_size); struct blocks * blk = create_blocks(1, coord->dim->rank);
for(size_t i=0; i<blk->dimension_size; ++i){ for(size_t i=0; i<blk->dimension_size; ++i){
blk->lower_bound_block[0]->x[i] = coord->x[i]-radius[i] ; blk->lower_bound_block[0]->x[i] = coord->x[i]-radius[i] ;
blk->upper_bound_block[0]->x[i] = coord->x[i]+radius[i] ; blk->upper_bound_block[0]->x[i] = coord->x[i]+radius[i] ;
@@ -263,13 +273,13 @@ struct blocks * block_neighbord_Point(struct coordinate *coord, float *radius ){
return blk; return blk;
} }
void print2D_blocks_withPoint(struct blocks *blk, float scale_x, float scale_y, char pad, struct coordinate *coordPoint){ void print2D_blocks_withPoint(struct blocks *blk, float scale_x, float scale_y, char pad, coordinate *coordPoint){
if(blk->dimension_size == 2){ if(blk->dimension_size == 2){
//struct coordinate ** bounds_coord = bounds_limits_blocks(blk); //struct coordinate ** bounds_coord = bounds_limits_blocks(blk);
update_bounds_limits_blocks(blk); update_bounds_limits_blocks(blk);
struct coordinate ** bounds_coord = blk->bounds_all_blocks; coordinate ** bounds_coord = blk->bounds_all_blocks;
struct coordinate * coord = create_coordinate(2); coordinate * coord = create_coordinate(2);
float *radius = malloc(2 * sizeof(float)); float *radius = malloc(2 * sizeof(float));
//radius[0] = MIN(scale_x, scale_y); //radius[0] = MIN(scale_x, scale_y);
@@ -306,12 +316,12 @@ void print2D_blocks_withPoint(struct blocks *blk, float scale_x, float scale_y,
} }
} }
void print2D_blocks_indexOne_withPoint(struct blocks *blk, float scale_x, float scale_y, struct coordinate *coordPoint){ void print2D_blocks_indexOne_withPoint(struct blocks *blk, float scale_x, float scale_y, coordinate *coordPoint){
if(blk->dimension_size == 2){ if(blk->dimension_size == 2){
update_bounds_limits_blocks(blk); update_bounds_limits_blocks(blk);
struct coordinate ** bounds_coord = blk->bounds_all_blocks; coordinate ** bounds_coord = blk->bounds_all_blocks;
struct coordinate * coord = create_coordinate(2); coordinate * coord = create_coordinate(2);
float *radius = malloc(2 * sizeof(float)); float *radius = malloc(2 * sizeof(float));
radius[0]=scale_x; radius[0]=scale_x;
@@ -378,10 +388,10 @@ void move_vehicle(struct vehicle *v){
v->coord->x[1] += v->speed * sin(v->direction * M_PI / 180); v->coord->x[1] += v->speed * sin(v->direction * M_PI / 180);
} }
float distance2_coordinate(struct coordinate *c0, struct coordinate *c1){ float distance2_coordinate(coordinate *c0, coordinate *c1){
if(c0->dimension_size != c1->dimension_size) return 0; if(c0->dim->rank != c1->dim->rank) return 0;
float d=0, tmp; float d=0, tmp;
for(size_t i=0; i<c0->dimension_size; ++i){ for(size_t i=0; i<c0->dim->rank; ++i){
tmp = (c0->x[i] - c1->x[i]); tmp = (c0->x[i] - c1->x[i]);
d += tmp * tmp; d += tmp * tmp;
} }
@@ -394,13 +404,13 @@ float distance2_coordinate(struct coordinate *c0, struct coordinate *c1){
diStep_sensor->x[0] += step_sensor * cos(direction_radian);\ diStep_sensor->x[0] += step_sensor * cos(direction_radian);\
diStep_sensor->x[1] += step_sensor * sin(direction_radian);\ diStep_sensor->x[1] += step_sensor * sin(direction_radian);\
}\ }\
v->sensor->value[position] = (MIN(49,(distance2_coordinate(diStep_sensor, v->coord)))) / 50;\ v->sensor->x[position] = (MIN(49,(distance2_coordinate(diStep_sensor, v->coord)))) / 50;\
//v->sensor->value[position] = (MIN(49,(int)(distance2_coordinate(diStep_sensor, v->coord)/10))) / 50;\ //v->sensor->x[position] = (MIN(49,(int)(distance2_coordinate(diStep_sensor, v->coord)/10))) / 50;\
void read_sensor(struct vehicle *v){ void read_sensor(struct vehicle *v){
float step_sensor = ((float)1)/SUBDIVISION; float step_sensor = ((float)1)/SUBDIVISION;
struct coordinate * diStep_sensor = create_coordinate(2); coordinate * diStep_sensor = create_coordinate(2);
copy_coordinate(diStep_sensor, v->coord->x); copy_coordinate(diStep_sensor, v->coord->x);
// count the number of step until we go out of the path = distance // count the number of step until we go out of the path = distance
@@ -466,20 +476,20 @@ void add_string_log(struct game_status *status, char *str ){
} }
void step(struct vehicle *v, int action){ void step(struct vehicle *v, int action){
float action_value[NB_ACTION]={-3,0,3}; // [LEFT, CENTER, RIGHT] float action_x[NB_ACTION]={-3,0,3}; // [LEFT, CENTER, RIGHT]
v->direction = v->direction + action_value[action % 3]; v->direction = v->direction + action_x[action % 3];
v->speed = ((float)1)/2; v->speed = ((float)1)/2;
move_vehicle(v); move_vehicle(v);
read_sensor(v); read_sensor(v);
struct game_status *status = v->status; struct game_status *status = v->status;
status->state = v->sensor->value[LEFT]* 2500 + status->state = v->sensor->x[LEFT]* 2500 +
v->sensor->value[CENTER]* 50 + v->sensor->x[CENTER]* 50 +
v->sensor->value[RIGHT] ; v->sensor->x[RIGHT] ;
status->reward = 0; status->reward = 0;
status->done =false; status->done =false;
struct blocks * path = v->path; struct blocks * path = v->path;
//printf(" center : %f vs %f direction: %f\n",v->sensor->value[CENTER], LIMIT_DISTANCE, v->direction); //printf(" center : %f vs %f direction: %f\n",v->sensor->x[CENTER], LIMIT_DISTANCE, v->direction);
if( v->sensor->value[CENTER]<= LIMIT_DISTANCE ){ if( v->sensor->x[CENTER]<= LIMIT_DISTANCE ){
status->reward = REWARD_STOP; status->reward = REWARD_STOP;
status->done = true; status->done = true;
} }
+3 -2
View File
@@ -58,6 +58,7 @@ typedef tensor_TYPE_FLOAT coordinate;
*/ */
struct blocks { struct blocks {
size_t nb_blocks; size_t nb_blocks;
size_t dimension_size; /* rank */
coordinate **lower_bound_block; coordinate **lower_bound_block;
coordinate **upper_bound_block; coordinate **upper_bound_block;
coordinate **bounds_all_blocks; coordinate **bounds_all_blocks;
@@ -88,7 +89,7 @@ struct game_status * greate_game_status();
coordinate * create_coordinate(size_t dim_size); coordinate * create_coordinate(size_t dim_size);
struct blocks * create_blocks(size_t nb_blocks, size_t dim_size); struct blocks * create_blocks(size_t nb_blocks, size_t dim_size);
struct sensors * create_sensors(size_t nb_values); sensors * create_sensors(size_t nb_values);
struct vehicle * create_vehicle( struct vehicle * create_vehicle(
struct blocks *path struct blocks *path
); );
@@ -113,7 +114,7 @@ void step(struct vehicle *v, int action);
void reset(struct vehicle *v); void reset(struct vehicle *v);
void print2D_blocks_indexOne_withPoint(struct blocks *blk, float scale_x, float scale_y, struct coordinate *coordPoint); void print2D_blocks_indexOne_withPoint(struct blocks *blk, float scale_x, float scale_y, coordinate *coordPoint);
void print_vehicle_n_path(struct vehicle *v, float scale_x, float scale_y); void print_vehicle_n_path(struct vehicle *v, float scale_x, float scale_y);
float distance2_coordinate(coordinate *c0, coordinate *c1); float distance2_coordinate(coordinate *c0, coordinate *c1);
+3 -3
View File
@@ -23,7 +23,7 @@
#include "learn_to_drive.h" #include "learn_to_drive.h"
TEST(create_coordenate){ TEST(create_coordenate){
struct coordinate * coord = create_coordinate(3); coordinate * coord = create_coordinate(3);
coord->x[0]=0; coord->x[0]=0;
coord->x[1]=1.2; coord->x[1]=1.2;
coord->x[0]=0.8; coord->x[0]=0.8;
@@ -72,7 +72,7 @@ TEST(is_in_blocks){
copy_coordinate(zn->lower_bound_block[2], (float[]){0,4}); copy_coordinate(zn->lower_bound_block[2], (float[]){0,4});
copy_coordinate(zn->upper_bound_block[2], (float[]){6,7}); copy_coordinate(zn->upper_bound_block[2], (float[]){6,7});
struct coordinate *coord = create_coordinate(2); coordinate *coord = create_coordinate(2);
copy_coordinate(coord, (float[]){1,1}); copy_coordinate(coord, (float[]){1,1});
EXPECT_TRUE(is_in_blocks(zn, coord)); EXPECT_TRUE(is_in_blocks(zn, coord));
@@ -128,7 +128,7 @@ TEST(print_blocks_withPoint){
copy_coordinate(zn->upper_bound_block[6], (float[]){6,8}); copy_coordinate(zn->upper_bound_block[6], (float[]){6,8});
struct coordinate *coord = create_coordinate(2); coordinate *coord = create_coordinate(2);
copy_coordinate(coord, (float[]){1,1}); copy_coordinate(coord, (float[]){1,1});
EXPECT_TRUE(is_in_blocks(zn, coord)); EXPECT_TRUE(is_in_blocks(zn, coord));