y_nnn: WORK IN PROGRESS... have to save

This commit is contained in:
2025-11-16 00:01:16 +01:00
parent 63d5f63030
commit ea26f5f3bc
9 changed files with 2747 additions and 16 deletions
@@ -387,9 +387,12 @@ char *fileNameDateScore(char * pre, char* post,size_t score){
return filename; return filename;
} }
const char* target_symlink = ".ff_target_.symlink";
const char* main_symlink = ".ff_main_.symlink";
void learn_to_drive(struct RL_agent * rlAgent){ void learn_to_drive(struct RL_agent * rlAgent){
printf("debug: start learn_to_drive\n");
int action; int action;
struct vehicle * car = rlAgent->car; struct vehicle * car = rlAgent->car;
struct game_status * car_status = car->status; struct game_status * car_status = car->status;
@@ -402,7 +405,7 @@ void learn_to_drive(struct RL_agent * rlAgent){
////pthread_create(&threadPrint, NULL, runPrint, (void*)rlAgent); ////pthread_create(&threadPrint, NULL, runPrint, (void*)rlAgent);
// while(true){ // while(true){
for(size_t index_episode = 0; index_episode < qlParams->number_episodes; ++index_episode){ for(size_t index_episode = 0; (!is_ending(qlStatus)) && (index_episode < qlParams->number_episodes) ; ++index_episode){
reset(car); reset(car);
qlStatus->nb_training_after_updated_weight_in_target = 0; qlStatus->nb_training_after_updated_weight_in_target = 0;
qlStatus->index_episode = index_episode; qlStatus->index_episode = index_episode;
@@ -427,9 +430,21 @@ void learn_to_drive(struct RL_agent * rlAgent){
push_back_list_TYPE_L_INT(qlStatus->progress_best_cumul, car_status->cumulative_reward); push_back_list_TYPE_L_INT(qlStatus->progress_best_cumul, car_status->cumulative_reward);
char *file = fileNameDateScore(".ff_main_",".txt",car_status->cumulative_reward); char *file = fileNameDateScore(".ff_main_",".txt",car_status->cumulative_reward);
EXPORT_TO_FILE_TENSOR_ATTRIBUTE_IN_NNEURONS(TYPE_FLOAT, rlAgent->networks->main_net ,weight_in, file); EXPORT_TO_FILE_TENSOR_ATTRIBUTE_IN_NNEURONS(TYPE_FLOAT, rlAgent->networks->main_net ,weight_in, file);
unlink(main_symlink);
if(symlink(file, main_symlink)==-1){
fprintf(stderr,"debug: symlink %s with %s.\n",main_symlink, file);
//fprintf(stderr,"debug: symlink %s with %s. explain:%s \n",main_symlink, file, explain_symlink(file, main_symlink) );
}
else write(1,":",1);
free(file); free(file);
file = fileNameDateScore(".ff_target_",".txt",car_status->cumulative_reward); file = fileNameDateScore(".ff_target_",".txt",car_status->cumulative_reward);
EXPORT_TO_FILE_TENSOR_ATTRIBUTE_IN_NNEURONS(TYPE_FLOAT, rlAgent->networks->target_net ,weight_in, file); EXPORT_TO_FILE_TENSOR_ATTRIBUTE_IN_NNEURONS(TYPE_FLOAT, rlAgent->networks->target_net ,weight_in, file);
unlink(target_symlink);
if(symlink(file, target_symlink)==-1){
fprintf(stderr,"debug: symlink %s with %s\n",target_symlink,file );
//fprintf(stderr,"debug: symlink %s with %s explain:%s\n",target_symlink,file,explain_symlink(file, target_symlink) );
}
else write(1,"-",1);
free(file); free(file);
} }
break; break;
@@ -25,6 +25,13 @@
//float d_reLU(float x); //float d_reLU(float x);
// to debug symlink error!: have to instal libexplain-dev (apt install),
// and add -lexplain in cflag (or in compile flag)
//#include <libexplain/symlink.h>
//#define target_symlink ".ff_target_.symlink"
//#define main_symlink ".ff_main_.symlink"
extern char *action_name[8]; extern char *action_name[8];
struct qlearning_params { struct qlearning_params {
@@ -2,6 +2,7 @@
#ifndef Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C #ifndef Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C
#define Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C #define Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C
#include <pthread.h>
#include <string.h> #include <string.h>
#include "learn_to_drive.h" #include "learn_to_drive.h"
@@ -52,6 +52,8 @@ struct arg_bash{
int fd_new_bash_pid; int fd_new_bash_pid;
int fd_current_bash_pid; int fd_current_bash_pid;
pthread_t *thread_launch;
pthread_mutex_t *mut_bash_var; pthread_mutex_t *mut_bash_var;
pthread_cond_t *cond_bash_var; pthread_cond_t *cond_bash_var;
int go_on; int go_on;
@@ -19,9 +19,12 @@ void free_arg_run_qlearn_bprint(struct arg_run_qlearn_bprint *arg){
free(arg); free(arg);
} }
void* run_sleep_wait_bash_and_print(void *arg);
void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){ void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){
struct arg_run_qlearn_bprint *run_arg=(struct arg_run_bash_print*)arg; struct arg_run_qlearn_bprint *run_arg=(struct arg_run_qlearn_bprint*)arg;
struct arg_bash *bash_arg=run_arg->bash_arg; struct arg_bash *bash_arg=run_arg->bash_arg;
struct RL_agent *rlAgent = run_arg->rlAgent;
if(arg && (len_buf>0)){ if(arg && (len_buf>0)){
@@ -32,6 +35,27 @@ void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){
launch_sleep_wait_bash(bash_arg); launch_sleep_wait_bash(bash_arg);
}else if(strncmp(buf,"killbash",8)==0){ }else if(strncmp(buf,"killbash",8)==0){
kill_all_bash(bash_arg); kill_all_bash(bash_arg);
}else if(strncmp(buf,"stoplearn",9)==0){
pthread_mutex_lock(rlAgent->status->mut_ending);
rlAgent->status->ending=1;
pthread_mutex_unlock(rlAgent->status->mut_ending);
kill_all_bash(bash_arg);
}else if(strncmp(buf,"startprintnewbash",17)==0){
run_newbash(bash_arg);
pthread_t thread_run;
pthread_create(&thread_run, NULL, runBashPrint, arg);
//Sleep(2);
}else if(strncmp(buf,"startprintwaitbash",18)==0){
pthread_t thread_run;
pthread_create(&thread_run, NULL, run_sleep_wait_bash_and_print, arg);
}else if(strncmp(buf,"stopprintbash",13)==0){
pthread_mutex_lock(bash_arg->mut_bash_var);
bash_arg->go_on=0;
pthread_mutex_unlock(bash_arg->mut_bash_var);
}else{ }else{
printf("debug: %s is not handle\n",buf); printf("debug: %s is not handle\n",buf);
} }
@@ -40,10 +64,29 @@ void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){
} }
void* run_sleep_wait_bash_and_print(void *arg){
struct arg_run_qlearn_bprint *arg_run=(struct arg_run_qlearn_bprint*)arg;
//struct RL_agent *rlAgent = arg_run->rlAgent;
struct arg_bash *bash_arg = arg_run->bash_arg;
wait_sleep_newbash(bash_arg);
return runBashPrint(arg);
}
void wait_valid_pid_bash(struct arg_bash *bash_arg){
pthread_mutex_lock(bash_arg->mut_bash_var);
while(
(bash_arg->fd_new_bash_pid == -1) &&
(bash_arg->fd_current_bash_pid == -1)
){
printf("debug: wait new bash!\n");
pthread_cond_wait(bash_arg->cond_bash_var, bash_arg->mut_bash_var);
}
pthread_mutex_unlock(bash_arg->mut_bash_var);
}
void* runBashPrint(void *arg){ void* runBashPrint(void *arg){
struct arg_run_bash_print *arg_run=(struct arg_run_bash_print*)arg; struct arg_run_qlearn_bprint *arg_run=(struct arg_run_qlearn_bprint*)arg;
struct RL_agent *rlAgent = arg_run->rlAgent; struct RL_agent *rlAgent = arg_run->rlAgent;
struct arg_bash *bash_arg = arg_run->bash_arg; struct arg_bash *bash_arg = arg_run->bash_arg;
@@ -53,6 +96,10 @@ void* runBashPrint(void *arg){
size_t count_print = 0; size_t count_print = 0;
char buf[SIZE_LOCAL_BUF]; char buf[SIZE_LOCAL_BUF];
int len_buf; int len_buf;
wait_valid_pid_bash(bash_arg);
printf("debug: start runBashPrint in episode: %ld\n",qlStatus->index_episode);
while((new_bash_exist(bash_arg)) && check_go_on_print_params(pprint) && !is_ending(qlStatus)){ while((new_bash_exist(bash_arg)) && check_go_on_print_params(pprint) && !is_ending(qlStatus)){
if(/*(qlStatus->nb_episodes %125 == 0) &&*/ pprint->printed){ if(/*(qlStatus->nb_episodes %125 == 0) &&*/ pprint->printed){
//pthread_mutex_lock(&(pprint->mut_printed)); //pthread_mutex_lock(&(pprint->mut_printed));
@@ -64,7 +111,7 @@ void* runBashPrint(void *arg){
len_buf=sprintf(buf,"%s ",pprint->string_space); len_buf=sprintf(buf,"%s ",pprint->string_space);
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf) BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
////printf("ep: %ld\n",qlStatus->index_episode); ////printf("ep: %ld ",qlStatus->index_episode);
len_buf=sprintf(buf,"ep: %ld\n",qlStatus->index_episode); len_buf=sprintf(buf,"ep: %ld\n",qlStatus->index_episode);
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf) BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
neurons_TYPE_FLOAT * net_main = rlAgent->networks->main_net; neurons_TYPE_FLOAT * net_main = rlAgent->networks->main_net;
@@ -104,5 +151,11 @@ void* runBashPrint(void *arg){
clear_screen(); clear_screen();
} }
} }
printf("debug: end runBashPrint\n");
return NULL; return NULL;
} }
// 0 if filename it exists
int file_exists(char *filename){
return access(filename, F_OK);
}
@@ -109,6 +109,7 @@ struct arg_bash *create_arg_bash(){
pthread_cond_init(b_arg->cond_bash_var,NULL); pthread_cond_init(b_arg->cond_bash_var,NULL);
b_arg->go_on=1; b_arg->go_on=1;
b_arg->thread_launch=NULL;
return b_arg; return b_arg;
} }
@@ -130,11 +131,16 @@ void free_arg_bash(struct arg_bash *arg){
kill(arg->current_bash_pid, SIGKILL); kill(arg->current_bash_pid, SIGKILL);
} }
if(arg->thread_launch){
pthread_join(*(arg->thread_launch), NULL);
free(arg->thread_launch);
}
free(arg); free(arg);
} }
int new_bash_exist(struct arg_bash *bash_arg){ int new_bash_exist(struct arg_bash *bash_arg){
return ((bash_arg->fd_new_bash_pid>0) || (bash_arg->fd_current_bash_pid)); return ((bash_arg->fd_new_bash_pid>0) || (bash_arg->fd_current_bash_pid>0));
} }
/* run new bash terminal graphic, can be called directly or in a thread */ /* run new bash terminal graphic, can be called directly or in a thread */
@@ -153,14 +159,23 @@ if(arg->new_bash_pid == arg->old_bash_pid){
}else{ }else{
wait(NULL); wait(NULL);
usleep(20000);
pthread_mutex_lock(arg->mut_bash_var); pthread_mutex_lock(arg->mut_bash_var);
arg->new_bash_pid = pidof("bash",NULL); arg->new_bash_pid = pidof("bash",NULL);
if(arg->new_bash_pid > arg->old_bash_pid){
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid); arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
printf("runnewbash: ready\n"); while(arg->new_bash_pid == arg->old_bash_pid)
{
printf("debug: create new terminal: %d vs %d\n",arg->new_bash_pid,arg->old_bash_pid);
getchar();
arg->new_bash_pid = pidof("bash",NULL);
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
//usleep(2000);
} }
pthread_mutex_unlock(arg->mut_bash_var); pthread_mutex_unlock(arg->mut_bash_var);
pthread_cond_signal(arg->cond_bash_var); pthread_cond_signal(arg->cond_bash_var);
printf("debug: send signal run_newbash\n");
} }
} }
+1 -1
View File
@@ -127,7 +127,7 @@ DEPLIBS=$(NETNEURNETDIR) $(YTESTDIR) $(YSOCKET_DIR) $(DEEPQLEARNDIR)
INCLUDE=$(INCLUDE_NNN) $(INCLUDE_SOCKET) $(INCLUDE_DEEPQLEARN) INCLUDE=$(INCLUDE_NNN) $(INCLUDE_SOCKET) $(INCLUDE_DEEPQLEARN)
CFLAGS=-Wall -lOpenCL -lm -lpthread -Werror -fpic $(INCLUDE) #-lcurses CFLAGS=-Wall -lOpenCL -lm -lpthread -Werror -fpic $(INCLUDE) #-lcurses -lexplain
#LDFLAGS=-L$(YTESTDIR) -lytest -lOpenCL -lm -lpthread #-lcurses #LDFLAGS=-L$(YTESTDIR) -lytest -lOpenCL -lm -lpthread #-lcurses
File diff suppressed because it is too large Load Diff
@@ -925,6 +925,16 @@ do{
#define TEST(name_f) \ #define TEST(name_f) \
FTEST__(__COUNTER__,name_f) FTEST__(__COUNTER__,name_f)
// HIDE_TEST
// to not execute test, it helps to manualy avoid test in code,
// instead of comment or #if 0 #endif
#define H__TEST__(count, name_f) \
void CONCAT(TEST_##name_f##____,count)(void) \
#define HIDE_TEST(name_f)\
H__TEST__(__COUNTER__,name_f)
/* /*
#define ASSERT_TRUE(val)\ #define ASSERT_TRUE(val)\
if(expected_true_f(val,#val,__func__) == false) {error_print("%s\n\n","Failure"); return;} if(expected_true_f(val,#val,__func__) == false) {error_print("%s\n\n","Failure"); return;}