% group monte carlo runner
clear all; close all; clc;

addpath('C:\Users\Larkin\Google Drive\DropBox\VT\Research\SAR\Interaction Planning\MATLAB\Hollinger_Singh');

M = 20;
A = 10;
m = 15;
offset = 294;
xdata = [floor(linspace(200,800,M))', linspace(0.2,0.1,M)'];
seeds = zeros(M,A);
util = zeros(M,2*A); % [[IIP_1 IIP_2 ... IIP_A], [HS_1 HS_2 ... HS_A]]
comp_time = zeros(M,2*A);
path_lengths = zeros(M,2*A);

while m <= M
    fprintf("\nbegin iteration %d with offset %d --------- \n",m,offset);
    input_pj = xdata(m,1);
    input_env_step_size = xdata(m,2);
    
    a = 1;
    while a <= A
        fprintf("\naveraging iteration %d, offset %d --------- \n",a,offset);
        rng(offset);
        [util(m,a), comp_time(m,a), path_lengths(m,a)] = multi_agent_path_planning(input_pj, m);
        if util(m,a) == -1 || comp_time(m,a) == -1
            fprintf("\niteration %d, run %d, has failed \n",m,a);
            offset = offset + 1;
            continue;
        end
        
        rng(offset);
        [util(m,A+a), comp_time(m,A+a), path_lengths(m,A+a)] = main(input_env_step_size, m);
        if util(m,A+a) == -1 || comp_time(m,A+a) == -1
            fprintf("\niteration %d, run %d, has failed \n",m,a);
            offset = offset + 1;
            continue;
        end
        
        seeds(m,a) = offset;
        a = a + 1;
        offset = offset + 1;
    end
    
    m = m + 1;
end
time_str = datestr(now,31);
time_str = regexprep(time_str(6:end-3),':','-');
time_str = regexprep(time_str,' ','_');
filename = ['averaged_p2_M',num2str(M),'_',time_str,'.mat'];
save(filename,'util','comp_time','xdata','seeds','A','M','path_lengths');

offset
m