DevioLab CRYPTO TRADING AUTOMATION
LIVE
AUTOMATED CRYPTO TRADING • BINANCE
Автоматизуйте свій криптопортфель
Торгові боти DevioLab аналізують крипторинок, автоматично відкривають і закривають позиції та керують вашим портфелем на Binance 24/7.
CRYPTO 80 Bots
BINANCE Spot Trading
TRADING 24 / 7
Спробувати DevioLab
deviolab.com

Лабораторна робота №6

Інформація про навчальний заклад

ВУЗ:
Національний університет Львівська політехніка
Інститут:
Не вказано
Факультет:
Не вказано
Кафедра:
Не вказано

Інформація про роботу

Рік:
2018
Тип роботи:
Лабораторна робота
Предмет:
теорія інтелектуальних систем

Частина тексту файла (без зображень, графіків і формул):

‘МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ „ЛЬВІВСЬКА ПОЛІТЕХНІКА” / Лабораторна робота №6 з дисципліни " Теорія інтелектуальних систем" на тему: «Дослідження та моделювання колективної поведінки інтелектуальних агентів в задачах навчання з підкріпленням» Львів 2017 Мета: Дослідити модель взаємодії колективу агентів з середовищем та засвоїти принципи ізольованого й інтерактивного колективного навчання з підкріпленням. Порядок виконання роботи 1. Дослідити роботу програми обчислювального експерименту по моделюванню взаємодії колективу агентів з стаціонарним випадковим середовищем. 2. Модифікувати вхідні дані програми обчислювального експерименту згідно заданого варіанту. 3. Провести обчислювальний експеримент: 1) по дослідженню взаємодії колективу "випадкових" агентів з СВС; 2) по дослідженню взаємодії колективу "ідеальних" агентів з СВС. 3) по дослідженню взаємодії колективу RL-агентів заданого типу з СВС, при умові що вони виконують одночасне ізольоване колективне навчання з підкріпленням (Concurrent Isolated Reinforcement Learning). 4) по дослідженню взаємодії колективу RL-агентів заданого типу з СВС, при умові що вони виконують інтерактивне колективне навчання з підкріпленням (Interactive Reinforcement Learning). 4. Для всіх чотирьох випадків отримати усереднену залежність 1) sumRmas(t) - сумарного виграшу колективу агентів від часу; 2) avrRmas(t) - середнього виграшу колективу агентів від часу. Порівняти отримані залежності та зробити висновки. Варіант: №2 Номер варіанту Метод навчання з підкріпленням Кількість доступних окремому агенту дій, k Кількість агентів в колективі, N  2 e-greedy 3 20   Код програми: // tis.lab6.2016 // lab6.cpp #include <stdio.h> #include <stdlib.h> #include <time.h> #include <tchar.h> #include <math.h> #define ENVTYPE 0 #define NACTIONS 3 #define NSTATES 3 #define NAGENTS 20 #define NSTEPS 200 #define NREPLICAS 1000 #define REWARD 1//+1 #define PENALTY 0//-1 #define RLTYPE 2 //3 //4 #define RLEPSILON 0.1f #define RLTAU 0.12f // --------------------------------------- // global parameters and values int t; // current time step int T = NSTEPS; // number of time steps = number of interactions between agent and environment int n = NREPLICAS;// number of replicas int nA = NACTIONS;// number of actions int nS = NSTATES;// number of states int k; // index for agents' numeration int nK = NAGENTS; // number of agents // --------------------------------------- // environment int env = ENVTYPE; // type of environment: // env = 0 -> se (stationary environment) // env = 1 -> ce (commutative environment) float sePa[NACTIONS]; // se: probabilities of rewards for each action int ceState; // ce: current state of commutative environment float cePa[NSTATES][NACTIONS]; // ce: probabilities of reward for each action for each state of environment float cePs[NSTATES][NSTATES]; // ce: probabilities of transition from one state to another // --------------------------------------- // agent --> multi-agent system int agt = 3; // type of agent: // agt = 0 -> random agent // agt = 1 -> perfect agent // agt = 2 -> greedy RL // agt = 3 -> epsilon-greedy RL // agt = 4 -> softmax action selection int mas; // type of multi-agent system: // mas = 0 -> system of random agents // mas = 1 -> system of perfect agents // mas = 2 -> system of RL-agents -> Concurrent Isolated RL // mas = 3 -> system of perfect agents -> Interactive RL int action[NAGENTS]; // current action = {0, ... ,(nA-1)} int response[NAGENTS]; // current response of environment = {0;1}/{-1;+1} int paction; // action of perfect agent float e = RLEPSILON; // epsilon value (epsilon-greedy RL) float tau = RLTAU; // tau value (softmax action selection) int ka[NAGENTS][NACTIONS]; // number of realizations for each action int ra[NAGENTS][NACTIONS]; // total reward for each action float Q[NAGENTS][NACTIONS]; // estimated action value Q[i]=r[i]/k[i] for each action; float p[NAGENTS][NACTIONS]; // selection probability for each action (softmax); // --------------------------------------- // results for current replica float sumR[NAGENTS];// total reward over time sumR(t) float avrR[NAGENTS];// average reward over time avrR(t) = sumR(t)/t float sumRmas; // total reward over time for multi-agent system float avrRmas; // average reward over time for multi-agent system // --------------------------------------- // tabulated results float _sumR[NSTEPS][NREPLICAS]; float _avrR[NSTEPS][NREPLICAS]; // --------------------------------------- // final simulation results float sumRm[NSTEPS]; // mean values of sumR(t) float sumRv[NSTEPS]; // corresponding variances float avrRm[NSTEPS]; // mean values of avrR(t) float avrRv[NSTEPS]; // corresponding variances // --------------------------------------- // files for parameters and results char * par_file_name = "d:\\lab6.parameters.txt"; FILE * par_file; char * RA_res_file_name = "d:\\lab6.MAS-RA.results.txt"; FILE * RA_res_file; char * PA_res_file_name = "d:\\lab6.MAS-PA.results.txt"; FILE * PA_res_file; char * RL1_res_file_name = "d:\\lab6.MAS-RL-ISO.results.txt"; FILE * RL1_res_file; char * RL2_res_file_name = "d:\\lab4.MAS-RL-INT.results.txt"; FILE * RL2_res_file; // --------------------------------------- // uniform discrete probability distribution int uRand(int x) { int _rnum = (int)((float)x * (float)rand() / (float)RAND_MAX); return _rnum; } // --------------------------------------- // discrete probability distribution specified by probabilities from <_array> int dRand(float* _array, int size) { int _rnum = size - 1; float _left = 0; float _right = _array[0]; float ftmp = (float)rand() / (float)RAND_MAX; for (int i = 0; i < size - 1; i++) { if ((ftmp >= _left) && (ftmp < _right)) { _rnum = i; break; } _left = _right; _right += _array[i + 1]; } return _rnum; } // --------------------------------------- // initialization of stationary environment void seInit(void) { for (int i = 0; i < nA; i++) sePa[i] = (float)rand() / (float)RAND_MAX; sePa[0] = 0.2f; sePa[1] = 0.8f; } // --------------------------------------- // response of stationary environment int seResponse(int agID) { int _r, _action = action[agID]; float rnum = (float)rand() / (float)RAND_MAX; if (rnum < sePa[_action]) _r = REWARD; else _r = PENALTY; return _r; } // --------------------------------------- // initialization of commutative environment void ceInit(void) { int i, j; float _sum1, _sum2; // probabilities of rewards for (i = 0; i < nS; i++) for (j = 0; j < nA; j++) cePa[i][j] = (float)rand() / (float)RAND_MAX; // probabilities of state transition for (i = 0; i < nS; i++) { _sum1 = 0; _sum2 = 0; for (j = 0; j < nS; j++) { cePs[i][j] = (float)rand() / (float)RAND_MAX; _sum1 += cePs[i][j]; } for (j = 0; j < nS - 1; j++) { cePs[i][j] = cePs[i][j] / _sum1; _sum2 += cePs[i][j]; } cePs[i][nS - 1] = 1.0f - _sum2; } // initial state ceState = uRand(nS); } // --------------------------------------- // response of commutative environment int ceResponse(int agID) { int _r, _action = action[agID]; // get response in current state float rnum = (float)rand() / (float)RAND_MAX; if (rnum < cePa[ceState][_action]) _r = REWARD; else _r = PENALTY; // commutate states ceState = dRand(cePs[ceState], nS); return _r; } // --------------------------------------- // environment int environment(int _env, int agID) { int _r = 0; switch (_env) { case 0: _r = seResponse(agID); break; case 1: _r = ceResponse(agID); break; default: printf("lab4 error: wrong env code specified\n"); } return _r; } // --------------------------------------- // save parameters in file void saveParameters(void) { int i, j; if ((par_file = fopen(par_file_name, "w")) == NULL) { fprintf(stderr, "Cannot open file <%s> for parameters of experiment.\n", par_file_name); } fprintf(par_file, "T = %d\n", T); fprintf(par_file, "n = %d\n", n); fprintf(par_file, "env = %d\n", env); fprintf(par_file, "nA = %d\n", nA); fprintf(par_file, "nK = %d\n", nK); if (env) fprintf(par_file, "nS = %d\n", nS); fprintf(par_file, "RL-agent type = %d\n", RLTYPE); if (agt == 3) fprintf(par_file, "epsilon = %f\n", e); if (agt == 4) fprintf(par_file, "tau = %f\n", tau); fprintf(par_file, "====================\n"); switch (env) { case 0: // se (stationary environment) for (i = 0; i < nA; i++) fprintf(par_file, "p(a%d) = %f\n", i, sePa[i]); break; case 1: // ce (commutative environment) // probabilities of rewards for (i = 0; i < nS; i++) { for (j = 0; j < nA; j++) fprintf(par_file, "p(s%d,a%d) = %f\n", i, j, cePa[i][j]); if (i < nS - 1) fprintf(par_file, "--------------------\n"); } fprintf(par_file, "\n====================\n"); // probabilities of state transition for (i = 0; i < nS; i++) { for (j = 0; j < nS; j++) fprintf(par_file, "p(s%d,s%d) = %f\n", i, j, cePs[i][j]); fprintf(par_file, "--------------------\n"); } break; default: printf("lab4 error: wrong env model code specified\n"); } fclose(par_file); } // --------------------------------------- // save results of random agent void saveResultsRA(void) { int i; if ((RA_res_file = fopen(RA_res_file_name, "w")) == NULL) fprintf(stderr, "Cannot open file <%s> for experimental results.\n", RA_res_file_name); for (i = 0; i < T; i++) fprintf(RA_res_file, "%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]); fclose(RA_res_file); } // --------------------------------------- // save results of perfect agent void saveResultsPA(void) { int i; if ((PA_res_file = fopen(PA_res_file_name, "w")) == NULL) fprintf(stderr, "Cannot open file <%s> for experimental results.\n", PA_res_file_name); for (i = 0; i < T; i++) fprintf(PA_res_file, "%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]); fclose(PA_res_file); } // --------------------------------------- // save results of RL-agent void saveResultsRL_ISO(void) { int i; if ((RL1_res_file = fopen(RL1_res_file_name, "w")) == NULL) fprintf(stderr, "Cannot open file <%s> for experimental results.\n", RL1_res_file_name); for (i = 0; i < T; i++) fprintf(RL1_res_file, "%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]); fclose(RL1_res_file); } // --------------------------------------- // save results of RL-agent void saveResultsRL_INT(void) { int i; if ((RL2_res_file = fopen(RL2_res_file_name, "w")) == NULL) fprintf(stderr, "Cannot open file <%s> for experimental results.\n", RL2_res_file_name); for (i = 0; i < T; i++) fprintf(RL2_res_file, "%f,%f,%f,%f\n", sumRm[i], sumRv[i], avrRm[i], avrRv[i]); fclose(RL2_res_file); } // --------------------------------------- // return index of maximal value in <_array> int argmax(float* _array, int size) { int _arg = uRand(size); float _max = _array[_arg]; for (int i = 0; i < size; i++) if (_array[i] > _max) { _max = _array[i]; _arg = i; } return _arg; } // --------------------------------------- // init agent void initAgent(int _ag, int id) { int i; switch (_ag) { case 0: break; case 1: break; case 2: for (i = 0; i<nA; i++){ ka[id][i] = 0; ra[id][i] = 0; Q[id][i] = 1.0f; }; action[id] = uRand(nA); break; case 3: for (i = 0; i<nA; i++){ ka[id][i] = 0; ra[id][i] = 0; Q[id][i] = 1.0f; }; action[id] = uRand(nA); break; case 4: for (i = 0; i<nA; i++){ ka[id][i] = 0; ra[id][i] = 0; Q[id][i] = 0.0f; p[id][i] = 0.0f; }; action[id] = uRand(nA); break; default: printf("lab4 error: wrong agent code specified\n"); } } // --------------------------------------- // random agent int randomAgent(void) { return uRand(nA); } // --------------------------------------- // perfect agent int perfectAgent(void) { if (env) paction = argmax(cePa[ceState], nA); else paction = argmax(sePa, nA); return paction; } // --------------------------------------- // greedy RL int greedy(int id) { int _action = action[id]; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // select next action _action = argmax(Q[id], nA); return _action; } // --------------------------------------- // epsilon greedy RL int epsilonGreedy(int id) { int _action = action[id]; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // select next action float rnum = (float)rand() / (float)RAND_MAX; if (rnum < e) _action = uRand(nA); else _action = argmax(Q[id], nA); return _action; } // ---------------------------------------------------------------------------- // softmax action selection int softmax(int id) { int i; int _action = action[id]; float tmp[NACTIONS], pSum = 0.0f; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // modify values of selection probabilities for (i = 0; i < nA; i++) { tmp[i] = expf(Q[id][i] / tau); pSum += tmp[i]; } for (i = 0; i < nA; i++) { p[id][i] = tmp[i] / pSum; } // select next action _action = dRand(p[id], nA); return _action; } // --------------------------------------- // interactive greedy RL int greedyINT(int id) { int _action = action[id]; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // get action values from neighbours -> average out int left = (id == 0) ? (nK - 1) : (id - 1); int right = (id == (nK - 1)) ? 0 : (id + 1); for (int i = 0; i<nA; i++) Q[id][i] = (Q[left][i] + Q[id][i] + Q[right][i]) / 3.0f; // select next action _action = argmax(Q[id], nA); return _action; } // --------------------------------------- // interactive epsilon greedy RL int epsilonGreedyINT(int id) { int _action = action[id]; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // get action values from neighbours -> average out int left = (id == 0) ? (nK - 1) : (id - 1); int right = (id == (nK - 1)) ? 0 : (id + 1); for (int i = 0; i<nA; i++) Q[id][i] = (Q[left][i] + Q[id][i] + Q[right][i]) / 3.0f; // select next action float rnum = (float)rand() / (float)RAND_MAX; if (rnum < e) _action = uRand(nA); else _action = argmax(Q[id], nA); return _action; } // --------------------------------------- // interactive softmax action selection int softmaxINT(int id) { int i; int _action = action[id]; float tmp[NACTIONS], pSum = 0.0f; // modify estimated action value ra[id][action[id]] += response[id]; ka[id][action[id]]++; Q[id][action[id]] = (float)ra[id][action[id]] / (float)ka[id][action[id]]; // get action values from neighbours -> average out int left = (id == 0) ? (nK - 1) : (id - 1); int right = (id == (nK - 1)) ? 0 : (id + 1); for (int i = 0; i<nA; i++) Q[id][i] = (Q[left][i] + Q[id][i] + Q[right][i]) / 3.0f; // modify values of selection probabilities for (i = 0; i < nA; i++) { tmp[i] = expf(Q[id][i] / tau); pSum += tmp[i]; } for (i = 0; i < nA; i++) { p[id][i] = tmp[i] / pSum; } // select next action _action = dRand(p[id], nA); return _action; } // --------------------------------------- // agent int agent(int _ag, int id) { int _a = 0; switch (_ag) { case 0: _a = randomAgent(); break; case 1: _a = perfectAgent(); break; case 2: _a = greedy(id); break; case 3: _a = epsilonGreedy(id); break; case 4: _a = softmax(id); break; case 12: _a = greedyINT(id); break; case 13: _a = epsilonGreedyINT(id); break; case 14: _a = softmaxINT(id); break; default: printf("lab4 error: wrong agent code specified\n"); } return _a; } // --------------------------------------- // init mas void initMAS(int _mas) { int i; switch (_mas) { case 0: for (i = 0; i<nK; i++){ initAgent(agt, i); sumR[i] = 0.0f; avrR[i] = 0.0f; } break; case 1: for (i = 0; i<nK; i++){ initAgent(agt, i); sumR[i] = 0.0f; avrR[i] = 0.0f; } break; case 2: for (i = 0; i<nK; i++){ initAgent(agt, i); sumR[i] = 0.0f; avrR[i] = 0.0f; } break; case 3: for (i = 0; i<nK; i++){ initAgent(agt - 10, i); sumR[i] = 0.0f; avrR[i] = 0.0f; } break; default: printf("lab4 error: wrong mas code specified\n"); } } // simulation void simulation(int _i) { float tmpS = 0.0f; float tmpA = 0.0f; initMAS(mas); sumRmas = 0.0f; avrRmas = 0.0f; for (t = 0; t < T; t++) { // get action of each agent for (k = 0; k < nK; k++) action[k] = agent(agt, k); // get response of environment for each agent for (k = 0; k < nK; k++) response[k] = environment(env, k); // calculate cumulative results for each agent tmpS = 0.0f; tmpA = 0.0f; for (k = 0; k < nK; k++) { sumR[k] = sumR[k] + (float)response[k]; tmpS = tmpS + sumR[k]; avrR[k] = sumR[k] / ((float)t + 1); tmpA = tmpA + avrR[k]; } // calculate cumulative results for malti-agent system sumRmas = tmpS / (float)nK; avrRmas = tmpA / (float)nK; // save results _sumR[t][_i] = sumRmas; _avrR[t][_i] = avrRmas; } } // --------------------------------------- // get mean values of simulation results void getMeanValues(void) { for (t = 0; t < T; t++) { float tmps1 = 0.0f; float tmps2 = 0.0f; for (int i = 0; i < n; i++) { tmps1 += _sumR[t][i]; tmps2 += _avrR[t][i]; } sumRm[t] = (float)tmps1 / (float)n; avrRm[t] = (float)tmps2 / (float)n; } } // get variances of simulation results void getVarianceValues(void) { for (t = 0; t < T; t++) { float tmps1 = 0.0f; float tmps2 = 0.0f; for (int i = 0; i < n; i++) { tmps1 += (sumRm[t] - _sumR[t][i]) * (sumRm[t] - _sumR[t][i]); tmps2 += (
Антиботан аватар за замовчуванням

15.02.2018 20:41

Коментарі

Ви не можете залишити коментар. Для цього, будь ласка, увійдіть або зареєструйтесь.

Ділись своїми роботами та отримуй миттєві бонуси!

Нічого не вибрано
0%

Оголошення від адміністратора

Антиботан аватар за замовчуванням

Подякувати Студентському архіву довільною сумою

Admin

26.02.2023 12:38

Дякуємо, що користуєтесь нашим архівом!