├── .gitignore ├── A1 ├── Q1_data │ ├── Images │ │ ├── 1.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 4.png │ │ ├── 5.png │ │ ├── 6.png │ │ ├── 7.png │ │ ├── 8.png │ │ ├── 9.png │ │ ├── bias.png │ │ ├── bullseye.png │ │ ├── bvplot.png │ │ ├── tableq1.png │ │ └── variance.png │ ├── README.md │ ├── Report1.pdf │ ├── data.pkl │ ├── q1.py │ ├── q1temp.py │ └── trial.py ├── Q2_data │ ├── Fx_test.pkl │ ├── Images │ │ ├── 1.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 4.png │ │ ├── 5.png │ │ ├── 6.png │ │ ├── 7.png │ │ ├── 8.png │ │ ├── 9.png │ │ ├── bias.png │ │ ├── bullseye.png │ │ ├── bvplot.png │ │ ├── tableq2.png │ │ └── variance.png │ ├── README.md │ ├── Report2.pdf │ ├── X_test.pkl │ ├── X_train.pkl │ ├── Y_train.pkl │ └── q2.py ├── README.md └── submission │ ├── Q1 │ ├── Images │ │ ├── 1.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 4.png │ │ ├── 5.png │ │ ├── 6.png │ │ ├── 7.png │ │ ├── 8.png │ │ ├── 9.png │ │ ├── bias.png │ │ ├── bullseye.png │ │ ├── bvplot.png │ │ ├── tableq1.png │ │ └── variance.png │ ├── Report1.pdf │ └── q1.py │ └── Q2 │ ├── Images │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ ├── 9.png │ ├── bias.png │ ├── bullseye.png │ ├── bvplot.png │ ├── tableq2.png │ └── variance.png │ ├── Report2.pdf │ └── q2.py ├── A2-2 ├── README.md ├── images │ ├── Bellman.png │ ├── Riaj.png │ └── vi.png └── submission │ ├── outputs │ ├── task_1_trace.txt │ ├── task_2_part_1_trace.txt │ ├── task_2_part_2_trace.txt │ └── task_2_part_3_trace.txt │ ├── task_1.py │ └── team_74_report.pdf ├── A2-3 ├── Readme.md ├── intermediate │ ├── check.txt │ ├── check2.txt │ ├── code.py │ ├── matA.txt │ ├── sample.py │ ├── slides.py │ ├── states_actions.txt │ ├── temp │ └── temp.txt ├── outputs │ └── output.json └── submission │ ├── outputs │ └── output.json │ ├── solution.py │ └── team_62_report.pdf ├── A3 ├── Readme.md ├── Report.pdf ├── __pycache__ │ ├── client_moodle.cpython-36.pyc │ └── tester.cpython-36.pyc ├── client_moodle.py ├── diagrams │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── diag_2.png │ ├── diag_3.png │ └── diag_4.png ├── good-error.png ├── intermediate │ ├── 0104 │ ├── 0104_1 │ ├── 0104_2 │ ├── 0104_3 │ ├── 0104_4 │ ├── check.txt │ ├── code.py │ ├── code_withprob.py │ ├── fresh.py │ ├── freshtables.py │ ├── lastday │ │ ├── file1.txt │ │ ├── file2.txt │ │ ├── file3.txt │ │ ├── file4.txt │ │ ├── file5.txt │ │ ├── file_10.txt │ │ ├── file_11.txt │ │ ├── file_12.txt │ │ ├── file_6.txt │ │ ├── file_7.txt │ │ ├── file_8.txt │ │ └── file_9.txt │ ├── newcode.py │ ├── newcode_26.py │ ├── oldcode.py │ ├── oldnew.py │ ├── oldstuff │ │ ├── client.py │ │ └── overfit_weights.txt │ ├── output.txt │ ├── outputs │ │ ├── 02_04.txt │ │ ├── 02_04_1.txt │ │ ├── 02_04_2.txt │ │ ├── 02_04_3.txt │ │ ├── 02_04_4.txt │ │ ├── 02_04_5.txt │ │ ├── 2303.txt │ │ ├── 2303_2.txt │ │ ├── 2403.txt │ │ ├── 2403_2.txt │ │ ├── 2403_3.txt │ │ ├── 2403_4.txt │ │ ├── 2503.txt │ │ ├── 2503_2.txt │ │ ├── 2603.txt │ │ ├── 2603_10.txt │ │ ├── 2603_2.txt │ │ ├── 2603_3.txt │ │ ├── 2603_4.txt │ │ ├── 2603_5.txt │ │ ├── 2603_6.txt │ │ ├── 2603_7.txt │ │ ├── 2603_8.txt │ │ ├── 2603_9.txt │ │ ├── 2703.txt │ │ ├── 2703_1.txt │ │ ├── 2703_2.txt │ │ ├── 2703_3.txt │ │ ├── 2803.txt │ │ ├── 2803_1.txt │ │ ├── 2803_2.txt │ │ ├── 2803_3.txt │ │ ├── 2903.txt │ │ ├── 2903_1.txt │ │ ├── 2903_2.txt │ │ ├── 2903_3.txt │ │ ├── 3003.txt │ │ ├── 3003_1.txt │ │ ├── 3003_2.txt │ │ ├── 3003_3.txt │ │ ├── 3103.txt │ │ ├── 3103_1.txt │ │ ├── 3103_2.txt │ │ ├── output1903.txt │ │ ├── output2003.txt │ │ ├── output2103.txt │ │ ├── output2203.txt │ │ └── output2203_2.txt │ ├── overfit.txt │ ├── submit.py │ ├── submit2.py │ ├── table.txt │ ├── tablesprint.py │ ├── temp.txt │ ├── tester.py │ ├── tester.pyc │ ├── trace.txt │ └── traceImages │ │ ├── im10_1.png │ │ ├── im10_2.png │ │ ├── im10_3.png │ │ ├── im11_1.png │ │ ├── im11_2.png │ │ ├── im11_3.png │ │ ├── im12_1.png │ │ ├── im12_2.png │ │ ├── im12_3.png │ │ ├── im13_1.png │ │ ├── im13_2.png │ │ ├── im13_3.png │ │ ├── im14_1.png │ │ ├── im14_2.png │ │ ├── im14_3.png │ │ ├── im15_1.png │ │ ├── im15_2.png │ │ ├── im15_3.png │ │ ├── im1_1.png │ │ ├── im1_2.png │ │ ├── im1_3.png │ │ ├── im2_1.png │ │ ├── im2_2.png │ │ ├── im2_3.png │ │ ├── im3_1.png │ │ ├── im3_2.png │ │ ├── im3_3.png │ │ ├── im4_1.png │ │ ├── im4_2.png │ │ ├── im4_3.png │ │ ├── im5_1.png │ │ ├── im5_2.png │ │ ├── im5_3.png │ │ ├── im6_1.png │ │ ├── im6_2.png │ │ ├── im6_3.png │ │ ├── im7_1.png │ │ ├── im7_2.png │ │ ├── im7_3.png │ │ ├── im8_1.png │ │ ├── im8_2.png │ │ ├── im8_3.png │ │ ├── im9_1.png │ │ ├── im9_2.png │ │ └── im9_3.png └── submission │ ├── Report.pdf │ ├── src │ └── finalcode.py │ └── trace.txt ├── A4 ├── 2018101041.pdf ├── 2018101041.txt └── Readme.md ├── AssgnPDFs ├── Assignment1.pdf ├── Assignment_4.pdf ├── MDL assgn-2 part-1.pdf ├── MDL assgn-2 part-2.pdf └── MDL_Assignment_3 │ ├── MDL_Assignment_3.pdf │ ├── client.py │ └── overfit_weights.txt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ./A*/*.zip 2 | /zips -------------------------------------------------------------------------------- /A1/Q1_data/Images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/1.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/2.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/3.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/4.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/5.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/6.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/7.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/8.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/9.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/bias.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/bias.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/bullseye.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/bullseye.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/bvplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/bvplot.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/tableq1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/tableq1.png -------------------------------------------------------------------------------- /A1/Q1_data/Images/variance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Images/variance.png -------------------------------------------------------------------------------- /A1/Q1_data/README.md: -------------------------------------------------------------------------------- 1 | # COMPUTING BIAS AND VARIANCE 2 | 3 | The given data set contains a 5000 pairs ( xi , yi). 4 | The data is loaded using the pickle.load function and then shuffled once. 5 | The main idea is that to calculate the variance we need multiple realisations of the model, so there are multiple ways to resample the given dataset. 6 | The easiest among them is to randomly divide the train set into multiple subsets, so that we have 10 different realisation of the model. 7 | 8 | >## Bias 9 | Bias is the difference between the average prediction of our model and the actual value which we are trying to predict. Models that overgeneralise or extrapolate the information learnt are characterized by a low bias and high variance. 10 | 11 | ### Formula 12 | ![1](Images/bias.png) 13 | 14 | >## Variance 15 | Variance is the variability of the model prediction for a given data point. It is how much the predictions for a given point vary between different realizations of the model. 16 | 17 | ### Formula 18 | ![1](Images/variance.png) 19 | 20 | 21 | >## Bull's Eye Graphs for the same 22 | ![1](Images/bullseye.png) 23 | 24 | ## Brief Algorithm 25 | 26 | The entire data is then split as 90% training data and 10% testing data. Witihin the entire training data set, data is split into 10 data sets. 27 | 28 | * The data set partitions are trained for polynomial models with complexity ranging from x to x9 29 | 30 | * For each of the 10 data set partitions, the model is trained using that data set and the sklearn API : sklearn.linear_model.LinearRegression.fit() using the (x,y) pairs belongning to that partition of the training set. 31 | 32 | ```py 33 | poly = PolynomialFeatures(degree=degree, include_bias=False) 34 | #Transform the polynomial features as required 35 | X = poly.fit_transform(x_train[i]) 36 | X_TEST = poly.fit_transform(x_test) 37 | reg = LinearRegression() 38 | 39 | #Train the model for the chosen training set 40 | reg.fit(X, y_train[i]) 41 | y_predict = reg.predict(X_TEST) 42 | out[i]=y_predict 43 | ``` 44 | 45 | * Once the model is trained, a matrix with the prediction of the output values for the X_TEST set are obrained for each of the 10 training data set partitions (out[]). 46 | 47 | * For Bias : the mean value of the predictions for each data point from all the 10 training data partitions is calculated. Then the Bias formula is used to compute the bias for that particular complexity model 48 | 49 | In the code snippet given below, the out matrix is a 10 x 500 matrix containing all the predicted y values for the 10 models. 50 | point_mean[] is then a 1 x 500 matrix that calculates the mean of the bias for each point. 51 | 52 | Finally bias_mean[degree-1] computes the bias for that particular degree complexity model. 53 | 54 | ```py 55 | #calculate bias 56 | point_mean=np.mean(out,axis=0) 57 | bias_mean[degree-1]=np.mean((point_mean-y_test)**2) 58 | ``` 59 | 60 | * For variance : Likewise, the variance of the points in all the partitions for that model is computed and then the mean of the variances is calculated for that particular complexity model. 61 | In the code snippet given below, the out matrix is a 20 x 500 matrix containing all the predicted y values for the 20 models. 62 | Point var is then a 1 x 500 matrix that calculates the var of each point. 63 | 64 | Finally var_mean[degree-1] computes the mean of the variance for that particular degree complexity model. 65 | 66 | ```py 67 | #calculate variance 68 | point_var = np.var(out,axis=0) 69 | var_mean[degree-1]=np.mean(point_var) 70 | ``` 71 | 72 | * The same procedure is now repeated for models of different complexities (ranging from degree 1 to degree 9). The corresponding values of Bias and Variance for each model are then tabulated. 73 | 74 | ## Tabulated Values 75 | 76 | ### Table1 77 | ![1](Images/tableq1.png) 78 | 79 | ## Plots obtained for a particular training model 80 | 81 | ### Degree 1 82 | ![1](Images/1.png) 83 | ### Degree 2 84 | ![2](Images/2.png) 85 | ### Degree 3 86 | ![3](Images/3.png) 87 | ### Degree 4 88 | ![4](Images/4.png) 89 | ### Degree 5 90 | ![5](Images/5.png) 91 | ### Degree 6 92 | ![6](Images/6.png) 93 | ### Degree 7 94 | ![7](Images/7.png) 95 | ### Degree 8 96 | ![8](Images/8.png) 97 | ### Degree 9 98 | ![9](Images/9.png) 99 | 100 | 101 | ## Bias Variance Tradeoff 102 | 103 | >Here the variance is scaled by a factor of 100 : ie variance = 100 * variance 104 | 105 | ![1](Images/bvplot.png) 106 | 107 | ## Observations 108 | From the above graphs and tabulated values, we observe that with an increase in the complexity of the model the bias decreases and variance increases. 109 | 110 | * That is with increase in the number of features, or complexity/flexibility of the model (essentially increasing the degree of the model to best fit the training data) the model extracts more information from the training sets and works well with them. This is characterised by a low bias with increase in model complexity. 111 | However at the same time it will not help us generalize data and derive patterns from them. Thus the model may perform poorly on data sets that have not been seen before. This is reflected in the increase in variance with increase in complexity. 112 | 113 | * Furthermore for lower degree polynomial models, we observe a high bias but low variance. The reason being, the model may not be able to perform well even on existing training data since the lower degree polynomials are unable to capture all features of the training data. Yet the variance is high, since the model is consistently performing poorly. 114 | -------------------------------------------------------------------------------- /A1/Q1_data/Report1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/Report1.pdf -------------------------------------------------------------------------------- /A1/Q1_data/data.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q1_data/data.pkl -------------------------------------------------------------------------------- /A1/Q1_data/q1.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn.preprocessing import PolynomialFeatures 5 | from sklearn.linear_model import LinearRegression 6 | from sklearn.pipeline import Pipeline 7 | from sklearn.metrics import mean_squared_error, r2_score 8 | from sklearn.model_selection import train_test_split 9 | import pandas as pd 10 | 11 | 12 | with open('./data.pkl', 'rb') as f: 13 | data = pickle.load(f) 14 | 15 | # data stores all the data that is present 16 | # x_train stores all the training data available 17 | # x_test stores all the testing data available 18 | 19 | size = data.shape[0] 20 | np.random.shuffle(data) 21 | 22 | x=data[:,:-1] 23 | y=data[:,1] 24 | 25 | x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=1/10, random_state=0) 26 | 27 | x_train=np.array((np.array_split(x_train, 10))) 28 | y_train=np.array((np.array_split(y_train, 10))) 29 | 30 | v_table=np.zeros((10,10)) 31 | b_table=np.zeros((10,10)) 32 | 33 | bias_mean=np.zeros((9)) 34 | var_mean=np.zeros((9)) 35 | 36 | #For the polynomial degrees 37 | for degree in range (1,10): 38 | bias_sq=np.zeros((10,500)) 39 | var=np.zeros((10,500)) 40 | out=np.zeros((10,500)) 41 | #For the training set 42 | for i in range (10): 43 | poly = PolynomialFeatures(degree=degree, include_bias=False) 44 | 45 | #Transform the polynomial features as required 46 | X = poly.fit_transform(x_train[i]) 47 | X_TEST = poly.fit_transform(x_test) 48 | reg = LinearRegression() 49 | 50 | #Train the model for the chosen training set 51 | reg.fit(X, y_train[i]) 52 | y_predict = reg.predict(X_TEST) 53 | 54 | # plot.scatter(x_train[i], y_train[i], color = 'red') 55 | # plot.scatter(x_train[i], reg.predict(X), color = 'blue') 56 | # plot.show() 57 | 58 | out[i]=y_predict 59 | 60 | #calculate bias 61 | point_mean=np.mean(out,axis=0) 62 | bias_mean[degree-1]=np.mean((point_mean-y_test)**2) 63 | 64 | #calculate variance 65 | point_var = np.var(out,axis=0) 66 | var_mean[degree-1]=np.mean(point_var) 67 | 68 | 69 | #printing the tabulated values 70 | table_bias=pd.DataFrame({'Degree':np.array(range(1,10)),'Bias^2':bias_mean,'Variance': var_mean, 'Variance*100':var_mean[:]*100}) 71 | print(table_bias.to_string(index=False)) 72 | 73 | plot.plot(bias_mean,label='Bias^2', color = 'blue') 74 | plot.plot(var_mean[:]*100,label='Variance * 100', color = 'red') 75 | plot.xlabel('Model Complexity', fontsize='medium') 76 | plot.ylabel('Error', fontsize='medium') 77 | plot.title("Bias vs Variance") 78 | plot.legend() 79 | # plot.show() 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /A1/Q1_data/q1temp.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn import datasets, linear_model 5 | from sklearn.metrics import mean_squared_error, r2_score 6 | 7 | 8 | with open('./data.pkl', 'rb') as f: 9 | data = pickle.load(f) 10 | 11 | # print(data) data stores all the data that is present 12 | # train stores all the training data available 13 | # test stores all the testing data available 14 | 15 | size = data.shape[0] 16 | np.random.shuffle(data) 17 | 18 | x = int(0.9*size) # x entries for training 90% 19 | y = int(0.1*size) # y entries for testing 10% 20 | 21 | train=data[0:x,:] 22 | test=data[x:size+1,:] 23 | np.random.shuffle(test) 24 | 25 | k=0 26 | temp=[] 27 | tempx=[] 28 | tempy=[] 29 | 30 | for i in range (10): 31 | temp.append(train[k:k+9,:]) 32 | tempx.append(train[k:k+9,0]) 33 | tempy.append(train[k:k+9,1]) 34 | k+=9 35 | 36 | f_train=np.array(temp) 37 | x_train=np.array(tempx) 38 | y_train=np.array(tempy) 39 | 40 | print(x_train) 41 | print(f_train[0]) 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /A1/Q1_data/trial.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn.preprocessing import PolynomialFeatures 5 | from sklearn.linear_model import LinearRegression 6 | from sklearn.pipeline import Pipeline 7 | from sklearn.metrics import mean_squared_error, r2_score 8 | from sklearn.model_selection import train_test_split 9 | import pandas as pd 10 | 11 | 12 | with open('./data.pkl', 'rb') as f: 13 | data = pickle.load(f) 14 | 15 | # data stores all the data that is present 16 | # train stores all the training data available 17 | # test stores all the testing data available 18 | 19 | size = data.shape[0] 20 | np.random.shuffle(data) 21 | 22 | x=data[:,:-1] 23 | y=data[:,1] 24 | 25 | x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=1/10, random_state=0) 26 | 27 | k=0 28 | temp=[] 29 | tempx=[] 30 | tempy=[] 31 | 32 | for i in range (10): 33 | tempx.append(x_train[k:k+450]) 34 | tempy.append(y_train[k:k+450]) 35 | k+=450 36 | 37 | x_train=np.array(tempx) 38 | y_train=np.array(tempy) 39 | 40 | def func(x): 41 | x = x.ravel() 42 | 43 | return np.exp(-x ** 2) + 1.5 * np.exp(-(x - 2) ** 2) 44 | 45 | 46 | v_table=np.zeros((10,10)) 47 | b_table=np.zeros((10,10)) 48 | 49 | bias_mean=np.zeros((9)) 50 | var_mean=np.zeros((9)) 51 | 52 | #For the polynomial degrees 53 | for degree in range (1,10): 54 | bias_sq=np.zeros((10,500)) 55 | var=np.zeros((10,500)) 56 | #For the training set 57 | for i in range (10): 58 | poly = PolynomialFeatures(degree=degree, include_bias=False) 59 | 60 | #Transform the pilynomial features as required 61 | X = poly.fit_transform(x_train[i]) 62 | X_TEST = poly.fit_transform(x_test) 63 | reg = LinearRegression() 64 | #Train the model for the chosen training set 65 | reg.fit(X, y_train[i]) 66 | y_predict = reg.predict(X_TEST) 67 | 68 | # plot.scatter(x_train[i], y_train[i], color = 'red') 69 | # plot.scatter(x_train[i], reg.predict(X), color = 'blue') 70 | bias_sq[i]=((np.mean(y_predict) - y_test) ** 2) 71 | var[i]=(np.var(y_predict, axis=0)) 72 | 73 | point_mean = np.mean(bias_sq,axis=0) 74 | bias_mean[degree-1]=np.mean(point_mean) 75 | point_var_mean = np.mean(var,axis=0) 76 | var_mean[degree-1]=np.mean(point_var_mean) 77 | 78 | # print(pd.DataFrame(var_mean)) 79 | # print(pd.DataFrame(bias_mean)) 80 | 81 | print(var_mean) 82 | print(bias_mean) 83 | 84 | 85 | plot.plot(bias_mean,'b',label='Bias^2') 86 | # plot.plot(var_mean,'r',label='Variance') 87 | plot.xlabel('Complexity', fontsize='medium') 88 | plot.ylabel('Error', fontsize='medium') 89 | plot.title("Bias vs Variance") 90 | plot.legend() 91 | plot.show() 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /A1/Q2_data/Fx_test.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Fx_test.pkl -------------------------------------------------------------------------------- /A1/Q2_data/Images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/1.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/2.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/3.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/4.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/5.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/6.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/7.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/8.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/9.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/bias.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/bias.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/bullseye.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/bullseye.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/bvplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/bvplot.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/tableq2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/tableq2.png -------------------------------------------------------------------------------- /A1/Q2_data/Images/variance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Images/variance.png -------------------------------------------------------------------------------- /A1/Q2_data/README.md: -------------------------------------------------------------------------------- 1 | # COMPUTING BIAS AND VARIANCE 2 | 3 | The given data set contains a 20 subsets of training data containing 400 samples each. 4 | The data is loaded using the pickle.load function. 5 | For each polynomial complexity, 20 models are trained on 20 different subsets. From this we obtain the bias and the variance of the predictions on the testing data 6 | 7 | >## Bias 8 | Bias is the difference between the average prediction of our model and the actual value which we are trying to predict. Models that overgeneralise or extrapolate the information learnt are characterized by a low bias and high variance. 9 | 10 | ### Formula 11 | ![1](Images/bias.png) 12 | 13 | >## Variance 14 | Variance is the variability of the model prediction for a given data point. It is how much the predictions for a given point vary between different realizations of the model. 15 | 16 | ### Formula 17 | ![1](Images/variance.png) 18 | 19 | >## Bull's Eye Graphs for the same 20 | ![1](Images/bullseye.png) 21 | 22 | 23 | ## Brief Algorithm 24 | 25 | All the 4 data files are loaded using pickle.load() : namely Fox_test, x_test, y_train, x_train 26 | 27 | * The data set partitions are trained for polynomial models with complexity ranging from x to x9 28 | 29 | * For each of the 20 data set partitions, the model is trained using that data set and the sklearn API : sklearn.linear_model.LinearRegression.fit() using the (x,y) pairs belongning to that partition of the training set. 30 | 31 | ```py 32 | poly = PolynomialFeatures(degree=degree, include_bias=False) 33 | #Transform the polynomial features as required 34 | X = poly.fit_transform(x_train[i]) 35 | X_TEST = poly.fit_transform(x_test) 36 | reg = LinearRegression() 37 | 38 | #Train the model for the chosen training set 39 | reg.fit(X, y_train[i]) 40 | y_predict = reg.predict(X_TEST) 41 | ``` 42 | 43 | * Once the model is trained, a matrix with the prediction of the output values for the X_TEST set are obrained for each of the 20 training data set partitions. 44 | 45 | * For Bias : the mean value of the predictions for each data point from all the 20 training data partitions is calculated. Then the Bias formula is used to compute the bias for that particular complexity model ... insert formula 46 | 47 | In the code snippet given below, the out matrix is a 20 x 500 matrix containing all the predicted y values for the 20 models. 48 | Point mean is then a 1 x 500 matrix that calculates the mean of the bias for each point. 49 | 50 | Finally bias_mean[degree-1] computes the bias for that particular degree complexity model. 51 | 52 | ```py 53 | #calculate bias 54 | point_mean=np.mean(out,axis=0) 55 | bias_mean[degree-1]=np.mean((point_mean-y_test)**2) 56 | ``` 57 | 58 | * For variance : Likewise, the variance of the points in all the partitions for that model is computed and then the mean of the variances is calculated for that particular complexity model. 59 | 60 | In the code snippet given below, the out matrix is a 20 x 500 matrix containing all the predicted y values for the 20 models. 61 | Point var is then a 1 x 500 matrix that calculates the var of each point. 62 | 63 | Finally var_mean[degree-1] computes the mean of the variance for that particular degree complexity model. 64 | 65 | ```py 66 | #calculate variance 67 | point_var = np.var(out,axis=0) 68 | var_mean[degree-1]=np.mean(point_var) 69 | ``` 70 | 71 | * The same procedure is now repeated for models of different complexities (ranging from degree 1 to degree 9). The corresponding values of Bias and Variance for each model are then tabulated. 72 | 73 | ## Tabulated Values 74 | 75 | ### Table 76 | ![1](Images/tableq2.png) 77 | 78 | ## Plots obtained for a particular training model 79 | 80 | ### Degree 1 81 | ![1](Images/1.png) 82 | ### Degree 2 83 | ![2](Images/2.png) 84 | ### Degree 3 85 | ![3](Images/3.png) 86 | ### Degree 4 87 | ![4](Images/4.png) 88 | ### Degree 5 89 | ![5](Images/5.png) 90 | ### Degree 6 91 | ![6](Images/6.png) 92 | ### Degree 7 93 | ![7](Images/7.png) 94 | ### Degree 8 95 | ![8](Images/8.png) 96 | ### Degree 9 97 | ![9](Images/9.png) 98 | 99 | ## Bias-Variance Trade-Off Plot obtained 100 | 101 | ![1](Images/bvplot.png) 102 | 103 | 104 | ## Observations 105 | From the above graphs and tabulated values, we observe that with an increase in the complexity of the model the bias decreases and variance increases. 106 | 107 | * Overfitting : 108 | The phenomenon of memorization can cause overfitting. 109 | That is with increase in the number of features, or complexity/flexibility of the model (essentially increasing the degree of the model to best fit the training data) the model extracts more information from the training sets and works well with them. However at the same time it will not help us generalize data and derive patterns from them. Thus the model may perform poorly on data sets that have not been seen before. This is reflected in the increase in variance with increase in complexity. Thus the model is said to be overfitting. 110 | Higher degree polynomials can usually be overfitting. 111 | Overfitting occurs when we try to describe the learning rules based on a relatively small number of observations. 112 | 113 | * Underfitting: 114 | When a model is underfit, it does not perform well on the training sets and does not perform well on the test sets either. That is it fails to capture the underlying trend of the date. Furthermore for lower degree polynomial models, we observe a high bias but low variance. The reason being, the model may not be able to perform well even on existing training data since the lower degree polynomials are unable to capture all features of the training data. Yet the variance is high, since the model is consistently performing poorly. Therefore lower degree polynomials can usually be underfitting. 115 | -------------------------------------------------------------------------------- /A1/Q2_data/Report2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Report2.pdf -------------------------------------------------------------------------------- /A1/Q2_data/X_test.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/X_test.pkl -------------------------------------------------------------------------------- /A1/Q2_data/X_train.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/X_train.pkl -------------------------------------------------------------------------------- /A1/Q2_data/Y_train.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/Q2_data/Y_train.pkl -------------------------------------------------------------------------------- /A1/Q2_data/q2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn.preprocessing import PolynomialFeatures 5 | from sklearn.linear_model import LinearRegression 6 | from sklearn.pipeline import Pipeline 7 | from sklearn.metrics import mean_squared_error, r2_score 8 | from sklearn.model_selection import train_test_split 9 | import pandas as pd 10 | 11 | 12 | with open('./Fx_test.pkl', 'rb') as f: 13 | Fox_test = pickle.load(f) #Testing set for f(x) 14 | with open('./X_test.pkl', 'rb') as f: 15 | x_test = pickle.load(f) #Testing set for x 16 | with open('./X_train.pkl', 'rb') as f: 17 | x_train = pickle.load(f) #Training set for x 18 | with open('./Y_train.pkl', 'rb') as f: 19 | y_train = pickle.load(f) #Training set for y 20 | 21 | x_test=x_test.reshape(-1,1) 22 | 23 | v_table=np.zeros((10,10)) 24 | b_table=np.zeros((10,10)) 25 | 26 | bias_mean=np.zeros((9)) 27 | var_mean=np.zeros((9)) 28 | 29 | #For the polynomial degrees 30 | for degree in range (1,10): 31 | bias_sq=np.zeros((20,80)) 32 | var=np.zeros((20,80)) 33 | out=np.zeros((20,80)) 34 | #For the training set 35 | for i in range (20): 36 | poly = PolynomialFeatures(degree=degree, include_bias=False) 37 | 38 | #Transform the polynomial features as required 39 | X = poly.fit_transform(x_train[i].reshape(-1,1)) 40 | X_TEST = poly.fit_transform(x_test) 41 | reg = LinearRegression() 42 | 43 | #Train the model for the chosen training set 44 | reg.fit(X, y_train[i]) 45 | y_predict = reg.predict(X_TEST) 46 | if(i==1): 47 | plot.scatter(x_train[i], y_train[i], color = 'red') 48 | plot.scatter(x_train[i], reg.predict(X), color = 'blue') 49 | plot.show() 50 | out[i]=y_predict 51 | 52 | #calculate bias 53 | point_mean = np.mean(out,axis=0) 54 | bias_mean[degree-1]=np.mean((point_mean-Fox_test)**2) 55 | 56 | #calculate variance 57 | point_var = np.var(out,axis=0) 58 | var_mean[degree-1]=np.mean(point_var) 59 | 60 | 61 | table_bias=pd.DataFrame({'Degree':np.array(range(1,10)),'Bias^2':bias_mean,'Variance': var_mean}) 62 | print(table_bias.to_string(index=False)) 63 | 64 | plot.plot(bias_mean,'b',label='Bias^2') 65 | plot.plot(var_mean,'r',label='Variance') 66 | plot.xlabel('Complexity', fontsize='medium') 67 | plot.ylabel('Error', fontsize='medium') 68 | plot.title("Bias vs Variance") 69 | plot.legend() 70 | plot.show() -------------------------------------------------------------------------------- /A1/README.md: -------------------------------------------------------------------------------- 1 | # Assignment 1 : Bias Variance Tradeoff 2 | 3 | In this assigment we calculate the bias and variance of your trained model. 4 | 5 | ## Task 1 6 | The dataset consists of pairs *(xi , yi)*. It is loaded into the python program using the pickle.load() function. The dataset is split into training and testing ```(90:10 split)```. We then divide the train set into 10 equal parts randomly, so that we get 10 different dataset to train our model. 7 | 8 | After resampling, we now have 11 different datasets. We train a linear classifier on each of the 10 train set separately, so that we 9 | have 10 different classifiers or models. Once we have 10 different models or classifiers trained separately on 10 different training set, we calculate the bias and variance of the model. 10 | 11 | 12 | ## Task 2 13 | 14 | Given a set of training data and a testing data we try to fit the given data to polynomials of degree 1 to 9(both inclusive) using the ```sklearn.linear_model.LinearRegression().fit()``` library. 15 | 16 | Given 20 subsets of training data containing 400 samples each, for each polynomial, we create 20 models trained on the 20 different 17 | subsets and find the variance of the predictions on the testing data. Further we find the bias of the trained models on the testing data. Finally the bias-variance trade-Off graphs are plotted. -------------------------------------------------------------------------------- /A1/submission/Q1/Images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/1.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/2.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/3.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/4.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/5.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/6.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/7.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/8.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/9.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/bias.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/bias.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/bullseye.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/bullseye.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/bvplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/bvplot.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/tableq1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/tableq1.png -------------------------------------------------------------------------------- /A1/submission/Q1/Images/variance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Images/variance.png -------------------------------------------------------------------------------- /A1/submission/Q1/Report1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q1/Report1.pdf -------------------------------------------------------------------------------- /A1/submission/Q1/q1.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn.preprocessing import PolynomialFeatures 5 | from sklearn.linear_model import LinearRegression 6 | from sklearn.pipeline import Pipeline 7 | from sklearn.metrics import mean_squared_error, r2_score 8 | from sklearn.model_selection import train_test_split 9 | import pandas as pd 10 | 11 | 12 | with open('./data.pkl', 'rb') as f: 13 | data = pickle.load(f) 14 | 15 | # data stores all the data that is present 16 | # x_train stores all the training data available 17 | # x_test stores all the testing data available 18 | 19 | size = data.shape[0] 20 | np.random.shuffle(data) 21 | 22 | x=data[:,:-1] 23 | y=data[:,1] 24 | 25 | x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=1/10, random_state=0) 26 | 27 | x_train=np.array((np.array_split(x_train, 10))) 28 | y_train=np.array((np.array_split(y_train, 10))) 29 | 30 | v_table=np.zeros((10,10)) 31 | b_table=np.zeros((10,10)) 32 | 33 | bias_mean=np.zeros((9)) 34 | var_mean=np.zeros((9)) 35 | 36 | #For the polynomial degrees 37 | for degree in range (1,10): 38 | bias_sq=np.zeros((10,500)) 39 | var=np.zeros((10,500)) 40 | out=np.zeros((10,500)) 41 | #For the training set 42 | for i in range (10): 43 | poly = PolynomialFeatures(degree=degree, include_bias=False) 44 | 45 | #Transform the polynomial features as required 46 | X = poly.fit_transform(x_train[i]) 47 | X_TEST = poly.fit_transform(x_test) 48 | reg = LinearRegression() 49 | 50 | #Train the model for the chosen training set 51 | reg.fit(X, y_train[i]) 52 | y_predict = reg.predict(X_TEST) 53 | 54 | # plot.scatter(x_train[i], y_train[i], color = 'red') 55 | # plot.scatter(x_train[i], reg.predict(X), color = 'blue') 56 | # plot.show() 57 | 58 | out[i]=y_predict 59 | 60 | #calculate bias 61 | point_mean=np.mean(out,axis=0) 62 | bias_mean[degree-1]=np.mean((point_mean-y_test)**2) 63 | 64 | #calculate variance 65 | point_var = np.var(out,axis=0) 66 | var_mean[degree-1]=np.mean(point_var) 67 | 68 | 69 | #printing the tabulated values 70 | table_bias=pd.DataFrame({'Degree':np.array(range(1,10)),'Bias^2':bias_mean,'Variance': var_mean, 'Variance*100':var_mean[:]*100}) 71 | print(table_bias.to_string(index=False)) 72 | 73 | plot.plot(bias_mean,label='Bias^2', color = 'blue') 74 | plot.plot(var_mean[:]*100,label='Variance * 100', color = 'red') 75 | plot.xlabel('Model Complexity', fontsize='medium') 76 | plot.ylabel('Error', fontsize='medium') 77 | plot.title("Bias vs Variance") 78 | plot.legend() 79 | # plot.show() 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /A1/submission/Q2/Images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/1.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/2.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/3.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/4.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/5.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/6.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/7.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/8.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/9.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/bias.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/bias.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/bullseye.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/bullseye.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/bvplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/bvplot.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/tableq2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/tableq2.png -------------------------------------------------------------------------------- /A1/submission/Q2/Images/variance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Images/variance.png -------------------------------------------------------------------------------- /A1/submission/Q2/Report2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A1/submission/Q2/Report2.pdf -------------------------------------------------------------------------------- /A1/submission/Q2/q2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import matplotlib.pyplot as plot 4 | from sklearn.preprocessing import PolynomialFeatures 5 | from sklearn.linear_model import LinearRegression 6 | from sklearn.pipeline import Pipeline 7 | from sklearn.metrics import mean_squared_error, r2_score 8 | from sklearn.model_selection import train_test_split 9 | import pandas as pd 10 | 11 | 12 | with open('./Fx_test.pkl', 'rb') as f: 13 | Fox_test = pickle.load(f) #Testing set for f(x) 14 | with open('./X_test.pkl', 'rb') as f: 15 | x_test = pickle.load(f) #Testing set for x 16 | with open('./X_train.pkl', 'rb') as f: 17 | x_train = pickle.load(f) #Training set for x 18 | with open('./Y_train.pkl', 'rb') as f: 19 | y_train = pickle.load(f) #Training set for y 20 | 21 | x_test=x_test.reshape(-1,1) 22 | 23 | v_table=np.zeros((10,10)) 24 | b_table=np.zeros((10,10)) 25 | 26 | bias_mean=np.zeros((9)) 27 | var_mean=np.zeros((9)) 28 | 29 | #For the polynomial degrees 30 | for degree in range (1,10): 31 | bias_sq=np.zeros((20,80)) 32 | var=np.zeros((20,80)) 33 | out=np.zeros((20,80)) 34 | #For the training set 35 | for i in range (20): 36 | poly = PolynomialFeatures(degree=degree, include_bias=False) 37 | 38 | #Transform the polynomial features as required 39 | X = poly.fit_transform(x_train[i].reshape(-1,1)) 40 | X_TEST = poly.fit_transform(x_test) 41 | reg = LinearRegression() 42 | 43 | #Train the model for the chosen training set 44 | reg.fit(X, y_train[i]) 45 | y_predict = reg.predict(X_TEST) 46 | if(i==1): 47 | plot.scatter(x_train[i], y_train[i], color = 'red') 48 | plot.scatter(x_train[i], reg.predict(X), color = 'blue') 49 | plot.show() 50 | out[i]=y_predict 51 | 52 | #calculate bias 53 | point_mean = np.mean(out,axis=0) 54 | bias_mean[degree-1]=np.mean((point_mean-Fox_test)**2) 55 | 56 | #calculate variance 57 | point_var = np.var(out,axis=0) 58 | var_mean[degree-1]=np.mean(point_var) 59 | 60 | 61 | table_bias=pd.DataFrame({'Degree':np.array(range(1,10)),'Bias^2':bias_mean,'Variance': var_mean}) 62 | print(table_bias.to_string(index=False)) 63 | 64 | plot.plot(bias_mean,'b',label='Bias^2') 65 | plot.plot(var_mean,'r',label='Variance') 66 | plot.xlabel('Complexity', fontsize='medium') 67 | plot.ylabel('Error', fontsize='medium') 68 | plot.title("Bias vs Variance") 69 | plot.legend() 70 | plot.show() -------------------------------------------------------------------------------- /A2-2/README.md: -------------------------------------------------------------------------------- 1 | # Value Iteration Algorithm 2 | 3 | The Bellman equation is the basis of the value iteration algorithm for solving MDPs. 4 | 5 | Let Ut(I) be the utility value for state s at the t'th iteration. The iteration 6 | step, called a Bellman update, looks like this: 7 | 8 | ![Bellman](images/Bellman.png) 9 | 10 | Where the Rewards R(I,A) for each state is the expected reward of taking action A in State I. That is :- 11 | 12 | ![Riaj](images/Riaj.png) 13 | 14 | The value iteration algorithm is as follows :- 15 | 16 | ![vi](images/vi.png) 17 | 18 | 19 | # TASK 1 20 | 21 | > Step Costs : [-5,-5,-5] 22 | 23 | > Gamma : 0.99 24 | 25 | > Delta : 0.001 26 | 27 | > Iterations : 112 28 | 29 | From the task_1_trace.txt file obtained, we can make the following inferences about the policy : 30 | 31 | * Whenever Lero has 0 stamina, the only optimal policy for that state is to RECHARGE 32 | 33 | * Whenever Lero has 0 arrows, he can RECHARGE or DODGE but cannot SHOOT 34 | 35 | * Most of the times, Lero is Risk Averse. That is he prefers to RECHARGE than to SHOOT in cases when his stamina = 50 instead of losing an arrow. 36 | 37 | # TASK 2 38 | 39 | ## PART 1 40 | 41 | > Step Costs : [-0.25,-2.5,-2.5] 42 | 43 | > Gamma : 0.99 44 | 45 | > Delta : 0.001 46 | 47 | > Iterations : 100 48 | 49 | From the task_2_part_1_trace.txt file obtained we can make the following inferences about the changes in the policy : 50 | 51 | * Since the step cost for the SHOOT action is less negative compared to the other actions, Lero now becomes Risk Seeking and shoots whenever possible. 52 | 53 | * Convergence is faster. In only 100 iterations, the value iteration algorithm converges when compared to the 112 iterations in TASK 1 54 | 55 | * Magnitude of the utilities are less negative in this case compared to TASK 1 because of the less negative step cost. 56 | 57 | ## PART 2 58 | 59 | > Step Costs : [-2.5,-2.5,-2.5] 60 | 61 | > Gamma : 0.1 62 | 63 | > Delta : 0.001 64 | 65 | > Iterations : 5 66 | 67 | From the task_2_part_2_trace.txt file obtained we can make the following inferences about the policy : 68 | 69 | * All states converge with a policy in the final iteration with more or less same utilities. 70 | 71 | * The RECHARGE policy is preferred over the others hence indicating a Risk Averse behaviour. 72 | 73 | * Bellman update is a contraction by a factor of γ on the space of utility vectors. There is fast convergence if we make γ small, but this effectively gives the agent a short horizon and could miss the long-term effects of the agent’s actions. So we can see that, Lero tries to play it safe and keeps RECHARGING instead of looking into the future. 74 | 75 | * Due to a very small discount factor (Gamma), the bellman iterations converge fast, within 4 iterations. 76 | 77 | ## PART 3 78 | 79 | > Step Costs : [-2.5,-2.5,-2.5] 80 | 81 | > Gamma : 0.1 82 | 83 | > Delta : 0.0000000001 84 | 85 | > Iterations : 12 86 | 87 | From the task_2_part_3_trace.txt file obtained we can make the following inferences about the policy : 88 | 89 | * The only difference between part 2 and part 3 is delta. 90 | 91 | * Since the Bellman error is very small we would require more iterations to get a higher accuracy. 92 | Clearly the number of iterations in part 3 are greater than the number of iterations in part 2. 93 | 94 | * Lero appears to be risk neutral according to the policy obtained. -------------------------------------------------------------------------------- /A2-2/images/Bellman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A2-2/images/Bellman.png -------------------------------------------------------------------------------- /A2-2/images/Riaj.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A2-2/images/Riaj.png -------------------------------------------------------------------------------- /A2-2/images/vi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A2-2/images/vi.png -------------------------------------------------------------------------------- /A2-2/submission/outputs/task_2_part_2_trace.txt: -------------------------------------------------------------------------------- 1 | iteration=0 2 | (0,0,0):-1=[0.0] 3 | (0,0,1):-1=[0.0] 4 | (0,0,2):-1=[0.0] 5 | (0,1,0):-1=[0.0] 6 | (0,1,1):-1=[0.0] 7 | (0,1,2):-1=[0.0] 8 | (0,2,0):-1=[0.0] 9 | (0,2,1):-1=[0.0] 10 | (0,2,2):-1=[0.0] 11 | (0,3,0):-1=[0.0] 12 | (0,3,1):-1=[0.0] 13 | (0,3,2):-1=[0.0] 14 | (1,0,0):RECHARGE=[-2.5] 15 | (1,0,1):RECHARGE=[-2.5] 16 | (1,0,2):DODGE=[-2.5] 17 | (1,1,0):RECHARGE=[-2.5] 18 | (1,1,1):SHOOT=[2.5] 19 | (1,1,2):SHOOT=[2.5] 20 | (1,2,0):RECHARGE=[-2.5] 21 | (1,2,1):SHOOT=[2.5] 22 | (1,2,2):SHOOT=[2.5] 23 | (1,3,0):RECHARGE=[-2.5] 24 | (1,3,1):SHOOT=[2.5] 25 | (1,3,2):SHOOT=[2.5] 26 | (2,0,0):RECHARGE=[-2.5] 27 | (2,0,1):RECHARGE=[-2.5] 28 | (2,0,2):DODGE=[-2.5] 29 | (2,1,0):RECHARGE=[-2.5] 30 | (2,1,1):RECHARGE=[-2.5] 31 | (2,1,2):DODGE=[-2.5] 32 | (2,2,0):RECHARGE=[-2.5] 33 | (2,2,1):RECHARGE=[-2.5] 34 | (2,2,2):SHOOT=[-2.5] 35 | (2,3,0):RECHARGE=[-2.5] 36 | (2,3,1):RECHARGE=[-2.5] 37 | (2,3,2):SHOOT=[-2.5] 38 | (3,0,0):RECHARGE=[-2.5] 39 | (3,0,1):RECHARGE=[-2.5] 40 | (3,0,2):DODGE=[-2.5] 41 | (3,1,0):RECHARGE=[-2.5] 42 | (3,1,1):RECHARGE=[-2.5] 43 | (3,1,2):DODGE=[-2.5] 44 | (3,2,0):RECHARGE=[-2.5] 45 | (3,2,1):RECHARGE=[-2.5] 46 | (3,2,2):DODGE=[-2.5] 47 | (3,3,0):RECHARGE=[-2.5] 48 | (3,3,1):RECHARGE=[-2.5] 49 | (3,3,2):DODGE=[-2.5] 50 | (4,0,0):RECHARGE=[-2.5] 51 | (4,0,1):RECHARGE=[-2.5] 52 | (4,0,2):DODGE=[-2.5] 53 | (4,1,0):RECHARGE=[-2.5] 54 | (4,1,1):RECHARGE=[-2.5] 55 | (4,1,2):DODGE=[-2.5] 56 | (4,2,0):RECHARGE=[-2.5] 57 | (4,2,1):RECHARGE=[-2.5] 58 | (4,2,2):DODGE=[-2.5] 59 | (4,3,0):RECHARGE=[-2.5] 60 | (4,3,1):RECHARGE=[-2.5] 61 | (4,3,2):DODGE=[-2.5] 62 | 63 | 64 | iteration=1 65 | (0,0,0):-1=[0.0] 66 | (0,0,1):-1=[0.0] 67 | (0,0,2):-1=[0.0] 68 | (0,1,0):-1=[0.0] 69 | (0,1,1):-1=[0.0] 70 | (0,1,2):-1=[0.0] 71 | (0,2,0):-1=[0.0] 72 | (0,2,1):-1=[0.0] 73 | (0,2,2):-1=[0.0] 74 | (0,3,0):-1=[0.0] 75 | (0,3,1):-1=[0.0] 76 | (0,3,2):-1=[0.0] 77 | (1,0,0):RECHARGE=[-2.75] 78 | (1,0,1):DODGE=[-2.75] 79 | (1,0,2):DODGE=[-2.43] 80 | (1,1,0):RECHARGE=[-2.35] 81 | (1,1,1):SHOOT=[2.375] 82 | (1,1,2):SHOOT=[2.375] 83 | (1,2,0):RECHARGE=[-2.35] 84 | (1,2,1):SHOOT=[2.375] 85 | (1,2,2):SHOOT=[2.625] 86 | (1,3,0):RECHARGE=[-2.35] 87 | (1,3,1):SHOOT=[2.375] 88 | (1,3,2):SHOOT=[2.625] 89 | (2,0,0):RECHARGE=[-2.75] 90 | (2,0,1):RECHARGE=[-2.75] 91 | (2,0,2):DODGE=[-2.75] 92 | (2,1,0):RECHARGE=[-2.75] 93 | (2,1,1):SHOOT=[-2.75] 94 | (2,1,2):SHOOT=[-2.75] 95 | (2,2,0):RECHARGE=[-2.75] 96 | (2,2,1):SHOOT=[-2.75] 97 | (2,2,2):SHOOT=[-2.5] 98 | (2,3,0):RECHARGE=[-2.75] 99 | (2,3,1):SHOOT=[-2.75] 100 | (2,3,2):SHOOT=[-2.5] 101 | (3,0,0):RECHARGE=[-2.75] 102 | (3,0,1):RECHARGE=[-2.75] 103 | (3,0,2):DODGE=[-2.75] 104 | (3,1,0):RECHARGE=[-2.75] 105 | (3,1,1):SHOOT=[-2.75] 106 | (3,1,2):SHOOT=[-2.75] 107 | (3,2,0):RECHARGE=[-2.75] 108 | (3,2,1):SHOOT=[-2.75] 109 | (3,2,2):SHOOT=[-2.75] 110 | (3,3,0):RECHARGE=[-2.75] 111 | (3,3,1):DODGE=[-2.75] 112 | (3,3,2):SHOOT=[-2.75] 113 | (4,0,0):RECHARGE=[-2.75] 114 | (4,0,1):RECHARGE=[-2.75] 115 | (4,0,2):DODGE=[-2.75] 116 | (4,1,0):RECHARGE=[-2.75] 117 | (4,1,1):SHOOT=[-2.75] 118 | (4,1,2):SHOOT=[-2.75] 119 | (4,2,0):RECHARGE=[-2.75] 120 | (4,2,1):SHOOT=[-2.75] 121 | (4,2,2):SHOOT=[-2.75] 122 | (4,3,0):RECHARGE=[-2.75] 123 | (4,3,1):DODGE=[-2.75] 124 | (4,3,2):SHOOT=[-2.75] 125 | 126 | 127 | iteration=2 128 | (0,0,0):-1=[0.0] 129 | (0,0,1):-1=[0.0] 130 | (0,0,2):-1=[0.0] 131 | (0,1,0):-1=[0.0] 132 | (0,1,1):-1=[0.0] 133 | (0,1,2):-1=[0.0] 134 | (0,2,0):-1=[0.0] 135 | (0,2,1):-1=[0.0] 136 | (0,2,2):-1=[0.0] 137 | (0,3,0):-1=[0.0] 138 | (0,3,1):-1=[0.0] 139 | (0,3,2):-1=[0.0] 140 | (1,0,0):RECHARGE=[-2.775] 141 | (1,0,1):DODGE=[-2.743] 142 | (1,0,2):DODGE=[-2.441] 143 | (1,1,0):RECHARGE=[-2.357] 144 | (1,1,1):SHOOT=[2.362] 145 | (1,1,2):SHOOT=[2.362] 146 | (1,2,0):RECHARGE=[-2.357] 147 | (1,2,1):SHOOT=[2.382] 148 | (1,2,2):SHOOT=[2.619] 149 | (1,3,0):RECHARGE=[-2.357] 150 | (1,3,1):SHOOT=[2.382] 151 | (1,3,2):SHOOT=[2.619] 152 | (2,0,0):RECHARGE=[-2.775] 153 | (2,0,1):RECHARGE=[-2.775] 154 | (2,0,2):DODGE=[-2.775] 155 | (2,1,0):RECHARGE=[-2.775] 156 | (2,1,1):RECHARGE=[-2.775] 157 | (2,1,2):SHOOT=[-2.775] 158 | (2,2,0):RECHARGE=[-2.775] 159 | (2,2,1):RECHARGE=[-2.755] 160 | (2,2,2):SHOOT=[-2.519] 161 | (2,3,0):RECHARGE=[-2.775] 162 | (2,3,1):RECHARGE=[-2.755] 163 | (2,3,2):SHOOT=[-2.519] 164 | (3,0,0):RECHARGE=[-2.775] 165 | (3,0,1):RECHARGE=[-2.775] 166 | (3,0,2):DODGE=[-2.775] 167 | (3,1,0):RECHARGE=[-2.775] 168 | (3,1,1):RECHARGE=[-2.775] 169 | (3,1,2):DODGE=[-2.775] 170 | (3,2,0):RECHARGE=[-2.775] 171 | (3,2,1):RECHARGE=[-2.775] 172 | (3,2,2):DODGE=[-2.775] 173 | (3,3,0):RECHARGE=[-2.775] 174 | (3,3,1):RECHARGE=[-2.775] 175 | (3,3,2):SHOOT=[-2.775] 176 | (4,0,0):RECHARGE=[-2.775] 177 | (4,0,1):RECHARGE=[-2.775] 178 | (4,0,2):DODGE=[-2.775] 179 | (4,1,0):RECHARGE=[-2.775] 180 | (4,1,1):RECHARGE=[-2.775] 181 | (4,1,2):DODGE=[-2.775] 182 | (4,2,0):RECHARGE=[-2.775] 183 | (4,2,1):RECHARGE=[-2.775] 184 | (4,2,2):DODGE=[-2.775] 185 | (4,3,0):RECHARGE=[-2.775] 186 | (4,3,1):RECHARGE=[-2.775] 187 | (4,3,2):DODGE=[-2.775] 188 | 189 | 190 | iteration=3 191 | (0,0,0):-1=[0.0] 192 | (0,0,1):-1=[0.0] 193 | (0,0,2):-1=[0.0] 194 | (0,1,0):-1=[0.0] 195 | (0,1,1):-1=[0.0] 196 | (0,1,2):-1=[0.0] 197 | (0,2,0):-1=[0.0] 198 | (0,2,1):-1=[0.0] 199 | (0,2,2):-1=[0.0] 200 | (0,3,0):-1=[0.0] 201 | (0,3,1):-1=[0.0] 202 | (0,3,2):-1=[0.0] 203 | (1,0,0):RECHARGE=[-2.775] 204 | (1,0,1):DODGE=[-2.744] 205 | (1,0,2):DODGE=[-2.442] 206 | (1,1,0):RECHARGE=[-2.358] 207 | (1,1,1):SHOOT=[2.361] 208 | (1,1,2):SHOOT=[2.363] 209 | (1,2,0):RECHARGE=[-2.357] 210 | (1,2,1):SHOOT=[2.382] 211 | (1,2,2):SHOOT=[2.618] 212 | (1,3,0):RECHARGE=[-2.357] 213 | (1,3,1):SHOOT=[2.382] 214 | (1,3,2):SHOOT=[2.619] 215 | (2,0,0):RECHARGE=[-2.778] 216 | (2,0,1):RECHARGE=[-2.778] 217 | (2,0,2):DODGE=[-2.778] 218 | (2,1,0):RECHARGE=[-2.778] 219 | (2,1,1):RECHARGE=[-2.778] 220 | (2,1,2):SHOOT=[-2.776] 221 | (2,2,0):RECHARGE=[-2.776] 222 | (2,2,1):RECHARGE=[-2.757] 223 | (2,2,2):SHOOT=[-2.521] 224 | (2,3,0):RECHARGE=[-2.776] 225 | (2,3,1):RECHARGE=[-2.757] 226 | (2,3,2):SHOOT=[-2.519] 227 | (3,0,0):RECHARGE=[-2.778] 228 | (3,0,1):RECHARGE=[-2.778] 229 | (3,0,2):DODGE=[-2.778] 230 | (3,1,0):RECHARGE=[-2.778] 231 | (3,1,1):RECHARGE=[-2.778] 232 | (3,1,2):DODGE=[-2.778] 233 | (3,2,0):RECHARGE=[-2.778] 234 | (3,2,1):RECHARGE=[-2.778] 235 | (3,2,2):DODGE=[-2.778] 236 | (3,3,0):RECHARGE=[-2.778] 237 | (3,3,1):RECHARGE=[-2.778] 238 | (3,3,2):SHOOT=[-2.776] 239 | (4,0,0):RECHARGE=[-2.778] 240 | (4,0,1):RECHARGE=[-2.778] 241 | (4,0,2):DODGE=[-2.778] 242 | (4,1,0):RECHARGE=[-2.778] 243 | (4,1,1):RECHARGE=[-2.778] 244 | (4,1,2):DODGE=[-2.778] 245 | (4,2,0):RECHARGE=[-2.778] 246 | (4,2,1):RECHARGE=[-2.778] 247 | (4,2,2):DODGE=[-2.778] 248 | (4,3,0):RECHARGE=[-2.778] 249 | (4,3,1):RECHARGE=[-2.778] 250 | (4,3,2):DODGE=[-2.778] 251 | 252 | 253 | iteration=4 254 | (0,0,0):-1=[0.0] 255 | (0,0,1):-1=[0.0] 256 | (0,0,2):-1=[0.0] 257 | (0,1,0):-1=[0.0] 258 | (0,1,1):-1=[0.0] 259 | (0,1,2):-1=[0.0] 260 | (0,2,0):-1=[0.0] 261 | (0,2,1):-1=[0.0] 262 | (0,2,2):-1=[0.0] 263 | (0,3,0):-1=[0.0] 264 | (0,3,1):-1=[0.0] 265 | (0,3,2):-1=[0.0] 266 | (1,0,0):RECHARGE=[-2.775] 267 | (1,0,1):DODGE=[-2.744] 268 | (1,0,2):DODGE=[-2.442] 269 | (1,1,0):RECHARGE=[-2.358] 270 | (1,1,1):SHOOT=[2.361] 271 | (1,1,2):SHOOT=[2.363] 272 | (1,2,0):RECHARGE=[-2.357] 273 | (1,2,1):SHOOT=[2.382] 274 | (1,2,2):SHOOT=[2.618] 275 | (1,3,0):RECHARGE=[-2.357] 276 | (1,3,1):SHOOT=[2.382] 277 | (1,3,2):SHOOT=[2.619] 278 | (2,0,0):RECHARGE=[-2.778] 279 | (2,0,1):RECHARGE=[-2.778] 280 | (2,0,2):DODGE=[-2.778] 281 | (2,1,0):RECHARGE=[-2.778] 282 | (2,1,1):RECHARGE=[-2.778] 283 | (2,1,2):SHOOT=[-2.776] 284 | (2,2,0):RECHARGE=[-2.776] 285 | (2,2,1):SHOOT=[-2.757] 286 | (2,2,2):SHOOT=[-2.521] 287 | (2,3,0):RECHARGE=[-2.776] 288 | (2,3,1):RECHARGE=[-2.757] 289 | (2,3,2):SHOOT=[-2.519] 290 | (3,0,0):RECHARGE=[-2.778] 291 | (3,0,1):RECHARGE=[-2.778] 292 | (3,0,2):DODGE=[-2.778] 293 | (3,1,0):RECHARGE=[-2.778] 294 | (3,1,1):RECHARGE=[-2.778] 295 | (3,1,2):DODGE=[-2.778] 296 | (3,2,0):RECHARGE=[-2.778] 297 | (3,2,1):RECHARGE=[-2.778] 298 | (3,2,2):SHOOT=[-2.778] 299 | (3,3,0):RECHARGE=[-2.778] 300 | (3,3,1):RECHARGE=[-2.778] 301 | (3,3,2):SHOOT=[-2.777] 302 | (4,0,0):RECHARGE=[-2.778] 303 | (4,0,1):RECHARGE=[-2.778] 304 | (4,0,2):DODGE=[-2.778] 305 | (4,1,0):RECHARGE=[-2.778] 306 | (4,1,1):RECHARGE=[-2.778] 307 | (4,1,2):DODGE=[-2.778] 308 | (4,2,0):RECHARGE=[-2.778] 309 | (4,2,1):RECHARGE=[-2.778] 310 | (4,2,2):DODGE=[-2.778] 311 | (4,3,0):RECHARGE=[-2.778] 312 | (4,3,1):RECHARGE=[-2.778] 313 | (4,3,2):DODGE=[-2.778] 314 | 315 | 316 | -------------------------------------------------------------------------------- /A2-2/submission/task_1.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import sys 4 | 5 | def check(health1,arrows1,stamina1,health2,arrows2,stamina2,dh,da,ds): 6 | if (dh==health1-health2) and (da==arrows1-arrows2) and (ds==stamina1-stamina2): 7 | return 1 8 | else: 9 | return 0 10 | 11 | def shoot(h1,a1,s1,h2,a2,s2): 12 | 13 | #no arrows or no stamina 14 | if a1==0 or s1==0: 15 | return check(h1,a1,s1,h2,a2,s2,0,0,0) 16 | 17 | #misses MD 18 | elif check(h1,a1,s1,h2,a2,s2,0,1,50): 19 | return 0.5 20 | 21 | #hits MD 22 | elif check(h1,a1,s1,h2,a2,s2,25,1,50): 23 | return 0.5 24 | 25 | else: 26 | return 0 27 | 28 | def dodge (h1,a1,s1,h2,a2,s2): 29 | 30 | #zero stamina 31 | if s1==0: 32 | return 0 33 | 34 | #stamina is 50 35 | elif s1==50: 36 | 37 | #arrows already full 38 | if a1==3 and check(h1,a1,s1,h2,a2,s2,0,0,50): 39 | return 1 40 | 41 | #unsuccessful arrow pickup 42 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 43 | return 0.2 44 | 45 | #successful arrow pickup 46 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 47 | return 0.8 48 | 49 | 50 | #stamina is 100 51 | elif s1==100: 52 | 53 | #3 arrows 54 | if(a1==3): 55 | 56 | #lose 50 stamins 57 | if check(h1,a1,s1,h2,a2,s2,0,0,50): 58 | return 0.8 59 | 60 | #lose 100 stamina 61 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 62 | return 0.2 63 | 64 | 65 | #successful arrow pickup, 50 stamina loss 66 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 67 | return 0.8*0.8 68 | 69 | #unsuccessful arrow pickup, 50 stamina loss 70 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 71 | return 0.2*0.8 72 | 73 | #successful arrow pickup, 100 stamina loss 74 | elif check(h1,a1,s1,h2,a2,s2,0,-1,100): 75 | return 0.8*0.2 76 | 77 | #unsuccessful arrow pickup, 100 stamina loss 78 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 79 | return 0.2*0.2 80 | 81 | return 0 82 | 83 | def recharge (h1,a1,s1,h2,a2,s2): 84 | 85 | #stamina is 100 86 | if(s1==100): 87 | 88 | if check(h1,a1,s1,h2,a2,s2,0,0,0)==1: 89 | return 1 90 | else: 91 | return 0 92 | 93 | #stamina not 100 94 | else: 95 | if check(h1,a1,s1,h2,a2,s2,0,0,-50): #since stamina increase therefore old-new = -50 96 | return 0.8 97 | elif check(h1,a1,s1,h2,a2,s2,0,0,0): 98 | return 0.2 99 | 100 | else: 101 | return 0 102 | 103 | def mapactions(ac): 104 | if ac==0: 105 | return "SHOOT" 106 | elif ac==1: 107 | return "DODGE" 108 | elif ac==2: 109 | return "RECHARGE" 110 | elif ac==-1: 111 | return "-1" 112 | else: 113 | return "ERROR" 114 | 115 | def callaction(ac,h1,a1,s1,h2,a2,s2): 116 | 117 | if ac==0: 118 | return shoot(h1,a1,s1,h2,a2,s2) 119 | 120 | elif ac==1: 121 | return dodge(h1,a1,s1,h2,a2,s2) 122 | 123 | elif ac==2: 124 | return recharge(h1,a1,s1,h2,a2,s2) 125 | 126 | 127 | # penalty=[-20,-20,-20] #step cost of shoot,dodge, recharge task 1 128 | # # penalty=[-0.25,-2.5,-2.5] #step cost of shoot,dodge, recharge task2_1 129 | # # penalty=[-2.5,-2.5,-2.5] #step cost of shoot,dodge, recharge task2_2 130 | # gamma=0.99 #task_1 131 | # # gamma=0.1 #task2_2 132 | # delta=0.001 133 | # # delta = 0.0000000001 #task3 134 | 135 | 136 | def vi(): 137 | actionsnum=3 #shoot,dodge,recharge 138 | health=5 #mult by 25 139 | arrows=4 140 | stamina=3 #mult by 50 141 | 142 | utilities=np.zeros((health,arrows,stamina)) 143 | actions=np.zeros((health,arrows,stamina)) 144 | 145 | 146 | #numpy.copyto(dst, src) 147 | deltacheck=100000000 148 | iterations = -1 149 | 150 | while(deltacheck>delta): 151 | 152 | temparray=np.zeros((health,arrows,stamina)) 153 | temparray[:]=-10000000000000 154 | temparray[0,:,:]=0 155 | deltacheck=-1000000000000 156 | # actions[:]=-1 157 | 158 | #updating the utilities 159 | for h1 in range(1,health): 160 | for a1 in range(0,arrows): 161 | for s1 in range(0,stamina): 162 | #for each state 163 | 164 | for ac in range(0,actionsnum): 165 | # for each action 166 | 167 | #conditions for skipping actions 168 | if (a1==0 and ac==0) or (s1==0 and ac==0) or (s1==0 and ac==1): 169 | continue 170 | 171 | temp=0 172 | for h2 in range(0,health): 173 | for a2 in range(0,arrows): 174 | for s2 in range(0,stamina): 175 | reward=penalty[ac] 176 | 177 | #reward for going to a terminal state 178 | if(h2==0): 179 | reward+=10 180 | 181 | temp+=round(round(callaction(ac,h1*25,a1,s1*50,h2*25,a2,s2*50),3)*(reward+gamma*utilities[h2,a2,s2]),11) 182 | 183 | temp=round(temp,11) 184 | if(temp>=temparray[h1,a1,s1]): 185 | temparray[h1,a1,s1]=temp 186 | # actions[h1,a1,s1]=ac 187 | 188 | if(abs(temparray[h1,a1,s1]-utilities[h1,a1,s1])>deltacheck): 189 | deltacheck=abs(temparray[h1,a1,s1]-utilities[h1,a1,s1]) 190 | 191 | np.copyto(utilities, temparray) 192 | #now the utilities have been set 193 | 194 | tempactions=np.zeros((health,arrows,stamina)) 195 | actions[:]=-1000000000000 196 | tempactions[:]=-1000000000000 197 | actions[0,:,:]=-1 198 | tempactions[0,:,:]=-1 199 | 200 | for h1 in range(1,health): 201 | for a1 in range(0,arrows): 202 | for s1 in range(0,stamina): 203 | #for each state 204 | 205 | for ac in range(0,actionsnum): 206 | # for each action 207 | 208 | #conditions for skipping actions 209 | if (a1==0 and ac==0) or (s1==0 and ac==0) or (s1==0 and ac==1) or (s1==2 and ac==2): 210 | continue 211 | 212 | temp=0 213 | for h2 in range(0,health): 214 | for a2 in range(0,arrows): 215 | for s2 in range(0,stamina): 216 | reward=penalty[ac] 217 | 218 | #reward for going to a terminal state 219 | if(h2==0): 220 | reward+=10 221 | 222 | temp+=round(round(callaction(ac,h1*25,a1,s1*50,h2*25,a2,s2*50),3)*(reward+gamma*utilities[h2,a2,s2]),11) 223 | 224 | if(temp>=tempactions[h1,a1,s1]): 225 | actions[h1,a1,s1]=ac 226 | tempactions[h1,a1,s1]=temp 227 | 228 | # if(abs(temparray[h1,a1,s1]-utilities[h1,a1,s1])>deltacheck): 229 | # deltacheck=abs(temparray[h1,a1,s1]-utilities[h1,a1,s1]) 230 | 231 | iterations+=1 232 | print("iteration="+str(iterations)) 233 | u=np.around(utilities,3) 234 | # u=utilities 235 | for h1 in range(0,health): 236 | for a1 in range(0,arrows): 237 | for s1 in range(0,stamina): 238 | print("("+str(h1)+","+str(a1)+","+str(s1)+"):"+mapactions(actions[h1,a1,s1])+"=["+str(u[h1,a1,s1])+"]") 239 | print("\n") 240 | 241 | 242 | original = sys.stdout 243 | os.mkdir("./outputs") 244 | 245 | 246 | #Task 1 : 247 | penalty=[-5,-5,-5] #step cost of shoot,dodge, recharge task 1 248 | gamma=0.99 249 | delta=0.001 250 | sys.stdout = open('./outputs/task_1_trace.txt', 'w') 251 | vi() 252 | sys.stdout = original 253 | 254 | #Task 2 - Part 1 : 255 | penalty=[-0.25,-2.5,-2.5] #step cost of shoot,dodge, recharge task2_1 256 | gamma=0.99 257 | delta=0.001 258 | 259 | sys.stdout = open('./outputs/task_2_part_1_trace.txt', 'w') 260 | vi() 261 | sys.stdout = original 262 | 263 | #Task 2 - Part 2 : 264 | penalty=[-2.5,-2.5,-2.5] #step cost of shoot,dodge, recharge task2_2 265 | gamma=0.1 266 | delta=0.001 267 | 268 | sys.stdout = open('./outputs/task_2_part_2_trace.txt', 'w') 269 | vi() 270 | sys.stdout = original 271 | 272 | #Task 2 - Part 3 : 273 | penalty=[-2.5,-2.5,-2.5] #step cost of shoot,dodge, recharge task2_2 274 | gamma=0.1 275 | delta = 0.0000000001 276 | 277 | 278 | sys.stdout = open('./outputs/task_2_part_3_trace.txt', 'w') 279 | vi() 280 | sys.stdout = original 281 | 282 | -------------------------------------------------------------------------------- /A2-2/submission/team_74_report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A2-2/submission/team_74_report.pdf -------------------------------------------------------------------------------- /A2-3/Readme.md: -------------------------------------------------------------------------------- 1 | # **OPTIMAL POLICY USING LPP** 2 | 3 | ## **TEAM** 4 | 5 | * Tanvi Karandikar 2018101059 6 | 7 | * Mallika Subramanian 2018101041 8 | 9 | ## **OVERVIEW** 10 | 11 | The optimal policy for a Markov Decision Process can be obtained via many ways. Here the problem is posed as a Linear Programming Problem(LPP) and the optimal policy is found by solving the LP. 12 | 13 | **Setup and Run** 14 | 15 | ```bash 16 | cd team_62 17 | python3 solution.py 18 | ``` 19 | 20 | This will generate the output.json file that contains the following parameters : 21 | 22 | * The A matrix 23 | * The R array 24 | * The alpha array 25 | * The optimised x array 26 | * The derived 27 | * The Deterministic policy 28 | * The final optimised objective value 29 | 30 | 31 | ## **MATRIX A :** 32 | 33 | The X vector (that is to be solved for, using the LPP) contains all (STATE, ACITON) pairs which represents the expected number of times an action is taken in a particular state. 34 | 35 | The number of rows for the matrix A is equal to the number of **STATES** in the MDP. 36 | The number of coloumns for the matrix A is equal to the number of **(STATE, ACTION) pairs.** possible in the MDP. 37 | Eg: x11,x12,x12,x21,x22 ... 38 | Where each (xij) represents the action **j** taken in state **i** 39 | 40 | Corresponding to each row (state) the entries in the matrix A represent the effect that, that state will have for every (STATE, ACTION) pair. 41 | 42 | The probabilities of the (STATE, ACTION) pairs contribute to the value of the corresponding matrix entries. 43 | 44 | All **Inflow** probabilites are subtracted and **Outflow** probabilities are added. 45 | 46 | That is, consider a any row of matrix A, say *p*, if the (STATE, ACTION) pair causes an incoming edge into *state p* then this probability is **subtracted** whereas if it is an outgoing edge, the probability is **added**. 47 | 48 | Our implementation involves filling the matrix A **Coloumn wise**. Here we have considered the effect that a particular (STATE, ACTION) pair will have on all the states of the MDP and accordingly filled the result of the probabilites (outflow-inflow) 49 | 50 | The standard that we have followed in case of the NOOP actions taken at the terminal states is that the entry in A corresponing to the row of the TERMINAL state and coloumn for the pair (TERMINAL, NOOP) the entry will be a 1. 51 | 52 | In our given MDP for which we have to obtain the policy, the dimensions of A are 60 x 100 - since there are 60 states in total (5 values of health, 4 values of arros x 3 values of stamina) and there are 100 (STATE, ACTION) pairs. 53 | 54 | 55 | ## **PROCEDURE TO OBTAIN THE POLICY :** 56 | 57 | The optimal policy for an MDP will be the one that results in the maximum reward. Hence this is the objective function that we have to maximize. 58 | 59 | > Objective function = Σ Vi ... TO MAXIMIZE 60 | ```python 61 | objective = cp.Maximize(cp.sum(cp.matmul(r,x), axis=0)) 62 | ``` 63 | Using the Bellmann equation we know that : 64 | 65 | > Vi < = [ R ( I , A ) + γ * Σ P ( J | I , A ) *Vj ] 66 | 67 | Therefore the function is subject to to the following constraints: 68 | 69 | > Ax = *alpha* 70 | Where alpha represents the probabilities of each state being a start state. 71 | 72 | And the non-negativity constraints for the values of x that is : 73 | 74 | > x > = 0 75 | 76 | ```python 77 | constraints = [cp.matmul(a, x) == alpha, x>=0] 78 | ``` 79 | 80 | These are the pre-requisites to solve the LPP. On solving the LPP we obtain the values for the vector **x**. 81 | 82 | Each element of **x** (xij) represents the expected number of times an action **j** is taken in the state **i**. 83 | 84 | Once the LPP is solved and the vector *x* is obtained, the optimal policy is one that selects that action with the **maximum expected value** for each particular state. 85 | 86 | 87 | 90 | 91 | 92 | ## **MULTIPLE POLICIES :** 93 | 94 | Yes there can be multiple policies that can be generated for a single MDP. 95 | 96 | * In order to select an optimal action to be taken in a state, we select the one with the maximum value for *expected number of times the action will be taken in that state*. It may so happen that this *expected value* is the same for 2 or more actions in the same state. Hence if the condition to look for the maximum is changed from a strict inequality to a normal inequality, then only the first action amongst these equal ones will be chosen. This not affect the values of the matrix A, or the values of the vectors R, X or alpha. 97 | 98 | * Likewise if the actions are mapped to the numbers 0,1,2,3, differently and not the way they currently are mapped (0:SHOOT, 1:DODGE, 2:RECHARGE, 3:NOOP) this can also lead to selection of a different action in case of 2 or more actions having the same *expected value*. This will also not affect the value of A, R, X or alpha. 99 | 100 | * Changing the **reward/step-cost/penalty** for each state can also result in a change in the policy. Since if the penalty is higher (stepcost is lower) the agent will want to and try to reach the terminal state faster with a greater reward. Here the R vector will be different. 101 | 102 | * Selecting a different start state can also result in generating a different policy to the MDP. That is any change in the *alpha* vector can change the policy. 103 | 104 | * Furthermore, changing values of parameters such as the probabilities of taking various different actions, the rewards/penalties received at each state etc will obviously change the policy since the state diagram of the problem itself would be different in such cases. 105 | -------------------------------------------------------------------------------- /A2-3/intermediate/check.txt: -------------------------------------------------------------------------------- 1 | 0 0 0 [3] 2 | 0 0 1 [3] 3 | 0 0 2 [3] 4 | 0 1 0 [3] 5 | 0 1 1 [3] 6 | 0 1 2 [3] 7 | 0 2 0 [3] 8 | 0 2 1 [3] 9 | 0 2 2 [3] 10 | 0 3 0 [3] 11 | 0 3 1 [3] 12 | 0 3 2 [3] 13 | 1 0 0 [2] 14 | 1 0 1 [1, 2] 15 | 1 0 2 [1] 16 | 1 1 0 [2] 17 | 1 1 1 [0, 1, 2] 18 | 1 1 2 [0, 1] 19 | 1 2 0 [2] 20 | 1 2 1 [0, 1, 2] 21 | 1 2 2 [0, 1] 22 | 1 3 0 [2] 23 | 1 3 1 [0, 1, 2] 24 | 1 3 2 [0, 1] 25 | 2 0 0 [2] 26 | 2 0 1 [1, 2] 27 | 2 0 2 [1] 28 | 2 1 0 [2] 29 | 2 1 1 [0, 1, 2] 30 | 2 1 2 [0, 1] 31 | 2 2 0 [2] 32 | 2 2 1 [0, 1, 2] 33 | 2 2 2 [0, 1] 34 | 2 3 0 [2] 35 | 2 3 1 [0, 1, 2] 36 | 2 3 2 [0, 1] 37 | 3 0 0 [2] 38 | 3 0 1 [1, 2] 39 | 3 0 2 [1] 40 | 3 1 0 [2] 41 | 3 1 1 [0, 1, 2] 42 | 3 1 2 [0, 1] 43 | 3 2 0 [2] 44 | 3 2 1 [0, 1, 2] 45 | 3 2 2 [0, 1] 46 | 3 3 0 [2] 47 | 3 3 1 [0, 1, 2] 48 | 3 3 2 [0, 1] 49 | 4 0 0 [2] 50 | 4 0 1 [1, 2] 51 | 4 0 2 [1] 52 | 4 1 0 [2] 53 | 4 1 1 [0, 1, 2] 54 | 4 1 2 [0, 1] 55 | 4 2 0 [2] 56 | 4 2 1 [0, 1, 2] 57 | 4 2 2 [0, 1] 58 | 4 3 0 [2] 59 | 4 3 1 [0, 1, 2] 60 | 4 3 2 [0, 1] 61 | -------------------------------------------------------------------------------- /A2-3/intermediate/check2.txt: -------------------------------------------------------------------------------- 1 | 2 | list of actions = [3] 3 | x values = [0.9999999998699223] 4 | 0 0 1 action : NOOP 5 | 6 | 7 | list of actions = [3] 8 | x values = [4.675964862287982e-11] 9 | 0 0 2 action : NOOP 10 | 11 | 12 | list of actions = [3] 13 | x values = [2.628157412355072e-13] 14 | 0 1 0 action : NOOP 15 | 16 | 17 | list of actions = [3] 18 | x values = [4.262931778922846e-11] 19 | 0 1 1 action : NOOP 20 | 21 | 22 | list of actions = [3] 23 | x values = [2.2371336276186244e-11] 24 | 0 1 2 action : NOOP 25 | 26 | 27 | list of actions = [3] 28 | x values = [2.628157412355072e-13] 29 | 0 2 0 action : NOOP 30 | 31 | 32 | list of actions = [3] 33 | x values = [-3.381492999955128e-12] 34 | 0 2 1 action : NOOP 35 | 36 | 37 | list of actions = [3] 38 | x values = [1.7463816065792178e-11] 39 | 0 2 2 action : NOOP 40 | 41 | 42 | list of actions = [3] 43 | x values = [2.628157412355072e-13] 44 | 0 3 0 action : NOOP 45 | 46 | 47 | list of actions = [3] 48 | x values = [2.628157412355072e-13] 49 | 0 3 1 action : NOOP 50 | 51 | 52 | list of actions = [3] 53 | x values = [2.628157412355072e-13] 54 | 0 3 2 action : NOOP 55 | 56 | 57 | list of actions = [3] 58 | x values = [2.628157412355072e-13] 59 | 1 0 0 action : NOOP 60 | 61 | 62 | list of actions = [2] 63 | x values = [1.990391612453656] 64 | 1 0 1 action : RECHARGE 65 | 66 | 67 | list of actions = [1, 2] 68 | x values = [1.8555191288978166, 1.5611372489292963e-10] 69 | 1 0 2 action : DODGE 70 | 71 | 72 | list of actions = [1] 73 | x values = [1.2489001767001358e-10] 74 | 1 1 0 action : DODGE 75 | 76 | 77 | list of actions = [2] 78 | x values = [2.105525137785066] 79 | 1 1 1 action : RECHARGE 80 | 81 | 82 | list of actions = [0, 1, 2] 83 | x values = [1.9999999997398423, -1.7656845449296473e-11, 7.776596961064193e-11] 84 | 1 1 2 action : SHOOT 85 | 86 | 87 | list of actions = [0, 1] 88 | x values = [9.31952746406608e-11, -3.067270741011536e-11] 89 | 1 2 0 action : SHOOT 90 | 91 | 92 | list of actions = [2] 93 | x values = [9.145945264422454e-11] 94 | 1 2 1 action : RECHARGE 95 | 96 | 97 | list of actions = [0, 1, 2] 98 | x values = [8.506586947012974e-11, 6.078805110542981e-11, 2.2089694460261196e-11] 99 | 1 2 2 action : SHOOT 100 | 101 | 102 | list of actions = [0, 1] 103 | x values = [4.4408197975139286e-11, -2.6543406811621867e-11] 104 | 1 3 0 action : SHOOT 105 | 106 | 107 | list of actions = [2] 108 | x values = [-2.3299042834583258e-11] 109 | 1 3 1 action : RECHARGE 110 | 111 | 112 | list of actions = [0, 1, 2] 113 | x values = [-7.098974194236212e-12, -5.210763457019462e-11, -2.387686702540466e-11] 114 | 1 3 2 action : SHOOT 115 | 116 | 117 | list of actions = [0, 1] 118 | x values = [3.4543999106521986e-11, -5.3401679559353163e-11] 119 | 2 0 0 action : SHOOT 120 | 121 | 122 | list of actions = [2] 123 | x values = [0.7297549978831364] 124 | 2 0 1 action : RECHARGE 125 | 126 | 127 | list of actions = [1, 2] 128 | x values = [1.0275558475111413, 3.482449054115718e-10] 129 | 2 0 2 action : DODGE 130 | 131 | 132 | list of actions = [1] 133 | x values = [2.786226408104658e-10] 134 | 2 1 0 action : DODGE 135 | 136 | 137 | list of actions = [2] 138 | x values = [1.6694856084355356] 139 | 2 1 1 action : RECHARGE 140 | 141 | 142 | list of actions = [0, 1, 2] 143 | x values = [0.4424189286168401, 0.8951883903511595, 0.6580145975539808] 144 | 2 1 2 action : DODGE 145 | 146 | 147 | list of actions = [0, 1] 148 | x values = [0.5264116779863436, 5.686355329688412e-11] 149 | 2 2 0 action : SHOOT 150 | 151 | 152 | list of actions = [2] 153 | x values = [1.0652433328724873] 154 | 2 2 1 action : RECHARGE 155 | 156 | 157 | list of actions = [0, 1, 2] 158 | x values = [0.4000096141040468, 4.556920300204301e-10, 0.7889497236475611] 159 | 2 2 2 action : RECHARGE 160 | 161 | 162 | list of actions = [0, 1] 163 | x values = [0.6311597789181898, -1.240365506461434e-13] 164 | 2 3 0 action : SHOOT 165 | 166 | 167 | list of actions = [2] 168 | x values = [3.849610801876808e-10] 169 | 2 3 1 action : RECHARGE 170 | 171 | 172 | list of actions = [0, 1, 2] 173 | x values = [1.6985001292822188e-10, -4.695028457586793e-11, 1.891265276565083e-10] 174 | 2 3 2 action : RECHARGE 175 | 176 | 177 | list of actions = [0, 1] 178 | x values = [1.94067326834938e-10, -4.192556762476104e-11] 179 | 3 0 0 action : SHOOT 180 | 181 | 182 | list of actions = [2] 183 | x values = [0.420611814942967] 184 | 3 0 1 action : RECHARGE 185 | 186 | 187 | list of actions = [1, 2] 188 | x values = [0.5865255680721023, 3.4156542071532667e-10] 189 | 3 0 2 action : DODGE 190 | 191 | 192 | list of actions = [1] 193 | x values = [2.7321161451508174e-10] 194 | 3 1 0 action : DODGE 195 | 196 | 197 | list of actions = [2] 198 | x values = [0.9845952650378579] 199 | 3 1 1 action : RECHARGE 200 | 201 | 202 | list of actions = [0, 1, 2] 203 | x values = [0.3141667289694078, 0.45294067637580704, 0.45136502614101315] 204 | 3 1 2 action : DODGE 205 | 206 | 207 | list of actions = [0, 1] 208 | x values = [0.361092020890906, 2.1861748456310718e-11] 209 | 3 2 0 action : SHOOT 210 | 211 | 212 | list of actions = [2] 213 | x values = [0.9147406511910847] 214 | 3 2 1 action : RECHARGE 215 | 216 | 217 | list of actions = [0, 1, 2] 218 | x values = [0.2690026471407827, 0.7875467021511742, 0.5321265515964506] 219 | 3 2 2 action : DODGE 220 | 221 | 222 | list of actions = [0, 1] 223 | x values = [0.42570124123245806, 4.466542901393184e-11] 224 | 3 3 0 action : SHOOT 225 | 226 | 227 | list of actions = [2] 228 | x values = [0.7875467020861017] 229 | 3 3 1 action : RECHARGE 230 | 231 | 232 | list of actions = [0, 1, 2] 233 | x values = [0.2720879076637911, -5.1074785841579895e-11, 0.44743681756547776] 234 | 3 3 2 action : RECHARGE 235 | 236 | 237 | list of actions = [0, 1] 238 | x values = [0.35794945409275597, -4.040230171740773e-11] 239 | 4 0 0 action : SHOOT 240 | 241 | 242 | list of actions = [2] 243 | x values = [0.1187484296302197] 244 | 4 0 1 action : RECHARGE 245 | 246 | 247 | list of actions = [1, 2] 248 | x values = [0.164488849206264, 5.939949093071854e-10] 249 | 4 0 2 action : DODGE 250 | 251 | 252 | list of actions = [1] 253 | x values = [4.747154879340513e-10] 254 | 4 1 0 action : DODGE 255 | 256 | 257 | list of actions = [2] 258 | x values = [0.3091491639985499] 259 | 4 1 1 action : RECHARGE 260 | 261 | 262 | list of actions = [0, 1, 2] 263 | x values = [0.12420194768876588, 0.111809765358538, 0.17372526488010567] 264 | 4 1 2 action : RECHARGE 265 | 266 | 267 | list of actions = [0, 1] 268 | x values = [0.1389802118037794, 9.99164385457043e-11] 269 | 4 2 0 action : SHOOT 270 | 271 | 272 | list of actions = [2] 273 | x values = [0.29877438993227934] 274 | 4 2 1 action : RECHARGE 275 | 276 | 277 | list of actions = [0, 1, 2] 278 | x values = [0.18673259736501427, 0.3684250702812824, 0.3191814836506619] 279 | 4 2 2 action : DODGE 280 | 281 | 282 | list of actions = [0, 1] 283 | x values = [0.25534518686644175, 5.378728783169216e-11] 284 | 4 3 0 action : SHOOT 285 | 286 | 287 | list of actions = [2] 288 | x values = [0.36842507019619347] 289 | 4 3 1 action : RECHARGE 290 | 291 | 292 | list of actions = [0, 1, 2] 293 | x values = [0.15177337116968637, -6.385227252294208e-11, 0.17870835629402831] 294 | 4 3 2 action : RECHARGE 295 | 296 | 297 | list of actions = [0, 1] 298 | x values = [1.1429666850975693, -6.253850759606887e-11] 299 | 0 0 0 action : SHOOT 300 | 301 | -------------------------------------------------------------------------------- /A2-3/intermediate/code.py: -------------------------------------------------------------------------------- 1 | import cvxpy as cp 2 | import numpy as np 3 | import json 4 | import os 5 | import sys 6 | 7 | def check(health1,arrows1,stamina1,health2,arrows2,stamina2,dh,da,ds): 8 | if (dh==health1-health2) and (da==arrows1-arrows2) and (ds==stamina1-stamina2): 9 | return 1 10 | else: 11 | return 0 12 | 13 | 14 | def shoot(h1,a1,s1,h2,a2,s2): 15 | 16 | #no arrows or no stamina 17 | if a1==0 or s1==0: 18 | return check(h1,a1,s1,h2,a2,s2,0,0,0) 19 | 20 | #misses MD 21 | elif check(h1,a1,s1,h2,a2,s2,0,1,50): 22 | return 0.5 23 | 24 | #hits MD 25 | elif check(h1,a1,s1,h2,a2,s2,25,1,50): 26 | return 0.5 27 | 28 | else: 29 | return 0 30 | 31 | def dodge (h1,a1,s1,h2,a2,s2): 32 | 33 | #zero stamina 34 | if s1==0: 35 | return 0 36 | 37 | #stamina is 50 38 | elif s1==50: 39 | 40 | #arrows already full 41 | if a1==3 and check(h1,a1,s1,h2,a2,s2,0,0,50): 42 | return 1 43 | 44 | #unsuccessful arrow pickup 45 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 46 | return 0.2 47 | 48 | #successful arrow pickup 49 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 50 | return 0.8 51 | 52 | 53 | #stamina is 100 54 | elif s1==100: 55 | 56 | #3 arrows 57 | if(a1==3): 58 | 59 | #lose 50 stamins 60 | if check(h1,a1,s1,h2,a2,s2,0,0,50): 61 | return 0.8 62 | 63 | #lose 100 stamina 64 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 65 | return 0.2 66 | 67 | 68 | #successful arrow pickup, 50 stamina loss 69 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 70 | return 0.8*0.8 71 | 72 | #unsuccessful arrow pickup, 50 stamina loss 73 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 74 | return 0.2*0.8 75 | 76 | #successful arrow pickup, 100 stamina loss 77 | elif check(h1,a1,s1,h2,a2,s2,0,-1,100): 78 | return 0.8*0.2 79 | 80 | #unsuccessful arrow pickup, 100 stamina loss 81 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 82 | return 0.2*0.2 83 | 84 | return 0 85 | 86 | def recharge (h1,a1,s1,h2,a2,s2): 87 | 88 | #stamina is 100 89 | if(s1==100): 90 | 91 | if check(h1,a1,s1,h2,a2,s2,0,0,0)==1: 92 | return 1 93 | else: 94 | return 0 95 | 96 | #stamina not 100 97 | else: 98 | if check(h1,a1,s1,h2,a2,s2,0,0,-50): #since stamina increase therefore old-new = -50 99 | return 0.8 100 | elif check(h1,a1,s1,h2,a2,s2,0,0,0): 101 | return 0.2 102 | 103 | else: 104 | return 0 105 | 106 | def numtocomp(num): 107 | s=num%3 108 | a=(int(num/3))%4 109 | h=(int(num/12))%5 110 | 111 | return h,a,s 112 | 113 | def comptonum(h,a,s): 114 | num=h*12+a*3+s 115 | return num 116 | 117 | def mapactions(ac): 118 | if ac==0: 119 | return "SHOOT" 120 | elif ac==1: 121 | return "DODGE" 122 | elif ac==2: 123 | return "RECHARGE" 124 | elif ac==3: 125 | return "NOOP" 126 | else: 127 | return "ERROR" 128 | 129 | def callaction(ac,h1,a1,s1,h2,a2,s2): 130 | 131 | if ac==0: 132 | return shoot(h1,a1,s1,h2,a2,s2) 133 | 134 | elif ac==1: 135 | return dodge(h1,a1,s1,h2,a2,s2) 136 | 137 | elif ac==2: 138 | return recharge(h1,a1,s1,h2,a2,s2) 139 | 140 | elif ac==3: 141 | return 0 142 | 143 | actionsnum=4 #shoot,dodge,recharge,noop 144 | health=5 #mult by 25 145 | arrows=4 146 | stamina=3 #mult by 50 147 | states = 60 148 | 149 | columns=0 # every time we find an action we can add a column 150 | r=[] #rewards array 151 | reward = -5 152 | actions=[] 153 | a=[] 154 | for h1 in range(0,health): 155 | for a1 in range(0,arrows): 156 | for s1 in range(0,stamina): 157 | #for each state 158 | 159 | #storing the actions possible 160 | tempac=[] 161 | for ac in range(0,actionsnum): 162 | # for each action 163 | 164 | #noop conditions 165 | if (ac!=3 and h1==0) or (ac==3 and h1!=0): 166 | continue 167 | 168 | #conditions for skipping actions 169 | if (a1==0 and ac==0) or (s1==0 and ac==0) or (s1==0 and ac==1) or (s1==2 and ac==2): 170 | continue 171 | 172 | #reward =0 for terminal, reward = -5 for others 173 | if(h1==0): 174 | r.append(0) 175 | else: 176 | r.append(reward) 177 | 178 | columns+=1 179 | # print(columns, ". In state ", h1,a1,s1,"taking action ", mapactions(ac)) 180 | tempac.append(ac) 181 | newcol=[] 182 | out=0 183 | #making the column, we are iterating 0 to 60 184 | for h2 in range(0,health): 185 | for a2 in range(0,arrows): 186 | for s2 in range(0,stamina): 187 | 188 | if check(h1,a1,s1,h2,a2,s2,0,0,0)==1: #selfloop 189 | newcol.append(0) 190 | continue 191 | 192 | #callaction returns probalilty of action causing inflow into h2,a2,s2 193 | #and outflow from h1,a1,s1. 194 | prob=round(callaction(ac,h1*25,a1,s1*50,h2*25,a2,s2*50),3) 195 | out+=prob 196 | newcol.append(-prob) #inflow 197 | 198 | #exception case oof noop 199 | if ac==3: 200 | out=1 201 | 202 | #now adding the outflow of 203 | newcol[comptonum(h1,a1,s1)]=out 204 | 205 | a.append(newcol) 206 | 207 | #appending to the actions lis 208 | actions.append(tempac) 209 | 210 | # a and actions are prepared as list of lists 211 | # number of columns is stored in column 212 | 213 | # print(r) 214 | 215 | #initial probabilities 216 | alpha=[0]*60 217 | alpha[comptonum(4,3,2)]=1 #probability of starting here is 1 218 | 219 | 220 | #create the json object and add these lists to the json before they are used in cvxpy 221 | send = {} 222 | 223 | 224 | #Fields to include in the JSON : 225 | 226 | #The A matrix - a 227 | a=np.array(a) 228 | a=np.transpose(a) 229 | send["a"] = a.tolist() 230 | # print(a) 231 | # print(a.shape) 232 | 233 | send["r"]= r 234 | send["alpha"] = alpha 235 | 236 | #The R array - r 237 | r=np.array(r) 238 | # print("\n",r) 239 | 240 | #The alpha array - alpha 241 | alpha=np.array(alpha) 242 | alpha.shape=(states,1) 243 | # print("\n",alpha) 244 | 245 | 246 | x = cp.Variable(shape=(columns,1), name="x") 247 | 248 | 249 | constraints = [cp.matmul(a, x) == alpha, x>=0] 250 | objective = cp.Maximize(cp.sum(cp.matmul(r,x), axis=0)) 251 | problem = cp.Problem(objective, constraints) 252 | 253 | solution = problem.solve() 254 | 255 | x_send=[] 256 | 257 | for it in x.value: 258 | x_send.append(it[0]) 259 | 260 | send["x"] = x_send 261 | 262 | policy=[] 263 | index=0 264 | st = 0 265 | 266 | for ac in actions: 267 | #each element is an array 268 | # print(ac) 269 | num=index 270 | max_value = x_send[index] 271 | ac_taken=ac[0] 272 | allx=[] 273 | for i in range(len(ac)): 274 | allx.append(x_send[num]) 275 | if x_send[num]>=max_value: 276 | max_value = x_send[num] 277 | ac_taken=ac[i] 278 | num+=1 279 | index+=len(ac) 280 | h,a,s=numtocomp(st) 281 | st+=1 282 | # print("\nx's:",allx) 283 | # print("actions",ac) 284 | haslis=[] 285 | haslis.append(h) 286 | haslis.append(a) 287 | haslis.append(s) 288 | temp=[] 289 | temp.append(haslis) 290 | temp.append(mapactions(ac_taken)) 291 | policy.append(temp) 292 | # print(h,a,s, mapactions(ac_taken),ac_taken,ac,allx) 293 | 294 | send["policy"] = policy 295 | send["objective"]=solution 296 | 297 | 298 | # Dumping the JSON object into the directory 299 | 300 | # original = sys.stdout 301 | # os.mkdir("./outputs") 302 | # sys.stdout = original 303 | 304 | # file_object = open("outputs/output.json", 'w') 305 | # json.dump(send, file_object) 306 | 307 | 308 | filename="./outputs/output.json" 309 | os.makedirs(os.path.dirname(filename), exist_ok=True) 310 | with open(filename, "w") as file_object: 311 | json.dump(send,file_object) -------------------------------------------------------------------------------- /A2-3/intermediate/sample.py: -------------------------------------------------------------------------------- 1 | import cvxpy as cp 2 | import numpy as np 3 | 4 | x = cp.Variable(shape=(2,1), name="x") 5 | A = np.array([[4,3],[-3,4]]) 6 | 7 | constraints = [cp.matmul(A, x) <= 12, x<=2, x>=0] 8 | objective = cp.Maximize(cp.sum(x, axis=0)) 9 | problem = cp.Problem(objective, constraints) 10 | 11 | solution = problem.solve() 12 | print(solution) 13 | 14 | print(x.value) -------------------------------------------------------------------------------- /A2-3/intermediate/slides.py: -------------------------------------------------------------------------------- 1 | import cvxpy as cp 2 | import numpy as np 3 | 4 | x = cp.Variable(shape=(9,1), name="x") 5 | A = np.array([[1,1,0,0,0,0,0,0,0],[-1,0,1,0,0,0,0,0,0],[0,-1,0,1,0.5,0.2,0,0,0],[0,0,0,-1,0,0,1,0,0],[0,0,0,0,0,-0.2,0,1,0],[0,0,0,0,-0.5,0,0,0,1]]) 6 | alpha = np.array([0.1,0.1,0.1,0.1,0.1,0.5]) 7 | r=np.array([0,0,5,1,1,1,-10,50,60]) 8 | 9 | alpha.shape=(6,1) 10 | 11 | print(alpha) 12 | 13 | constraints = [x>=0,cp.matmul(A, x) <= alpha, cp.matmul(A, x) >= alpha] 14 | objective = cp.Maximize(cp.sum(cp.matmul(r,x), axis=0)) 15 | problem = cp.Problem(objective, constraints) 16 | 17 | solution = problem.solve() 18 | print(solution) 19 | 20 | print(x.value) -------------------------------------------------------------------------------- /A2-3/intermediate/states_actions.txt: -------------------------------------------------------------------------------- 1 | 1 . In state 0 0 0 taking action NOOP 2 | 2 . In state 0 0 1 taking action NOOP 3 | 3 . In state 0 0 2 taking action NOOP 4 | 4 . In state 0 1 0 taking action NOOP 5 | 5 . In state 0 1 1 taking action NOOP 6 | 6 . In state 0 1 2 taking action NOOP 7 | 7 . In state 0 2 0 taking action NOOP 8 | 8 . In state 0 2 1 taking action NOOP 9 | 9 . In state 0 2 2 taking action NOOP 10 | 10 . In state 0 3 0 taking action NOOP 11 | 11 . In state 0 3 1 taking action NOOP 12 | 12 . In state 0 3 2 taking action NOOP 13 | 13 . In state 1 0 0 taking action RECHARGE 14 | 14 . In state 1 0 1 taking action DODGE 15 | 15 . In state 1 0 1 taking action RECHARGE 16 | 16 . In state 1 0 2 taking action DODGE 17 | 17 . In state 1 1 0 taking action RECHARGE 18 | 18 . In state 1 1 1 taking action SHOOT 19 | 19 . In state 1 1 1 taking action DODGE 20 | 20 . In state 1 1 1 taking action RECHARGE 21 | 21 . In state 1 1 2 taking action SHOOT 22 | 22 . In state 1 1 2 taking action DODGE 23 | 23 . In state 1 2 0 taking action RECHARGE 24 | 24 . In state 1 2 1 taking action SHOOT 25 | 25 . In state 1 2 1 taking action DODGE 26 | 26 . In state 1 2 1 taking action RECHARGE 27 | 27 . In state 1 2 2 taking action SHOOT 28 | 28 . In state 1 2 2 taking action DODGE 29 | 29 . In state 1 3 0 taking action RECHARGE 30 | 30 . In state 1 3 1 taking action SHOOT 31 | 31 . In state 1 3 1 taking action DODGE 32 | 32 . In state 1 3 1 taking action RECHARGE 33 | 33 . In state 1 3 2 taking action SHOOT 34 | 34 . In state 1 3 2 taking action DODGE 35 | 35 . In state 2 0 0 taking action RECHARGE 36 | 36 . In state 2 0 1 taking action DODGE 37 | 37 . In state 2 0 1 taking action RECHARGE 38 | 38 . In state 2 0 2 taking action DODGE 39 | 39 . In state 2 1 0 taking action RECHARGE 40 | 40 . In state 2 1 1 taking action SHOOT 41 | 41 . In state 2 1 1 taking action DODGE 42 | 42 . In state 2 1 1 taking action RECHARGE 43 | 43 . In state 2 1 2 taking action SHOOT 44 | 44 . In state 2 1 2 taking action DODGE 45 | 45 . In state 2 2 0 taking action RECHARGE 46 | 46 . In state 2 2 1 taking action SHOOT 47 | 47 . In state 2 2 1 taking action DODGE 48 | 48 . In state 2 2 1 taking action RECHARGE 49 | 49 . In state 2 2 2 taking action SHOOT 50 | 50 . In state 2 2 2 taking action DODGE 51 | 51 . In state 2 3 0 taking action RECHARGE 52 | 52 . In state 2 3 1 taking action SHOOT 53 | 53 . In state 2 3 1 taking action DODGE 54 | 54 . In state 2 3 1 taking action RECHARGE 55 | 55 . In state 2 3 2 taking action SHOOT 56 | 56 . In state 2 3 2 taking action DODGE 57 | 57 . In state 3 0 0 taking action RECHARGE 58 | 58 . In state 3 0 1 taking action DODGE 59 | 59 . In state 3 0 1 taking action RECHARGE 60 | 60 . In state 3 0 2 taking action DODGE 61 | 61 . In state 3 1 0 taking action RECHARGE 62 | 62 . In state 3 1 1 taking action SHOOT 63 | 63 . In state 3 1 1 taking action DODGE 64 | 64 . In state 3 1 1 taking action RECHARGE 65 | 65 . In state 3 1 2 taking action SHOOT 66 | 66 . In state 3 1 2 taking action DODGE 67 | 67 . In state 3 2 0 taking action RECHARGE 68 | 68 . In state 3 2 1 taking action SHOOT 69 | 69 . In state 3 2 1 taking action DODGE 70 | 70 . In state 3 2 1 taking action RECHARGE 71 | 71 . In state 3 2 2 taking action SHOOT 72 | 72 . In state 3 2 2 taking action DODGE 73 | 73 . In state 3 3 0 taking action RECHARGE 74 | 74 . In state 3 3 1 taking action SHOOT 75 | 75 . In state 3 3 1 taking action DODGE 76 | 76 . In state 3 3 1 taking action RECHARGE 77 | 77 . In state 3 3 2 taking action SHOOT 78 | 78 . In state 3 3 2 taking action DODGE 79 | 79 . In state 4 0 0 taking action RECHARGE 80 | 80 . In state 4 0 1 taking action DODGE 81 | 81 . In state 4 0 1 taking action RECHARGE 82 | 82 . In state 4 0 2 taking action DODGE 83 | 83 . In state 4 1 0 taking action RECHARGE 84 | 84 . In state 4 1 1 taking action SHOOT 85 | 85 . In state 4 1 1 taking action DODGE 86 | 86 . In state 4 1 1 taking action RECHARGE 87 | 87 . In state 4 1 2 taking action SHOOT 88 | 88 . In state 4 1 2 taking action DODGE 89 | 89 . In state 4 2 0 taking action RECHARGE 90 | 90 . In state 4 2 1 taking action SHOOT 91 | 91 . In state 4 2 1 taking action DODGE 92 | 92 . In state 4 2 1 taking action RECHARGE 93 | 93 . In state 4 2 2 taking action SHOOT 94 | 94 . In state 4 2 2 taking action DODGE 95 | 95 . In state 4 3 0 taking action RECHARGE 96 | 96 . In state 4 3 1 taking action SHOOT 97 | 97 . In state 4 3 1 taking action DODGE 98 | 98 . In state 4 3 1 taking action RECHARGE 99 | 99 . In state 4 3 2 taking action SHOOT 100 | 100 . In state 4 3 2 taking action DODGE 101 | -------------------------------------------------------------------------------- /A2-3/intermediate/temp: -------------------------------------------------------------------------------- 1 | 0 0 0 NOOP 3 [3] [0.9999999998553312] 2 | 0 0 1 NOOP 3 [3] [5.24141750865167e-11] 3 | 0 0 2 NOOP 3 [3] [2.1564469633988095e-13] 4 | 0 1 0 NOOP 3 [3] [4.9808692255746475e-11] 5 | 0 1 1 NOOP 3 [3] [2.5146159079567816e-11] 6 | 0 1 2 NOOP 3 [3] [2.1564469633988095e-13] 7 | 0 2 0 NOOP 3 [3] [-3.323702993712063e-12] 8 | 0 2 1 NOOP 3 [3] [1.8466512153631013e-11] 9 | 0 2 2 NOOP 3 [3] [2.1564469633988095e-13] 10 | 0 3 0 NOOP 3 [3] [2.1564469633988095e-13] 11 | 0 3 1 NOOP 3 [3] [2.1564469633988095e-13] 12 | 0 3 2 NOOP 3 [3] [2.1564469633988095e-13] 13 | 1 0 0 RECHARGE 2 [2] [1.9897848664358744] 14 | 1 0 1 DODGE 1 [1, 2] [1.853306520643897, 1.8616972316884759e-10] 15 | 1 0 2 DODGE 1 [1] [1.4891790896051147e-10] 16 | 1 1 0 RECHARGE 2 [2] [2.1076851134307515] 17 | 1 1 1 SHOOT 0 [0, 1, 2] [1.9999999997106606, -2.0768350414079397e-11, 8.833242341839074e-11] 18 | 1 1 2 SHOOT 0 [0, 1] [1.0455860406133138e-10, -3.3631141401507276e-11] 19 | 1 2 0 RECHARGE 2 [2] [1.0392410848869202e-10] 20 | 1 2 1 SHOOT 0 [0, 1, 2] [9.943746045060543e-11, 6.556382259777458e-11, 2.658025516740928e-11] 21 | 1 2 2 SHOOT 0 [0, 1] [5.000620287484579e-11, -2.8563420783089985e-11] 22 | 1 3 0 RECHARGE 2 [2] [-2.4427219305574956e-11] 23 | 1 3 1 SHOOT 0 [0, 1, 2] [-6.931650643958877e-12, -5.5863935496054997e-11, -2.567510564158653e-11] 24 | 1 3 2 SHOOT 0 [0, 1] [3.661951273761467e-11, -5.694867404589408e-11] 25 | 2 0 0 RECHARGE 2 [2] [0.7276366279570717] 26 | 2 0 1 DODGE 1 [1, 2] [1.0231929139721037, 3.934364166049069e-10] 27 | 2 0 2 DODGE 1 [1] [3.147525569367137e-10] 28 | 2 1 0 RECHARGE 2 [2] [1.6715677982984123] 29 | 2 1 1 DODGE 1 [0, 1, 2] [0.4423331783172452, 0.8959664689324771, 0.6536965690037218] 30 | 2 1 2 SHOOT 0 [0, 1] [0.5229572551360284, 6.695425021032081e-11] 31 | 2 2 0 RECHARGE 2 [2] [1.067521381855358] 32 | 2 2 1 RECHARGE 2 [0, 1, 2] [0.4070057483228572, 5.170865900598965e-10, 0.784629772255621] 33 | 2 2 2 SHOOT 0 [0, 1] [0.6277038178020337, 2.4664854290292708e-12] 34 | 2 3 0 RECHARGE 2 [2] [4.4243912119919246e-10] 35 | 2 3 1 RECHARGE 2 [0, 1, 2] [1.9370968891539547e-10, -5.080470312808086e-11, 2.2163594988942898e-10] 36 | 2 3 2 SHOOT 0 [0, 1] [2.2184911011170004e-10, -4.40137410774797e-11] 37 | 3 0 0 RECHARGE 2 [2] [0.4196673712294868] 38 | 3 0 1 DODGE 1 [1, 2] [0.5847434238066058, 3.873536049747281e-10] 39 | 3 0 2 DODGE 1 [1] [3.0985410718341266e-10] 40 | 3 1 0 RECHARGE 2 [2] [0.9857047598498768] 41 | 3 1 1 DODGE 1 [0, 1, 2] [0.3126082608000465, 0.45403542777978617, 0.4490124608058079] 42 | 3 1 2 SHOOT 0 [0, 1] [0.35920996860563414, 3.897596648148438e-11] 43 | 3 2 0 RECHARGE 2 [2] [0.9184052204740976] 44 | 3 2 1 DODGE 1 [0, 1, 2] [0.27200747892016325, 0.7948409777739095, 0.5253768868260624] 45 | 3 2 2 SHOOT 0 [0, 1] [0.42030150940159594, 5.922253494857529e-11] 46 | 3 3 0 RECHARGE 2 [2] [0.7948409777034572] 47 | 3 3 1 RECHARGE 2 [0, 1, 2] [0.2744878602544394, -5.695636785058644e-11, 0.451731152459718] 48 | 3 3 2 SHOOT 0 [0, 1] [0.36138492201196626, -4.421831867276748e-11] 49 | 4 0 0 RECHARGE 2 [2] [0.11931560984685999] 50 | 4 0 1 DODGE 1 [1, 2] [0.16485703023986875, 6.217251151094565e-10] 51 | 4 0 2 DODGE 1 [1] [4.970917492707779e-10] 52 | 4 1 0 RECHARGE 2 [2] [0.31018349553128005] 53 | 4 1 1 RECHARGE 2 [0, 1, 2] [0.12496216361979141, 0.11151464190990663, 0.17351135708727583] 54 | 4 1 2 SHOOT 0 [0, 1] [0.1388090855611234, 1.0846279672060183e-10] 55 | 4 2 0 RECHARGE 2 [2] [0.29750492574752385] 56 | 4 2 1 DODGE 1 [0, 1, 2] [0.18791648753495024, 0.36754259292486047, 0.31784773619360346] 57 | 4 2 2 SHOOT 0 [0, 1] [0.25427818887857045, 7.612905948719039e-11] 58 | 4 3 0 RECHARGE 2 [2] [0.3675425928339313] 59 | 4 3 1 RECHARGE 2 [0, 1, 2] [0.15056741692974118, -7.033752846994573e-11, 0.17933332174838204] 60 | 4 3 2 SHOOT 0 [0, 1] [1.1434666574705614, -7.197765296998317e-11] 61 | -------------------------------------------------------------------------------- /A2-3/submission/solution.py: -------------------------------------------------------------------------------- 1 | import cvxpy as cp 2 | import numpy as np 3 | import json 4 | import os 5 | import sys 6 | 7 | def check(health1,arrows1,stamina1,health2,arrows2,stamina2,dh,da,ds): 8 | if (dh==health1-health2) and (da==arrows1-arrows2) and (ds==stamina1-stamina2): 9 | return 1 10 | else: 11 | return 0 12 | 13 | 14 | def shoot(h1,a1,s1,h2,a2,s2): 15 | 16 | #no arrows or no stamina 17 | if a1==0 or s1==0: 18 | return check(h1,a1,s1,h2,a2,s2,0,0,0) 19 | 20 | #misses MD 21 | elif check(h1,a1,s1,h2,a2,s2,0,1,50): 22 | return 0.5 23 | 24 | #hits MD 25 | elif check(h1,a1,s1,h2,a2,s2,25,1,50): 26 | return 0.5 27 | 28 | else: 29 | return 0 30 | 31 | def dodge (h1,a1,s1,h2,a2,s2): 32 | 33 | #zero stamina 34 | if s1==0: 35 | return 0 36 | 37 | #stamina is 50 38 | elif s1==50: 39 | 40 | #arrows already full 41 | if a1==3 and check(h1,a1,s1,h2,a2,s2,0,0,50): 42 | return 1 43 | 44 | #unsuccessful arrow pickup 45 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 46 | return 0.2 47 | 48 | #successful arrow pickup 49 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 50 | return 0.8 51 | 52 | 53 | #stamina is 100 54 | elif s1==100: 55 | 56 | #3 arrows 57 | if(a1==3): 58 | 59 | #lose 50 stamins 60 | if check(h1,a1,s1,h2,a2,s2,0,0,50): 61 | return 0.8 62 | 63 | #lose 100 stamina 64 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 65 | return 0.2 66 | 67 | 68 | #successful arrow pickup, 50 stamina loss 69 | elif check(h1,a1,s1,h2,a2,s2,0,-1,50): 70 | return 0.8*0.8 71 | 72 | #unsuccessful arrow pickup, 50 stamina loss 73 | elif check(h1,a1,s1,h2,a2,s2,0,0,50): 74 | return 0.2*0.8 75 | 76 | #successful arrow pickup, 100 stamina loss 77 | elif check(h1,a1,s1,h2,a2,s2,0,-1,100): 78 | return 0.8*0.2 79 | 80 | #unsuccessful arrow pickup, 100 stamina loss 81 | elif check(h1,a1,s1,h2,a2,s2,0,0,100): 82 | return 0.2*0.2 83 | 84 | return 0 85 | 86 | def recharge (h1,a1,s1,h2,a2,s2): 87 | 88 | #stamina is 100 89 | if(s1==100): 90 | 91 | if check(h1,a1,s1,h2,a2,s2,0,0,0)==1: 92 | return 1 93 | else: 94 | return 0 95 | 96 | #stamina not 100 97 | else: 98 | if check(h1,a1,s1,h2,a2,s2,0,0,-50): #since stamina increase therefore old-new = -50 99 | return 0.8 100 | elif check(h1,a1,s1,h2,a2,s2,0,0,0): 101 | return 0.2 102 | 103 | else: 104 | return 0 105 | 106 | def numtocomp(num): 107 | s=num%3 108 | a=(int(num/3))%4 109 | h=(int(num/12))%5 110 | 111 | return h,a,s 112 | 113 | def comptonum(h,a,s): 114 | num=h*12+a*3+s 115 | return num 116 | 117 | def mapactions(ac): 118 | if ac==0: 119 | return "SHOOT" 120 | elif ac==1: 121 | return "DODGE" 122 | elif ac==2: 123 | return "RECHARGE" 124 | elif ac==3: 125 | return "NOOP" 126 | else: 127 | return "ERROR" 128 | 129 | def callaction(ac,h1,a1,s1,h2,a2,s2): 130 | 131 | if ac==0: 132 | return shoot(h1,a1,s1,h2,a2,s2) 133 | 134 | elif ac==1: 135 | return dodge(h1,a1,s1,h2,a2,s2) 136 | 137 | elif ac==2: 138 | return recharge(h1,a1,s1,h2,a2,s2) 139 | 140 | elif ac==3: 141 | return 0 142 | 143 | actionsnum=4 #shoot,dodge,recharge,noop 144 | health=5 #mult by 25 145 | arrows=4 146 | stamina=3 #mult by 50 147 | states = 60 148 | 149 | columns=0 # every time we find an action we can add a column 150 | r=[] #rewards array 151 | reward = -5 152 | actions=[] 153 | a=[] 154 | for h1 in range(0,health): 155 | for a1 in range(0,arrows): 156 | for s1 in range(0,stamina): 157 | #for each state 158 | 159 | #storing the actions possible 160 | tempac=[] 161 | for ac in range(0,actionsnum): 162 | # for each action 163 | 164 | #noop conditions 165 | if (ac!=3 and h1==0) or (ac==3 and h1!=0): 166 | continue 167 | 168 | #conditions for skipping actions 169 | if (a1==0 and ac==0) or (s1==0 and ac==0) or (s1==0 and ac==1) or (s1==2 and ac==2): 170 | continue 171 | 172 | #reward =0 for terminal, reward = -5 for others 173 | if(h1==0): 174 | r.append(0) 175 | else: 176 | r.append(reward) 177 | 178 | columns+=1 179 | # print(columns, ". In state ", h1,a1,s1,"taking action ", mapactions(ac)) 180 | tempac.append(ac) 181 | newcol=[] 182 | out=0 183 | #making the column, we are iterating 0 to 60 184 | for h2 in range(0,health): 185 | for a2 in range(0,arrows): 186 | for s2 in range(0,stamina): 187 | 188 | if check(h1,a1,s1,h2,a2,s2,0,0,0)==1: #selfloop 189 | newcol.append(0) 190 | continue 191 | 192 | #callaction returns probalilty of action causing inflow into h2,a2,s2 193 | #and outflow from h1,a1,s1. 194 | prob=round(callaction(ac,h1*25,a1,s1*50,h2*25,a2,s2*50),3) 195 | out+=prob 196 | newcol.append(-prob) #inflow 197 | 198 | #exception case oof noop 199 | if ac==3: 200 | out=1 201 | 202 | #now adding the outflow of 203 | newcol[comptonum(h1,a1,s1)]=out 204 | 205 | a.append(newcol) 206 | 207 | #appending to the actions lis 208 | actions.append(tempac) 209 | 210 | # a and actions are prepared as list of lists 211 | # number of columns is stored in column 212 | 213 | # print(r) 214 | 215 | #initial probabilities 216 | alpha=[0]*60 217 | alpha[comptonum(4,3,2)]=1 #probability of starting here is 1 218 | 219 | 220 | #create the json object and add these lists to the json before they are used in cvxpy 221 | send = {} 222 | 223 | 224 | #Fields to include in the JSON : 225 | 226 | #The A matrix - a 227 | a=np.array(a) 228 | a=np.transpose(a) 229 | send["a"] = a.tolist() 230 | # print(a) 231 | # print(a.shape) 232 | 233 | send["r"]= r 234 | send["alpha"] = alpha 235 | 236 | #The R array - r 237 | r=np.array(r) 238 | # print("\n",r) 239 | 240 | #The alpha array - alpha 241 | alpha=np.array(alpha) 242 | alpha.shape=(states,1) 243 | # print("\n",alpha) 244 | 245 | 246 | x = cp.Variable(shape=(columns,1), name="x") 247 | 248 | 249 | constraints = [cp.matmul(a, x) == alpha, x>=0] 250 | objective = cp.Maximize(cp.sum(cp.matmul(r,x), axis=0)) 251 | problem = cp.Problem(objective, constraints) 252 | 253 | solution = problem.solve() 254 | 255 | x_send=[] 256 | 257 | for it in x.value: 258 | x_send.append(it[0]) 259 | 260 | send["x"] = x_send 261 | 262 | policy=[] 263 | index=0 264 | st = 0 265 | 266 | for ac in actions: 267 | #each element is an array 268 | # print(ac) 269 | num=index 270 | max_value = x_send[index] 271 | ac_taken=ac[0] 272 | allx=[] 273 | for i in range(len(ac)): 274 | allx.append(x_send[num]) 275 | if x_send[num]>=max_value: 276 | max_value = x_send[num] 277 | ac_taken=ac[i] 278 | num+=1 279 | index+=len(ac) 280 | h,a,s=numtocomp(st) 281 | st+=1 282 | # print("\nx's:",allx) 283 | # print("actions",ac) 284 | haslis=[] 285 | haslis.append(h) 286 | haslis.append(a) 287 | haslis.append(s) 288 | temp=[] 289 | temp.append(haslis) 290 | temp.append(mapactions(ac_taken)) 291 | policy.append(temp) 292 | # print(h,a,s, mapactions(ac_taken),ac_taken,ac,allx) 293 | 294 | send["policy"] = policy 295 | send["objective"]=solution 296 | 297 | 298 | # Dumping the JSON object into the directory 299 | 300 | # original = sys.stdout 301 | # os.mkdir("./outputs") 302 | # sys.stdout = original 303 | 304 | # file_object = open("outputs/output.json", 'w') 305 | # json.dump(send, file_object) 306 | 307 | 308 | filename="./outputs/output.json" 309 | os.makedirs(os.path.dirname(filename), exist_ok=True) 310 | with open(filename, "w") as file_object: 311 | json.dump(send,file_object) -------------------------------------------------------------------------------- /A2-3/submission/team_62_report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A2-3/submission/team_62_report.pdf -------------------------------------------------------------------------------- /A3/Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/Report.pdf -------------------------------------------------------------------------------- /A3/__pycache__/client_moodle.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/__pycache__/client_moodle.cpython-36.pyc -------------------------------------------------------------------------------- /A3/__pycache__/tester.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/__pycache__/tester.cpython-36.pyc -------------------------------------------------------------------------------- /A3/client_moodle.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | import numpy as np 4 | 5 | ######### DO NOT CHANGE ANYTHING IN THIS FILE ################## 6 | API_ENDPOINT = 'http://10.4.21.147' 7 | PORT = 3000 8 | MAX_DEG = 11 9 | 10 | #### functions that you can call 11 | def get_errors(id, vector): 12 | """ 13 | returns python array of length 2 14 | (train error and validation error) 15 | """ 16 | for i in vector: assert -10<=abs(i)<=10 17 | assert len(vector) == MAX_DEG 18 | 19 | return json.loads(send_request(id, vector, 'geterrors')) 20 | 21 | def submit(id, vector): 22 | """ 23 | used to make official submission of your weight vector 24 | returns string "successfully submitted" if properly submitted. 25 | """ 26 | for i in vector: assert -10<=abs(i)<=10 27 | assert len(vector) == MAX_DEG 28 | return send_request(id, vector, 'submit') 29 | 30 | #### utility functions 31 | def urljoin(root, port, path=''): 32 | root = root + ':' + str(port) 33 | if path: root = '/'.join([root.rstrip('/'), path.rstrip('/')]) 34 | return root 35 | 36 | def send_request(id, vector, path): 37 | api = urljoin(API_ENDPOINT, PORT, path) 38 | vector = json.dumps(vector) 39 | response = requests.post(api, data={'id':id, 'vector':vector}).text 40 | if "reported" in response: 41 | print(response) 42 | exit() 43 | 44 | return response 45 | 46 | 47 | if __name__ == "__main__": 48 | """ 49 | Replace "test" with your secret ID and just run this file 50 | to verify that the server is working for your ID. 51 | """ 52 | 53 | err = get_errors('847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo', list(-np.arange(0,1.1,0.1))) 54 | assert len(err) == 2 55 | 56 | submit_status = submit('847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo', list(-np.arange(0,1.1,0.1))) 57 | assert "submitted" in submit_status 58 | -------------------------------------------------------------------------------- /A3/diagrams/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/2.png -------------------------------------------------------------------------------- /A3/diagrams/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/3.png -------------------------------------------------------------------------------- /A3/diagrams/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/4.png -------------------------------------------------------------------------------- /A3/diagrams/diag_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/diag_2.png -------------------------------------------------------------------------------- /A3/diagrams/diag_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/diag_3.png -------------------------------------------------------------------------------- /A3/diagrams/diag_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/diagrams/diag_4.png -------------------------------------------------------------------------------- /A3/good-error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/good-error.png -------------------------------------------------------------------------------- /A3/intermediate/code.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | # import tester as server 3 | import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.1 11 | pop_size=40 12 | cross_n=int(pop_size/2) 13 | iter=62 14 | 15 | def mutation(vector,index=-1,mut_prob=0.1): 16 | #chooses a random float in -range to +range and makes change at index position in vector 17 | 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity= np.random.choice((0,1),p=[1-mut_prob,mut_prob]) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def mutateall(vector): 30 | 31 | for i in range(len(vector)): 32 | vector=mutation(vector,i,0.85) 33 | return vector 34 | 35 | def crossover(vector1, vector2, index=-1): 36 | #performs a crossover from index onwards 37 | 38 | #if no index is passed chooses a random index 39 | send1=[] 40 | send2=[] 41 | 42 | if index==-1: 43 | index=random.randint(0,MAX_DEG-1) 44 | 45 | for i in range(MAX_DEG): 46 | parity=random.randint(0,1) 47 | if parity==1: 48 | send1.append(vector1[i]) 49 | send2.append(vector2[i]) 50 | else: 51 | send1.append(vector2[i]) 52 | send2.append(vector1[i]) 53 | return send1, send2 54 | 55 | else: 56 | for i in range(MAX_DEG): 57 | if i0.000001): 84 | return 0 85 | 86 | return 1 87 | 88 | def main(): 89 | 90 | print("PC: " ,pc, " POP_SIZE: ",pop_size," ITER : ", iter) 91 | 92 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 93 | # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] 94 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 95 | min_error=-1 96 | min_error1=-1 97 | min_error2=-1 98 | 99 | parenterrors=np.zeros(pop_size) 100 | parenterrors1=np.zeros(pop_size) 101 | parenterrors2=np.zeros(pop_size) 102 | parentprobalities=np.zeros(pop_size) 103 | population=np.zeros((pop_size,MAX_DEG)) 104 | 105 | # generate the population 106 | for i in range(pop_size): 107 | temp=np.copy(vector_og) 108 | population[i]=mutateall(temp) 109 | 110 | #generate errors for each individual in the population 111 | for j in range(pop_size): 112 | temp=population[j].tolist() #passing a list to the get_errors function 113 | err=server.get_errors(key,temp) 114 | 115 | #adding the two errors and storing in parenterror 116 | parenterrors[j]=(err[0]+err[1]) 117 | parenterrors1[j]=(err[0]) 118 | parenterrors2[j]=(err[1]) 119 | 120 | bank=np.copy(population) 121 | bankerrors=np.copy(parenterrors) 122 | bankerrors1=np.copy(parenterrors1) 123 | bankerrors2=np.copy(parenterrors2) 124 | 125 | # have to change this to a while loop with appropriate condition later 126 | for iter_num in range(iter): 127 | new_iter=0 128 | 129 | print("\n\n\n\n********"+str(iter_num)+"*********") 130 | 131 | 132 | # Sort the errors in ascending order 133 | # Least error => max fittness 134 | # Correspondingly sort the population also 135 | parenterrorsinds=parenterrors.argsort() 136 | parenterrors=parenterrors[parenterrorsinds[::1]] 137 | parenterrors1=parenterrors1[parenterrorsinds[::1]] 138 | parenterrors2=parenterrors2[parenterrorsinds[::1]] 139 | population=population[parenterrorsinds[::1]] 140 | 141 | 142 | #debug statements 143 | for j in range(pop_size): 144 | print("person " + str(j)+" error "+ str(parenterrors[j])) 145 | print("\tvalues"+str(population[j])+"\n\n") 146 | 147 | # Assign probabilities to the population 148 | parentprobalities=gen_parent_probabilities(pop_size) 149 | 150 | # Checking sum(prob) = 1 151 | # print(np.sum(parentprobalities)) 152 | child_population=np.zeros((pop_size,MAX_DEG)) 153 | 154 | #perform crossover cross_n times 155 | for j in range (cross_n): 156 | arr=crossover_select(parentprobalities) 157 | 158 | # Two parents chosen based on probabilities => arr[0], arr[1] 159 | # Sending parents for crossover 160 | temp=crossover(population[arr[0]],population[arr[1]]) 161 | 162 | child_population[new_iter]=temp[0] 163 | new_iter+=1 164 | 165 | child_population[new_iter]=temp[1] 166 | new_iter+=1 167 | 168 | # print("BEFORE MUTATION: CHILD POPULATION") 169 | # for j in range(len(child_population)): 170 | # print("Child", j) 171 | # print(child_population[i]) 172 | 173 | # Send the new population for mutation 174 | for j in range(pop_size): 175 | temp=np.copy(child_population[j]) 176 | child_population[j]=mutation(temp) 177 | 178 | # print("AFTER MUTATION: CHILD POPULATION") 179 | # for j in range(len(child_population)): 180 | # print("Child", j) 181 | # print(child_population[i]) 182 | 183 | # get the errors for the new population 184 | childerrors=np.zeros(pop_size) 185 | childerrors1=np.zeros(pop_size) 186 | childerrors2=np.zeros(pop_size) 187 | 188 | # generate errors for each child 189 | for j in range(pop_size): 190 | temp=child_population[j].tolist() #passing a list to the get_errors function 191 | err=server.get_errors(key,temp) 192 | 193 | #adding the two errors and storing in parnterror 194 | childerrors[j]=(err[0]+err[1]) 195 | childerrors1[j]=(err[0]) 196 | childerrors2[j]=(err[1]) 197 | 198 | #combining parents and children into one array 199 | candidates=np.concatenate([population,child_population]) 200 | candidate_errors=np.concatenate([parenterrors,childerrors]) 201 | candidate_errors1=np.concatenate([parenterrors1,childerrors1]) 202 | candidate_errors2=np.concatenate([parenterrors2,childerrors2]) 203 | 204 | # sorting all the candidates by error 205 | candidateerrorsinds=candidate_errors.argsort() 206 | candidate_errors=candidate_errors[candidateerrorsinds[::1]] 207 | candidate_errors1=candidate_errors1[candidateerrorsinds[::1]] 208 | candidate_errors2=candidate_errors2[candidateerrorsinds[::1]] 209 | candidates=candidates[candidateerrorsinds[::1]] 210 | 211 | # setting the probability and choosing the indices 212 | # candidate_prob=gen_parent_probabilities(pop_size*2) 213 | # chosenindices=np.random.choice(np.arange(0,2*pop_size),pop_size, replace=False,p=candidate_prob) 214 | 215 | # set population for the next iteration 216 | ind=0 217 | 218 | for i in range(2*pop_size): 219 | # ind=chosenindices[i] 220 | if ind>=pop_size: 221 | break 222 | 223 | checkflag=0 224 | 225 | for j in range(ind): 226 | # print(i,j,ind) 227 | # print(population[i],population[j]) 228 | if check_match(candidates[i],population[j])==1: 229 | checkflag=1 230 | break 231 | 232 | if(checkflag==0): 233 | population[ind]=candidates[i] 234 | parenterrors[ind]=candidate_errors[i] 235 | parenterrors1[ind]=candidate_errors1[i] 236 | parenterrors2[ind]=candidate_errors2[i] 237 | ind+=1 238 | 239 | if (indcandidate_errors[0]): 252 | to_send=candidates[0] 253 | min_error=candidate_errors[0] 254 | min_error1=candidate_errors1[0] 255 | min_error2=candidate_errors2[0] 256 | print("-------------------------------------------------------------------------------\n") 257 | print("Min error = ", min_error,"\n\n") 258 | print("Min error1 = ", min_error1,"\n\n") 259 | print("Min error2 = ", min_error2,"\n\n") 260 | 261 | if(min_error<40000000): 262 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 263 | 264 | return to_send 265 | 266 | to_send=main() 267 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 268 | print("Code finished") 269 | print("-------------------------------------------------------------------------------\n\n") -------------------------------------------------------------------------------- /A3/intermediate/code_withprob.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | # import tester as server 3 | import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.2 11 | pop_size=100 12 | cross_n=int(pop_size/2) 13 | iter=16 14 | 15 | def mutation(vector,index=-1,mut_prob=0.5): 16 | #chooses a random float in -range to +range and makes change at index position in vector 17 | 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity= np.random.choice((0,1),p=[1-mut_prob,mut_prob]) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def mutateall(vector): 30 | 31 | for i in range(len(vector)): 32 | vector=mutation(vector,i,0.3) 33 | return vector 34 | 35 | def crossover(vector1, vector2, index=-1): 36 | #performs a crossover from index onwards 37 | 38 | #if no index is passed chooses a random index 39 | send1=[] 40 | send2=[] 41 | 42 | if index==-1: 43 | index=random.randint(0,MAX_DEG-1) 44 | 45 | for i in range(MAX_DEG): 46 | parity=random.randint(0,1) 47 | if parity==1: 48 | send1.append(vector1[i]) 49 | send2.append(vector2[i]) 50 | else: 51 | send1.append(vector2[i]) 52 | send2.append(vector1[i]) 53 | return send1, send2 54 | 55 | else: 56 | for i in range(MAX_DEG): 57 | if i top two? according to probability? 84 | # for now, choose only the children ignore the parents 85 | # mutate the crossovers 86 | # put them into an array called nextgen[] 87 | 88 | def gen_parent_probabilities(): 89 | parentprobalities=np.zeros(pop_size) 90 | for j in range(pop_size-1): 91 | parentprobalities[j]=((1-pc)**j)*pc 92 | #assign last probability 93 | parentprobalities[pop_size-1]=((1-pc)**(pop_size-1)) 94 | return parentprobalities 95 | 96 | def crossover_select(parentprobalities): 97 | parents=[] 98 | # i1=np.random.choice(np.arange(0,pop_size),p=parentprobalities) 99 | # i2=np.random.choice(np.arange(0,pop_size),2 replace=False,p=parentprobalities) 100 | # parents.append(i1) 101 | # parents.append(i2) 102 | parents=np.random.choice(np.arange(0,pop_size),2, replace=False,p=parentprobalities) 103 | return parents 104 | 105 | def main(): 106 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 107 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 108 | min_error=-1 109 | min_error1=-1 110 | min_error2=-1 111 | 112 | parenterrors=np.zeros(pop_size) 113 | parenterrors1=np.zeros(pop_size) 114 | parenterrors2=np.zeros(pop_size) 115 | parentprobalities=np.zeros(pop_size) 116 | population=np.zeros((pop_size,MAX_DEG)) 117 | 118 | # generate the population 119 | for i in range(pop_size): 120 | temp=np.copy(vector_og) 121 | population[i]=mutateall(temp) 122 | 123 | # have to change this to a while loop with appropriate condition later 124 | for i in range(iter): 125 | new_iter=0 126 | new_population=np.zeros((pop_size,MAX_DEG)) 127 | 128 | #generate errors for each individual in the population 129 | print("\n\n\n\n********"+str(i)+"*********") 130 | for j in range(pop_size): 131 | temp=population[j].tolist() #passing a list to the get_errors function 132 | err=server.get_errors(key,temp) 133 | 134 | #adding the two errors and storing in parenterror 135 | parenterrors[j]=(err[0]+err[1]) 136 | parenterrors1[j]=(err[0]) 137 | parenterrors2[j]=(err[1]) 138 | 139 | # Sort the errors in ascending order 140 | # Least error => max fittness 141 | # Correspondingly sort the population also 142 | parenterrorsinds=parenterrors.argsort() 143 | parenterrors=parenterrors[parenterrorsinds[::1]] 144 | parenterrors1=parenterrors1[parenterrorsinds[::1]] 145 | parenterrors2=parenterrors2[parenterrorsinds[::1]] 146 | population=population[parenterrorsinds[::1]] 147 | 148 | 149 | #set the send array 150 | if(min_error==-1 or min_error>parenterrors[0]): 151 | to_send=population[0] 152 | min_error=parenterrors[0] 153 | min_error1=parenterrors1[0] 154 | min_error2=parenterrors2[0] 155 | 156 | #debug statements 157 | for j in range(pop_size): 158 | print("person " + str(j)+" error "+ str(parenterrors[j])) 159 | print("\tvalues"+str(population[j])+"\n\n") 160 | 161 | # Assign probabilities to the population 162 | parentprobalities=gen_parent_probabilities() 163 | 164 | # Checking sum(prob) = 1 165 | # print(np.sum(parentprobalities)) 166 | 167 | #perform crossover cross_n times 168 | for j in range (cross_n): 169 | arr=crossover_select(parentprobalities) 170 | 171 | # Two parents chosen based on probabilities => arr[0], arr[1] 172 | # Sending parents for crossover 173 | temp=crossover(population[arr[0]],population[arr[1]]) 174 | 175 | #select from the two parents and the two children the ones with min val and train error 176 | childerror0=server.get_errors(key,temp[0]) 177 | childerror1=server.get_errors(key,temp[1]) 178 | # new_iter is the iterator for the new_population numpy 179 | min1=min(childerror0[0],childerror1[0],parenterrors1[arr[0]],parenterrors1[arr[1]]) 180 | min2=min(childerror0[1],childerror1[1],parenterrors2[arr[0]],parenterrors2[arr[1]]) 181 | 182 | if min1==childerror0[0]: 183 | new_population[new_iter]=temp[0] 184 | elif min1==childerror1[0]: 185 | new_population[new_iter]=temp[1] 186 | elif min1==parenterrors1[arr[0]]: 187 | new_population[new_iter]=population[arr[0]] 188 | elif min1==parenterrors1[arr[1]]: 189 | new_population[new_iter]=population[arr[1]] 190 | new_iter+=1 191 | 192 | if min2==childerror0[1]: 193 | new_population[new_iter]=temp[0] 194 | elif min2==childerror1[1]: 195 | new_population[new_iter]=temp[1] 196 | elif min2==parenterrors2[arr[0]]: 197 | new_population[new_iter]=population[arr[0]] 198 | elif min2==parenterrors2[arr[1]]: 199 | new_population[new_iter]=population[arr[1]] 200 | new_iter+=1 201 | # Send the new population for mutation 202 | for j in range(pop_size): 203 | temp=np.copy(new_population[j]) 204 | new_population[j]=mutation(temp) 205 | 206 | # print(new_population) 207 | population=np.copy(new_population) 208 | 209 | print("-------------------------------------------------------------------------------\n") 210 | print("Min error = ", min_error,"\n\n") 211 | print("Min error1 = ", min_error1,"\n\n") 212 | print("Min error2 = ", min_error2,"\n\n") 213 | return to_send 214 | 215 | to_send=main() 216 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 217 | print("Code finished") 218 | print("-------------------------------------------------------------------------------\n\n") 219 | -------------------------------------------------------------------------------- /A3/intermediate/fresh.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | # import tester as server 3 | import client_moodle as server 4 | import random 5 | 6 | team_name = "team_62" # for our reference 7 | MAX_DEG = 11 # number of features 8 | key = '847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger = 10 10 | pc = 0.2 11 | pop_size = 10 12 | select_sure = 2 13 | cross_select_from = 3 14 | crossover_no = 5 15 | iter = 40 16 | 17 | 18 | def mutateall(temp,prob, mutate_range): 19 | vector = np.copy(temp) 20 | for i in range(len(vector)): 21 | fact=random.uniform(-mutate_range, mutate_range) 22 | vector[i] = np.random.choice([vector[i]*(fact+1), vector[i]], p=[prob,1-prob]) 23 | if(vector[i]<-10) : 24 | vector[i]=-10 25 | elif(vector[i]>10) : 26 | vector[i]=10 27 | 28 | return vector 29 | 30 | 31 | # def crossover(vector1, vector2, index=-1): 32 | # send1 = [] 33 | # send2 = [] 34 | 35 | # index = random.randint(0, MAX_DEG-1) 36 | 37 | # for i in range(MAX_DEG): 38 | # parity = random.randint(0, 1) 39 | # if parity == 1: 40 | # send1.append(vector1[i]) 41 | # send2.append(vector2[i]) 42 | # else: 43 | # send1.append(vector2[i]) 44 | # send2.append(vector1[i]) 45 | 46 | # return mutation(send1), mutation(send2) 47 | def crossover(vector1, vector2, mutate_range,prob_mut_cross, index=-1): 48 | send1 = vector1.tolist() 49 | send2 = vector2.tolist() 50 | 51 | # a = np.random.choice(np.arange(0, 11), crossover_no, replace=False) 52 | a = np.random.choice(np.arange(0, 11),5, replace=False) 53 | 54 | for i in a.tolist(): 55 | send1[i] = np.copy(vector2[i]) 56 | send2[i] = np.copy(vector1[i]) 57 | 58 | return mutateall(send1,prob_mut_cross,mutate_range), mutateall(send2,prob_mut_cross,mutate_range) 59 | 60 | 61 | def crossover_select2(parenterrors, num): 62 | return random.sample(range(num), 2) 63 | 64 | 65 | def check_match(vector1, vector2): 66 | # return 1 if match 67 | count = 0 68 | for i in range(MAX_DEG): 69 | if (abs(vector1[i]-vector2[i]) > 0.5): 70 | count += 1 71 | if count >= 5: 72 | return 0 73 | else: 74 | return 1 75 | 76 | 77 | def main(): 78 | 79 | mutate_range=0.1 80 | prob_mut_cross = 0.5 81 | print("pop_size:", pop_size, "iter:", iter, "cross_select_from",cross_select_from) 82 | print("select_sure",select_sure,"prob_mut_cross",prob_mut_cross,"mutate_range",mutate_range) 83 | 84 | vector_og = [-1e-19, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, - 85 | 6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 86 | to_send = [-20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20] 87 | min_error = -1 88 | min_error1 = -1 89 | min_error2 = -1 90 | 91 | parenterrors = np.zeros(pop_size) 92 | parenterrors1 = np.zeros(pop_size) 93 | parenterrors2 = np.zeros(pop_size) 94 | # parentprobalities = np.zeros(pop_size) 95 | population = np.zeros((pop_size, MAX_DEG)) 96 | 97 | # generate the population 98 | for i in range(pop_size): 99 | temp = np.copy(vector_og) 100 | population[i] = np.copy(mutateall(temp,0.85,mutate_range)) 101 | 102 | # generate errors for each individual in the population 103 | for j in range(pop_size): 104 | # passing a list to the get_errors function 105 | temp = population[j].tolist() 106 | err = server.get_errors(key, temp) 107 | # adding the two errors and storing in parenterror - fitness function 108 | parenterrors[j] = np.copy((err[0]+1.5*err[1])) 109 | parenterrors1[j] = np.copy((err[0])) 110 | parenterrors2[j] = np.copy((err[1])) 111 | 112 | # have to change this to a while loop with appropriate condition later 113 | for iter_num in range(iter): 114 | 115 | if((iter_num)%6==0 and iter_num!=0): 116 | mutate_range-=0.01 117 | prob_mut_cross+=0.01 118 | print("::::::::::::::::changing ", mutate_range) 119 | 120 | 121 | print("\n\n\n\n********"+str(iter_num)+"*********") 122 | 123 | parenerrorsinds = parenterrors.argsort() 124 | parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) 125 | parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) 126 | parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) 127 | population = np.copy(population[parenerrorsinds[::1]]) 128 | 129 | # debug statements 130 | for j in range(pop_size): 131 | print("person " + str(j)+" errorfunc" + str(parenterrors[j])) 132 | print("person " + str(j)+" error1" + str(parenterrors1[j])) 133 | print("person " + str(j)+" error2" + str(parenterrors2[j])) 134 | print("\tvalues"+str(population[j])+"\n\n") 135 | 136 | # Assign probabilities to the population 137 | # parentprobalities = gen_parent_probabilities(pop_size) 138 | 139 | child_population = np.zeros((pop_size, MAX_DEG)) 140 | new_iter = 0 141 | 142 | while(new_iter < pop_size): 143 | 144 | # TODO: WE MAY HAVE TO CHOOSE BETWEEN THESE TWO OPTIONS 145 | # arr = crossover_select(parentprobalities) 146 | 147 | # TODO: Select randomly among top k parents (For now k =10) 148 | arr = crossover_select2(parenterrors, cross_select_from) 149 | 150 | # Sending parents for crossover 151 | temp = crossover(population[arr[0]], population[arr[1]],mutate_range,prob_mut_cross) 152 | if temp[0].tolist() == population[arr[0]].tolist() or temp[1].tolist() == population[arr[0]].tolist() or temp[0].tolist() == population[arr[1]].tolist() or temp[1].tolist() == population[arr[1]].tolist(): 153 | # print("repeated") 154 | # print("first", temp[0]) 155 | # print("Second", temp[1]) 156 | continue 157 | 158 | child_population[new_iter] = np.copy(temp[0]) 159 | new_iter += 1 160 | 161 | child_population[new_iter] = np.copy(temp[1]) 162 | new_iter += 1 163 | 164 | childerrors = np.zeros(pop_size) 165 | childerrors1 = np.zeros(pop_size) 166 | childerrors2 = np.zeros(pop_size) 167 | 168 | # generate errors for each child 169 | for j in range(pop_size): 170 | temp = child_population[j].tolist() 171 | err = server.get_errors(key, temp) 172 | 173 | # adding the two errors and storing in parenterror 174 | if(iter_num >= 15): 175 | childerrors[j] = np.copy((err[0]+err[1])) 176 | else: 177 | childerrors[j] = np.copy((err[0]+1.5*err[1])) 178 | childerrors1[j] = np.copy((err[0])) 179 | childerrors2[j] = np.copy((err[1])) 180 | 181 | # Sort children 182 | childinds = np.copy(childerrors.argsort()) 183 | childerrors = np.copy(childerrors[childinds[::1]]) 184 | childerrors1 = np.copy(childerrors1[childinds[::1]]) 185 | childerrors2 = np.copy(childerrors2[childinds[::1]]) 186 | child_population = np.copy(child_population[childinds[::1]]) 187 | # TODO: Select the best select_sure number of parents and chilren [select these many parents and children for sure] 188 | 189 | # now the children are sorted and stored in child and parents are sorted in population 190 | # we will now create a tempbank array to store top k parents, top k childs and rest being sorted taking from the top 191 | tempbankerr = np.zeros(pop_size) 192 | tempbankerr1 = np.zeros(pop_size) 193 | tempbankerr2 = np.zeros(pop_size) 194 | tempbank= np.zeros((pop_size, MAX_DEG)) 195 | 196 | for j in range(select_sure): 197 | 198 | #choosing the top jth parent and putting in the array 199 | tempbank[j]=np.copy(population[j]) 200 | tempbankerr[j]=np.copy(parenterrors[j]) 201 | tempbankerr1[j]=np.copy(parenterrors1[j]) 202 | tempbankerr2[j]=np.copy(parenterrors2[j]) 203 | 204 | #choosing the top jth child and putting it into the array 205 | tempbank[j+select_sure]=np.copy(child_population[j]) 206 | tempbankerr[j+select_sure]=np.copy(childerrors[j]) 207 | tempbankerr1[j+select_sure]=np.copy(childerrors1[j]) 208 | tempbankerr2[j+select_sure]=np.copy(childerrors2[j]) 209 | 210 | # combining parents and children into one array 211 | # TODO: Concatenating remaining parents and children and selecting from them 212 | candidates = np.copy(np.concatenate([population[select_sure:], child_population[select_sure:]])) 213 | candidate_errors = np.copy(np.concatenate([parenterrors[select_sure:], childerrors[select_sure:]])) 214 | candidate_errors1 = np.copy(np.concatenate([parenterrors1[select_sure:], childerrors1[select_sure:]])) 215 | candidate_errors2 = np.copy(np.concatenate([parenterrors2[select_sure:], childerrors2[select_sure:]])) 216 | 217 | # sorting all the candidates by error 218 | candidate_errors_inds = candidate_errors.argsort() 219 | candidate_errors = np.copy(candidate_errors[candidate_errors_inds[::1]]) 220 | candidate_errors1 = np.copy(candidate_errors1[candidate_errors_inds[::1]]) 221 | candidate_errors2 = np.copy(candidate_errors2[candidate_errors_inds[::1]]) 222 | candidates = np.copy(candidates[candidate_errors_inds[::1]]) 223 | 224 | # TODO: Select the best popsize - 2*(select_sure) 225 | cand_iter = 0 226 | 227 | while(cand_iter + 2*select_sure < pop_size): 228 | tempbank[cand_iter+2*select_sure] = np.copy(candidates[cand_iter]) 229 | tempbankerr[cand_iter+2*select_sure] = np.copy(candidate_errors[cand_iter]) 230 | tempbankerr1[cand_iter+2*select_sure] = np.copy(candidate_errors1[cand_iter]) 231 | tempbankerr2[cand_iter+2*select_sure] = np.copy(candidate_errors2[cand_iter]) 232 | cand_iter += 1 233 | 234 | 235 | #now setting the next population 236 | population=np.copy(tempbank) 237 | parenterrors=np.copy(tempbankerr) 238 | parenterrors1=np.copy(tempbankerr1) 239 | parenterrors2=np.copy(tempbankerr2) 240 | 241 | #we will now sort before updating min_error 242 | parenerrorsinds = parenterrors.argsort() 243 | parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) 244 | parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) 245 | parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) 246 | population = np.copy(population[parenerrorsinds[::1]]) 247 | 248 | 249 | if(min_error == -1 or min_error > parenterrors[0]): 250 | to_send = np.copy(population[0]) 251 | min_error = np.copy(parenterrors[0]) 252 | min_error1 = np.copy(parenterrors1[0]) 253 | min_error2 = np.copy(parenterrors2[0]) 254 | nochange=0 255 | 256 | else: 257 | print("no improvement!!!") 258 | nochange+=1 259 | # if(nochange>10): 260 | # print("Breaking") 261 | # print("-------------------------------------------------------------------------------\n") 262 | # print("Min error = ", min_error, "\n\n") 263 | # print("Min error1 = ", min_error1, "\n\n") 264 | # print("Min error2 = ", min_error2, "\n\n") 265 | # break 266 | print("-------------------------------------------------------------------------------\n") 267 | print("Min error = ", min_error, "\n\n") 268 | print("Min error1 = ", min_error1, "\n\n") 269 | print("Min error2 = ", min_error2, "\n\n") 270 | 271 | # if(min_error<40000000): 272 | # print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 273 | 274 | return to_send 275 | 276 | 277 | to_send = main() 278 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", 279 | server.submit(key, to_send.tolist())) 280 | print("Code finished") 281 | print("-------------------------------------------------------------------------------\n\n") 282 | -------------------------------------------------------------------------------- /A3/intermediate/newcode.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | # import tester as server 3 | import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.2 11 | pop_size=50 12 | cross_n=int(pop_size/2) 13 | iter=20 14 | 15 | 16 | def mutation(vector,index=-1,mut_prob=0.1): 17 | #chooses a random float in -range to +range and makes change at index position in vector 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity=random.randint(0,1) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def mutateall(vector): 30 | for i in range(len(vector)): 31 | vector=mutation(vector,i,0.85) 32 | return vector 33 | 34 | def crossover(vector1, vector2, index=-1): 35 | #performs a crossover from index onwards 36 | 37 | #if no index is passed chooses a random index 38 | send1=[] 39 | send2=[] 40 | 41 | index=random.randint(0,MAX_DEG-1) 42 | 43 | for i in range(MAX_DEG): 44 | parity=random.randint(0,1) 45 | if parity==1: 46 | send1.append(vector1[i]) 47 | send2.append(vector2[i]) 48 | else: 49 | send1.append(vector2[i]) 50 | send2.append(vector1[i]) 51 | return send1, send2 52 | 53 | 54 | 55 | # how to choose individuals for crossover 56 | # how to choose the next gen 57 | 58 | #flow of the program 59 | #main 60 | 61 | #generate initial genration (mutation) size 100 62 | 63 | #while loop 64 | #create a utility array that stores the error for every individual in the population 65 | #parenterrors[] 66 | #get the probability of selection for every parent for going into crossover, this will generate the probaliliites based on the error 67 | #above function will return an array: parentprobality 68 | 69 | #for i in range(number of time you want to do crossover): 70 | # pick two parents using numpy.random.choice 71 | # cross them over, now we have two new 72 | # choose between them. how? -> top two? according to probability? 73 | # for now, choose only the children ignore the parents 74 | # mutate the crossovers 75 | # put them into an array called nextgen[] 76 | 77 | def gen_parent_probabilities(size): 78 | parentprobalities=np.zeros(size) 79 | for j in range(size-1): 80 | parentprobalities[j]=((1-pc)**j)*pc 81 | #assign last probability 82 | parentprobalities[size-1]=((1-pc)**(size-1)) 83 | return parentprobalities 84 | 85 | def crossover_select(parentprobalities): 86 | parents=[] 87 | parents=np.random.choice(np.arange(0,pop_size),2, replace=False,p=parentprobalities) 88 | return parents 89 | 90 | def main(): 91 | w1=0.2 92 | w2=0.8 93 | print("PC: " ,pc, " POP_SIZE: ",pop_size," ITER : ", iter, "w1: ",w1,"w2: ",w2, "Stop : ", "50") 94 | 95 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 96 | # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] 97 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 98 | min_error=-1 99 | min_error1=-1 100 | min_error2=-1 101 | 102 | parenterrors=np.zeros(pop_size) 103 | parenterrors1=np.zeros(pop_size) 104 | parenterrors2=np.zeros(pop_size) 105 | parentprobalities=np.zeros(pop_size) 106 | population=np.zeros((pop_size,MAX_DEG)) 107 | 108 | # generate the population 109 | for i in range(pop_size): 110 | temp=np.copy(vector_og) 111 | population[i]=mutateall(temp) 112 | 113 | #generate errors for each individual in the population 114 | for j in range(pop_size): 115 | temp=population[j].tolist() #passing a list to the get_errors function 116 | err=server.get_errors(key,temp) 117 | 118 | #adding the two errors and storing in parenterror 119 | parenterrors[j]=(w1*err[0]+w2*err[1]) 120 | parenterrors1[j]=(err[0]) 121 | parenterrors2[j]=(err[1]) 122 | 123 | # have to change this to a while loop with appropriate condition later 124 | for iter_num in range(iter): 125 | new_iter=0 126 | if(iter_num > 50 ): 127 | w1=1 128 | w2=1 129 | 130 | print("\n\n\n\n********"+str(iter_num)+"*********") 131 | 132 | 133 | # Sort the errors in ascending order 134 | # Least error => max fittness 135 | # Correspondingly sort the population also 136 | # arrinds=sortfunc(parenterrors,parenterrors1,parenterrors2) 137 | parenerrorsinds=parenterrors.argsort() 138 | parenterrors=parenterrors[parenerrorsinds[::1]] 139 | parenterrors1=parenterrors1[parenerrorsinds[::1]] 140 | parenterrors2=parenterrors2[parenerrorsinds[::1]] 141 | population=population[parenerrorsinds[::1]] 142 | 143 | #debug statements 144 | for j in range(pop_size): 145 | print("person " + str(j)+" error "+ str(parenterrors[j])) 146 | print("\tvalues"+str(population[j])+"\n\n") 147 | 148 | # Assign probabilities to the population 149 | parentprobalities=gen_parent_probabilities(pop_size) 150 | 151 | # Checking sum(prob) = 1 152 | # print(np.sum(parentprobalities)) 153 | child_population=np.zeros((pop_size,MAX_DEG)) 154 | 155 | #perform crossover cross_n times 156 | for j in range (cross_n): 157 | arr=crossover_select(parentprobalities) 158 | 159 | # Two parents chosen based on probabilities => arr[0], arr[1] 160 | # Sending parents for crossover 161 | temp=crossover(population[arr[0]],population[arr[1]]) 162 | 163 | child_population[new_iter]=temp[0] 164 | new_iter+=1 165 | 166 | child_population[new_iter]=temp[1] 167 | new_iter+=1 168 | 169 | # Send the new population for mutation 170 | for j in range(pop_size): 171 | temp=np.copy(child_population[j]) 172 | child_population[j]=mutation(temp) 173 | 174 | # get the errors for the new population 175 | childerrors=np.zeros(pop_size) 176 | childerrors1=np.zeros(pop_size) 177 | childerrors2=np.zeros(pop_size) 178 | 179 | # generate errors for each child 180 | for j in range(pop_size): 181 | temp=child_population[j].tolist() #passing a list to the get_errors function 182 | err=server.get_errors(key,temp) 183 | 184 | #adding the two errors and storing in parenterror 185 | childerrors[j]=(w1*err[0]+w2*err[1]) 186 | childerrors1[j]=(err[0]) 187 | childerrors2[j]=(err[1]) 188 | 189 | #combining parents and children into one array 190 | candidates=np.concatenate([population,child_population]) 191 | candidate_errors=np.concatenate([parenterrors,childerrors]) 192 | candidate_errors1=np.concatenate([parenterrors1,childerrors1]) 193 | candidate_errors2=np.concatenate([parenterrors2,childerrors2]) 194 | 195 | # sorting all the candidates by error 196 | # arrinds=sortfunc(candidate_errors,candidate_errors1, candidate_errors2) 197 | candidate_errors_inds=candidate_errors.argsort() 198 | candidate_errors=candidate_errors[candidate_errors_inds[::1]] 199 | candidate_errors1=candidate_errors1[candidate_errors_inds[::1]] 200 | candidate_errors2=candidate_errors2[candidate_errors_inds[::1]] 201 | candidates=candidates[candidate_errors_inds[::1]] 202 | 203 | # set population for the next iteration by selecting the best k in the candidaes 204 | for i in range(pop_size): 205 | population[i]=candidates[i] 206 | parenterrors[i]=candidate_errors[i] 207 | parenterrors1[i]=candidate_errors1[i] 208 | parenterrors2[i]=candidate_errors2[i] 209 | 210 | # set the send array by choosing the minimum from all the candidates NOTE: it may not be selected in the new population 211 | if(min_error==-1 or min_error>candidate_errors[0]): 212 | to_send=candidates[0] 213 | min_error=candidate_errors[0] 214 | min_error1=candidate_errors1[0] 215 | min_error2=candidate_errors2[0] 216 | print("-------------------------------------------------------------------------------\n") 217 | print("Min error = ", min_error,"\n\n") 218 | print("Min error1 = ", min_error1,"\n\n") 219 | print("Min error2 = ", min_error2,"\n\n") 220 | 221 | if(min_error<40000000): 222 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 223 | 224 | return to_send 225 | 226 | to_send=main() 227 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 228 | print("Code finished") 229 | print("-------------------------------------------------------------------------------\n\n") -------------------------------------------------------------------------------- /A3/intermediate/newcode_26.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tester as server 3 | # import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.2 11 | pop_size=30 12 | cross_n=int(pop_size/2) 13 | iter=30 14 | 15 | 16 | def mutation(vector,index=-1,mut_prob=0.1): 17 | #chooses a random float in -range to +range and makes change at index position in vector 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity= np.random.choice((0,1),p=[1-mut_prob,mut_prob]) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def mutateall(temp): 30 | vector=np.copy(temp) 31 | for i in range(len(vector)): 32 | # vector=mutation(vector,i,0.85) 33 | #TODO: MUTATE WITH RANGES 34 | # lo=max(-ranger,vector[i]-2) 35 | # hi=min(ranger,vector[i]+2) 36 | lo=max(-ranger,vector[i]+vector[i]*random.uniform(0,1)) 37 | hi=min(ranger,vector[i]-vector[i]*random.uniform(0,1)) 38 | vector[i]=np.random.choice([random.uniform(lo,hi),vector[i]],p=[0.95,0.05]) 39 | return vector 40 | 41 | def mutatesome(temp): 42 | #5 random integers 43 | vector=np.copy(temp) 44 | a=np.random.choice(np.arange(0,11),11,replace=False) 45 | 46 | for i in a.tolist(): 47 | #TODO: MUTATE WITH RANGES 48 | # lo=max(-ranger,vector[i]-2) 49 | # hi=min(ranger,vector[i]+2) 50 | lo=max(-ranger,vector[i]+vector[i]*random.uniform(0,1)) 51 | hi=min(ranger,vector[i]-vector[i]*random.uniform(0,1)) 52 | vector[i]=random.uniform(lo,hi) 53 | while vector[i]==temp[i]: 54 | vector[i]=random.uniform(lo,hi) 55 | return vector 56 | 57 | def crossover(vector1, vector2, index=-1): 58 | #performs a crossover from index onwards 59 | 60 | #if no index is passed chooses a random index 61 | send1=[] 62 | send2=[] 63 | 64 | index=random.randint(0,MAX_DEG-1) 65 | 66 | for i in range(MAX_DEG): 67 | parity=random.randint(0,1) 68 | if parity==1: 69 | send1.append(vector1[i]) 70 | send2.append(vector2[i]) 71 | else: 72 | send1.append(vector2[i]) 73 | send2.append(vector1[i]) 74 | 75 | return mutation(send1), mutation(send2) 76 | 77 | 78 | def gen_parent_probabilities(size): 79 | parentprobalities=np.zeros(size) 80 | for j in range(size-1): 81 | parentprobalities[j]=((1-pc)**j)*pc 82 | #assign last probability 83 | parentprobalities[size-1]=((1-pc)**(size-1)) 84 | return parentprobalities 85 | 86 | def crossover_select(parentprobalities): 87 | parents=[] 88 | parents=np.random.choice(np.arange(0,pop_size),2, replace=False,p=parentprobalities) 89 | return parents 90 | 91 | def check_match(vector1,vector2): 92 | #return 1 if match 93 | count=0 94 | for i in range(MAX_DEG): 95 | if (abs(vector1[i]-vector2[i])>0.5): 96 | count+=1 97 | if count>=5: 98 | return 0 99 | else: 100 | return 1 101 | 102 | def main(): 103 | print("PC: " ,pc, " POP_SIZE: ",pop_size," ITER : ", iter, "Stop : ", "50") 104 | 105 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 106 | # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] 107 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 108 | min_error=-1 109 | min_error1=-1 110 | min_error2=-1 111 | 112 | parenterrors=np.zeros(pop_size) 113 | parenterrors1=np.zeros(pop_size) 114 | parenterrors2=np.zeros(pop_size) 115 | parentprobalities=np.zeros(pop_size) 116 | population=np.zeros((pop_size,MAX_DEG)) 117 | 118 | # generate the population 119 | for i in range(pop_size): 120 | temp=np.copy(vector_og) 121 | population[i]=mutateall(temp) 122 | 123 | #generate errors for each individual in the population 124 | for j in range(pop_size): 125 | temp=population[j].tolist() #passing a list to the get_errors function 126 | err=server.get_errors(key,temp) 127 | 128 | #adding the two errors and storing in parenterror 129 | parenterrors[j]=(err[0]+err[1]) 130 | parenterrors1[j]=(err[0]) 131 | parenterrors2[j]=(err[1]) 132 | 133 | # have to change this to a while loop with appropriate condition later 134 | for iter_num in range(iter): 135 | 136 | 137 | print("\n\n\n\n********"+str(iter_num)+"*********") 138 | 139 | 140 | # Sort the errors in ascending order 141 | # Least error => max fittness 142 | # Correspondingly sort the population also 143 | # arrinds=sortfunc(parenterrors,parenterrors1,parenterrors2) 144 | parenerrorsinds=parenterrors.argsort() 145 | parenterrors=parenterrors[parenerrorsinds[::1]] 146 | parenterrors1=parenterrors1[parenerrorsinds[::1]] 147 | parenterrors2=parenterrors2[parenerrorsinds[::1]] 148 | population=population[parenerrorsinds[::1]] 149 | 150 | # debug statements 151 | for j in range(pop_size): 152 | print("person " + str(j)+" error "+ str(parenterrors[j])) 153 | print("person " + str(j)+" error "+ str(parenterrors1[j])) 154 | print("person " + str(j)+" error "+ str(parenterrors2[j])) 155 | print("\tvalues"+str(population[j])+"\n\n") 156 | 157 | # Assign probabilities to the population 158 | parentprobalities=gen_parent_probabilities(pop_size) 159 | 160 | # Checking sum(prob) = 1 161 | # print(np.sum(parentprobalities)) 162 | child_population=np.zeros((pop_size,MAX_DEG)) 163 | 164 | #perform crossover cross_n times 165 | new_iter=0 166 | shalliquit=0 167 | 168 | while(new_iter=100): 175 | print("your population has converged") 176 | new_iter=0 177 | for t in range(pop_size): 178 | lol=mutatesome(population[0]) 179 | print("mutated a to b",population[0],lol) 180 | child_population[t]=lol 181 | break 182 | 183 | if check_match(population[arr[0]].tolist(),population[arr[1]].tolist())==1: 184 | shalliquit+=1 185 | continue 186 | else: 187 | shalliquit=0 188 | 189 | # Two parents chosen based on probabilities => arr[0], arr[1] 190 | # Sending parents for crossover 191 | temp=crossover(population[arr[0]],population[arr[1]]) 192 | if temp[0]==population[arr[0]].tolist() or temp[1]==population[arr[0]].tolist() or temp[0]==population[arr[1]].tolist() or temp[1]==population[arr[1]].tolist(): 193 | print("repeated") 194 | print("first",temp[0]) 195 | print("Second",temp[1]) 196 | continue 197 | 198 | child_population[new_iter]=temp[0] 199 | new_iter+=1 200 | 201 | child_population[new_iter]=temp[1] 202 | new_iter+=1 203 | 204 | # Send the new population for mutation 205 | # for j in range(pop_size): 206 | # temp=np.copy(child_population[j]) 207 | # child_population[j]=mutation(temp) 208 | 209 | # get the errors for the new population 210 | childerrors=np.zeros(pop_size) 211 | childerrors1=np.zeros(pop_size) 212 | childerrors2=np.zeros(pop_size) 213 | 214 | # generate errors for each child 215 | for j in range(pop_size): 216 | temp=child_population[j].tolist() #passing a list to the get_errors function 217 | err=server.get_errors(key,temp) 218 | 219 | #adding the two errors and storing in parenterror 220 | childerrors[j]=(err[0]+err[1]) 221 | childerrors1[j]=(err[0]) 222 | childerrors2[j]=(err[1]) 223 | 224 | #combining parents and children into one array 225 | candidates=np.concatenate([population,child_population]) 226 | candidate_errors=np.concatenate([parenterrors,childerrors]) 227 | candidate_errors1=np.concatenate([parenterrors1,childerrors1]) 228 | candidate_errors2=np.concatenate([parenterrors2,childerrors2]) 229 | 230 | # sorting all the candidates by error 231 | # arrinds=sortfunc(candidate_errors,candidate_errors1, candidate_errors2) 232 | candidate_errors_inds=candidate_errors.argsort() 233 | candidate_errors=candidate_errors[candidate_errors_inds[::1]] 234 | candidate_errors1=candidate_errors1[candidate_errors_inds[::1]] 235 | candidate_errors2=candidate_errors2[candidate_errors_inds[::1]] 236 | candidates=candidates[candidate_errors_inds[::1]] 237 | 238 | # set population for the next iteration by selecting the best k in the candidaes 239 | for i in range(pop_size): 240 | population[i]=candidates[i] 241 | parenterrors[i]=candidate_errors[i] 242 | parenterrors1[i]=candidate_errors1[i] 243 | parenterrors2[i]=candidate_errors2[i] 244 | 245 | # set the send array by choosing the minimum from all the candidates NOTE: it may not be selected in the new population 246 | if(min_error==-1 or min_error>candidate_errors[0]): 247 | to_send=candidates[0] 248 | min_error=candidate_errors[0] 249 | min_error1=candidate_errors1[0] 250 | min_error2=candidate_errors2[0] 251 | 252 | else: 253 | print("no improvement!!!") 254 | print("-------------------------------------------------------------------------------\n") 255 | print("Min error = ", min_error,"\n\n") 256 | print("Min error1 = ", min_error1,"\n\n") 257 | print("Min error2 = ", min_error2,"\n\n") 258 | 259 | # if(min_error<40000000): 260 | # print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 261 | 262 | return to_send 263 | 264 | to_send=main() 265 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 266 | print("Code finished") 267 | print("-------------------------------------------------------------------------------\n\n") -------------------------------------------------------------------------------- /A3/intermediate/oldcode.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tester as server 3 | # import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.2 11 | pop_size=100 12 | cross_n=int(pop_size/2) 13 | iter=29 14 | 15 | def mutation(vector,index=-1): 16 | #chooses a random float in -range to +range and makes change at index position in vector 17 | 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity=random.randint(0,1) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def crossover(vector1, vector2, index=-1): 30 | #performs a crossover from index onwards 31 | 32 | #if no index is passed chooses a random index 33 | send1=[] 34 | send2=[] 35 | 36 | if index==-1: 37 | index=random.randint(0,MAX_DEG-1) 38 | 39 | for i in range(MAX_DEG): 40 | parity=random.randint(0,1) 41 | if parity==1: 42 | send1.append(vector1[i]) 43 | send2.append(vector2[i]) 44 | else: 45 | send1.append(vector2[i]) 46 | send2.append(vector1[i]) 47 | return send1, send2 48 | 49 | else: 50 | for i in range(MAX_DEG): 51 | if i top two? according to probability? 78 | # for now, choose only the children ignore the parents 79 | # mutate the crossovers 80 | # put them into an array called nextgen[] 81 | 82 | def gen_parent_probabilities(): 83 | parentprobalities=np.zeros(pop_size) 84 | for j in range(pop_size-1): 85 | parentprobalities[j]=((1-pc)**j)*pc 86 | #assign last probability 87 | parentprobalities[pop_size-1]=((1-pc)**(pop_size-1)) 88 | return parentprobalities 89 | 90 | def crossover_select(parentprobalities): 91 | parents=[] 92 | # i1=np.random.choice(np.arange(0,pop_size),p=parentprobalities) 93 | # i2=np.random.choice(np.arange(0,pop_size),2 replace=False,p=parentprobalities) 94 | # parents.append(i1) 95 | # parents.append(i2) 96 | parents=np.random.choice(np.arange(0,pop_size),2, replace=False,p=parentprobalities) 97 | return parents 98 | 99 | def main(): 100 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 101 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 102 | min_error=-1 103 | min_error1=-1 104 | min_error2=-1 105 | 106 | parenterrors=np.zeros(pop_size) 107 | parenterrors1=np.zeros(pop_size) 108 | parenterrors2=np.zeros(pop_size) 109 | parentprobalities=np.zeros(pop_size) 110 | population=np.zeros((pop_size,MAX_DEG)) 111 | 112 | # generate the population 113 | for i in range(pop_size): 114 | temp=np.copy(vector_og) 115 | population[i]=mutation(temp) 116 | 117 | # have to change this to a while loop with appropriate condition later 118 | for i in range(iter): 119 | new_iter=0 120 | new_population=np.zeros((pop_size,MAX_DEG)) 121 | 122 | #generate errors for each individual in the population 123 | print("\n\n\n\n********"+str(i)+"*********") 124 | for j in range(pop_size): 125 | temp=population[j].tolist() #passing a list to the get_errors function 126 | err=server.get_errors(key,temp) 127 | 128 | #adding the two errors and storing in parenterror 129 | parenterrors[j]=(err[0]+err[1]) 130 | parenterrors1[j]=(err[0]) 131 | parenterrors2[j]=(err[1]) 132 | 133 | # Sort the errors in ascending order 134 | # Least error => max fittness 135 | # Correspondingly sort the population also 136 | parenterrorsinds=parenterrors.argsort() 137 | parenterrors=parenterrors[parenterrorsinds[::1]] 138 | parenterrors1=parenterrors1[parenterrorsinds[::1]] 139 | parenterrors2=parenterrors2[parenterrorsinds[::1]] 140 | population=population[parenterrorsinds[::1]] 141 | 142 | 143 | #set the send array 144 | if(min_error==-1 or min_error>parenterrors[0]): 145 | to_send=population[0] 146 | min_error=parenterrors[0] 147 | min_error1=parenterrors1[0] 148 | min_error2=parenterrors2[0] 149 | 150 | #debug statements 151 | for j in range(pop_size): 152 | print("person " + str(j)+" error "+ str(parenterrors[j])) 153 | print("\tvalues"+str(population[j])+"\n\n") 154 | 155 | # Assign probabilities to the population 156 | parentprobalities=gen_parent_probabilities() 157 | 158 | # Checking sum(prob) = 1 159 | # print(np.sum(parentprobalities)) 160 | 161 | #perform crossover cross_n times 162 | for j in range (cross_n): 163 | arr=crossover_select(parentprobalities) 164 | 165 | # Two parents chosen based on probabilities => arr[0], arr[1] 166 | # Sending parents for crossover 167 | temp=crossover(population[arr[0]],population[arr[1]]) 168 | 169 | # new_iter is the iterator for the new_population numpy 170 | new_population[new_iter]=temp[0] 171 | new_iter+=1 172 | new_population[new_iter]=temp[1] 173 | new_iter+=1 174 | 175 | 176 | # Send the new population for mutation 177 | for j in range(pop_size): 178 | temp=np.copy(new_population[j]) 179 | new_population[j]=mutation(temp) 180 | 181 | # print(new_population) 182 | population=np.copy(new_population) 183 | 184 | print("-------------------------------------------------------------------------------\n") 185 | print("Min error = ", min_error,"\n\n") 186 | print("Min error1 = ", min_error1,"\n\n") 187 | print("Min error2 = ", min_error2,"\n\n") 188 | return to_send 189 | 190 | to_send=main() 191 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 192 | print("Code finished") 193 | print("-------------------------------------------------------------------------------\n\n") -------------------------------------------------------------------------------- /A3/intermediate/oldnew.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | # import tester as server 3 | import client_moodle as server 4 | import random 5 | 6 | team_name="team_62" #for our reference 7 | MAX_DEG=11 #number of features 8 | key='847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 9 | ranger=10 10 | pc=0.2 11 | pop_size=100 12 | cross_n=int(pop_size/2) 13 | iter=40 14 | 15 | def mutation(vector,index=-1,mut_prob=0.1): 16 | #chooses a random float in -range to +range and makes change at index position in vector 17 | 18 | #if no index is passed chooses a random index 19 | if index==-1: 20 | index=random.randint(0,MAX_DEG-1) 21 | 22 | #here probability is 0.5 23 | parity=random.randint(0,1) 24 | # print("Parity",parity) 25 | if parity==1: 26 | vector[index]=random.uniform(-ranger,ranger) 27 | return vector 28 | 29 | def mutateall(vector): 30 | 31 | for i in range(len(vector)): 32 | vector=mutation(vector,i,0.85) 33 | return vector 34 | 35 | def crossover(vector1, vector2, index=-1): 36 | #performs a crossover from index onwards 37 | 38 | #if no index is passed chooses a random index 39 | send1=[] 40 | send2=[] 41 | 42 | if index==-1: 43 | index=random.randint(0,MAX_DEG-1) 44 | 45 | for i in range(MAX_DEG): 46 | parity=random.randint(0,1) 47 | if parity==1: 48 | send1.append(vector1[i]) 49 | send2.append(vector2[i]) 50 | else: 51 | send1.append(vector2[i]) 52 | send2.append(vector1[i]) 53 | return send1, send2 54 | 55 | else: 56 | for i in range(MAX_DEG): 57 | if i top two? according to probability? 84 | # for now, choose only the children ignore the parents 85 | # mutate the crossovers 86 | # put them into an array called nextgen[] 87 | 88 | def gen_parent_probabilities(size): 89 | parentprobalities=np.zeros(size) 90 | for j in range(size-1): 91 | parentprobalities[j]=((1-pc)**j)*pc 92 | #assign last probability 93 | parentprobalities[size-1]=((1-pc)**(size-1)) 94 | return parentprobalities 95 | 96 | def crossover_select(parentprobalities): 97 | parents=[] 98 | # i1=np.random.choice(np.arange(0,pop_size),p=parentprobalities) 99 | # i2=np.random.choice(np.arange(0,pop_size),2 replace=False,p=parentprobalities) 100 | # parents.append(i1) 101 | # parents.append(i2) 102 | parents=np.random.choice(np.arange(0,pop_size),2, replace=False,p=parentprobalities) 103 | return parents 104 | 105 | def main(): 106 | print("PC: " ,pc, " POP_SIZE: ",pop_size," ITER : ", iter) 107 | 108 | vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 109 | # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] 110 | to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] 111 | min_error=-1 112 | min_error1=-1 113 | min_error2=-1 114 | 115 | parenterrors=np.zeros(pop_size) 116 | parenterrors1=np.zeros(pop_size) 117 | parenterrors2=np.zeros(pop_size) 118 | parentprobalities=np.zeros(pop_size) 119 | population=np.zeros((pop_size,MAX_DEG)) 120 | 121 | # generate the population 122 | for i in range(pop_size): 123 | temp=np.copy(vector_og) 124 | population[i]=mutateall(temp) 125 | 126 | #generate errors for each individual in the population 127 | for j in range(pop_size): 128 | temp=population[j].tolist() #passing a list to the get_errors function 129 | err=server.get_errors(key,temp) 130 | 131 | #adding the two errors and storing in parenterror 132 | parenterrors[j]=(err[0]+err[1]) 133 | parenterrors1[j]=(err[0]) 134 | parenterrors2[j]=(err[1]) 135 | 136 | # have to change this to a while loop with appropriate condition later 137 | for iter_num in range(iter): 138 | new_iter=0 139 | 140 | print("\n\n\n\n********"+str(iter_num)+"*********") 141 | 142 | 143 | # Sort the errors in ascending order 144 | # Least error => max fittness 145 | # Correspondingly sort the population also 146 | parenterrorsinds=parenterrors.argsort() 147 | parenterrors=parenterrors[parenterrorsinds[::1]] 148 | parenterrors1=parenterrors1[parenterrorsinds[::1]] 149 | parenterrors2=parenterrors2[parenterrorsinds[::1]] 150 | population=population[parenterrorsinds[::1]] 151 | 152 | 153 | #debug statements 154 | for j in range(pop_size): 155 | print("person " + str(j)+" error "+ str(parenterrors[j])) 156 | print("\tvalues"+str(population[j])+"\n\n") 157 | 158 | # Assign probabilities to the population 159 | parentprobalities=gen_parent_probabilities(pop_size) 160 | 161 | # Checking sum(prob) = 1 162 | # print(np.sum(parentprobalities)) 163 | child_population=np.zeros((pop_size,MAX_DEG)) 164 | 165 | #perform crossover cross_n times 166 | for j in range (cross_n): 167 | arr=crossover_select(parentprobalities) 168 | 169 | # Two parents chosen based on probabilities => arr[0], arr[1] 170 | # Sending parents for crossover 171 | temp=crossover(population[arr[0]],population[arr[1]]) 172 | 173 | child_population[new_iter]=temp[0] 174 | new_iter+=1 175 | 176 | child_population[new_iter]=temp[1] 177 | new_iter+=1 178 | 179 | # print("BEFORE MUTATION: CHILD POPULATION") 180 | # for j in range(len(child_population)): 181 | # print("Child", j) 182 | # print(child_population[i]) 183 | 184 | # Send the new population for mutation 185 | for j in range(pop_size): 186 | temp=np.copy(child_population[j]) 187 | child_population[j]=mutation(temp) 188 | 189 | # print("AFTER MUTATION: CHILD POPULATION") 190 | # for j in range(len(child_population)): 191 | # print("Child", j) 192 | # print(child_population[i]) 193 | 194 | # get the errors for the new population 195 | childerrors=np.zeros(pop_size) 196 | childerrors1=np.zeros(pop_size) 197 | childerrors2=np.zeros(pop_size) 198 | 199 | # generate errors for each child 200 | for j in range(pop_size): 201 | temp=child_population[j].tolist() #passing a list to the get_errors function 202 | err=server.get_errors(key,temp) 203 | 204 | #adding the two errors and storing in parenterror 205 | childerrors[j]=(err[0]+err[1]) 206 | childerrors1[j]=(err[0]) 207 | childerrors2[j]=(err[1]) 208 | 209 | #combining parents and children into one array 210 | candidates=np.concatenate([population,child_population]) 211 | candidate_errors=np.concatenate([parenterrors,childerrors]) 212 | candidate_errors1=np.concatenate([parenterrors1,childerrors1]) 213 | candidate_errors2=np.concatenate([parenterrors2,childerrors2]) 214 | 215 | # sorting all the candidates by error 216 | candidateerrorsinds=candidate_errors.argsort() 217 | candidate_errors=candidate_errors[candidateerrorsinds[::1]] 218 | candidate_errors1=candidate_errors1[candidateerrorsinds[::1]] 219 | candidate_errors2=candidate_errors2[candidateerrorsinds[::1]] 220 | candidates=candidates[candidateerrorsinds[::1]] 221 | 222 | # setting the probability and choosing the indices 223 | # candidate_prob=gen_parent_probabilities(pop_size*2) 224 | # chosenindices=np.random.choice(np.arange(0,2*pop_size),pop_size, replace=False,p=candidate_prob) 225 | 226 | # set population for the next iteration 227 | 228 | for i in range(pop_size): 229 | population[i]=candidates[i] 230 | parenterrors[i]=candidate_errors[i] 231 | parenterrors1[i]=candidate_errors1[i] 232 | parenterrors2[i]=candidate_errors2[i] 233 | 234 | # set the send array by choosing the minimum from all the candidates NOTE: it may not be selected in the new population 235 | if(min_error==-1 or min_error>candidate_errors[0]): 236 | to_send=candidates[0] 237 | min_error=candidate_errors[0] 238 | min_error1=candidate_errors1[0] 239 | min_error2=candidate_errors2[0] 240 | print("-------------------------------------------------------------------------------\n") 241 | print("Min error = ", min_error,"\n\n") 242 | print("Min error1 = ", min_error1,"\n\n") 243 | print("Min error2 = ", min_error2,"\n\n") 244 | 245 | if(min_error<40000000): 246 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 247 | 248 | return to_send 249 | 250 | to_send=main() 251 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 252 | print("Code finished") 253 | print("-------------------------------------------------------------------------------\n\n") -------------------------------------------------------------------------------- /A3/intermediate/oldstuff/client.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | import numpy as np 4 | 5 | ######### DO NOT CHANGE ANYTHING IN THIS FILE ################## 6 | API_ENDPOINT = 'http://10.4.21.147' 7 | PORT = 3000 8 | MAX_DEG = 11 9 | 10 | #### functions that you can call 11 | def get_errors(id, vector): 12 | """ 13 | returns python array of length 2 14 | (train error and validation error) 15 | """ 16 | for i in vector: assert -1<=abs(i)<=1 17 | assert len(vector) == MAX_DEG 18 | 19 | return json.loads(send_request(id, vector, 'geterrors')) 20 | 21 | def submit(id, vector): 22 | """ 23 | used to make official submission of your weight vector 24 | returns string "successfully submitted" if properly submitted. 25 | """ 26 | for i in vector: assert -1<=abs(i)<=1 27 | assert len(vector) == MAX_DEG 28 | return send_request(id, vector, 'submit') 29 | 30 | #### utility functions 31 | def urljoin(root, port, path=''): 32 | root = root + ':' + str(port) 33 | if path: root = '/'.join([root.rstrip('/'), path.rstrip('/')]) 34 | return root 35 | 36 | def send_request(id, vector, path): 37 | api = urljoin(API_ENDPOINT, PORT, path) 38 | vector = json.dumps(vector) 39 | response = requests.post(api, data={'id':id, 'vector':vector}).text 40 | if "reported" in response: 41 | print(response) 42 | exit() 43 | 44 | return response 45 | 46 | 47 | if __name__ == "__main__": 48 | """ 49 | Replace "test" with your secret ID and just run this file 50 | to verify that the server is working for your ID. 51 | """ 52 | 53 | err = get_errors('847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo', list(-np.arange(0,1.1,0.1))) 54 | assert len(err) == 2 55 | 56 | submit_status = submit('847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo', list(-np.arange(0,1.1,0.1))) 57 | assert "submitted" in submit_status 58 | -------------------------------------------------------------------------------- /A3/intermediate/oldstuff/overfit_weights.txt: -------------------------------------------------------------------------------- 1 | [-0.00016927573251173823, 0.0010953590656607808, 0.003731869524518327, 0.08922889556431182, 0.03587507175384199, -0.0015634754169704097, -7.439827367266828e-05, 3.7168210026033343e-06, 1.555252501348866e-08, -2.2215895929103804e-09, 2.306783174308054e-11] 2 | -------------------------------------------------------------------------------- /A3/intermediate/overfit.txt: -------------------------------------------------------------------------------- 1 | [0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] -------------------------------------------------------------------------------- /A3/intermediate/submit.py: -------------------------------------------------------------------------------- 1 | # import tester as server 2 | import client_moodle as server 3 | key = '847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 4 | 5 | # creating an empty list 6 | lst =input().split() 7 | print(lst) 8 | for i in range(0,11): 9 | lst[i]=float(lst[i]) 10 | print("sending\n\n"+str(lst)+"\n\nwas it successfully submitted?", server.submit(key,lst)) 11 | print("Code finished") 12 | print("-------------------------------------------------------------------------------\n\n") 13 | -------------------------------------------------------------------------------- /A3/intermediate/submit2.py: -------------------------------------------------------------------------------- 1 | # import tester as server 2 | import client_moodle as server 3 | key = '847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 4 | 5 | lst=[] 6 | # creating an empty list 7 | lst1 =input().split() 8 | lst2 =input().split() 9 | lst3 =input().split() 10 | for i in range(0,4): 11 | lst.append(float(lst1[i])) 12 | 13 | for i in range(0,4): 14 | lst.append(float(lst2[i])) 15 | 16 | for i in range(0,3): 17 | lst.append(float(lst3[i])) 18 | 19 | 20 | print(lst) 21 | print("sending\n\n"+str(lst)+"\n\nwas it successfully submitted?", server.submit(key,lst)) 22 | print("Code finished") 23 | print("-------------------------------------------------------------------------------\n\n") 24 | -------------------------------------------------------------------------------- /A3/intermediate/tablesprint.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tester as server 3 | # import client_moodle as server 4 | from tabulate import tabulate 5 | import random 6 | 7 | team_name = "team_62" # for our reference 8 | MAX_DEG = 11 # number of features 9 | key = '847FWwSwTAxKTPvfixjzNnxbeudiTzJ9psoVIdUxqehtQ5efNo' 10 | ranger = 10 11 | pc = 0.2 12 | pop_size = 6 13 | select_sure = 3 14 | cross_select_from = 6 15 | crossover_no = 5 16 | iter = 18 17 | 18 | 19 | def formatArray(x): 20 | y=[] 21 | for i in range (pop_size): 22 | y.append([x[i]]) 23 | y=np.array(y) 24 | return y 25 | 26 | def showtable(arrparents,arrparrerrs,arrchoices,arrchildren,arrchildrenmutated,arrchilderrors): 27 | 28 | #format all arrays for printing 29 | arrparents = formatArray(arrparents) 30 | arrchildren = formatArray(arrchildren) 31 | arrchildrenmutated = formatArray(arrchildrenmutated) 32 | 33 | tempchoice = [] 34 | for i in range(pop_size): 35 | ind = int(i/2) 36 | tempchoice.append([[arrchoices[ind][0],arrchoices[ind][1]]]) 37 | tempchoice=np.array(tempchoice) 38 | 39 | 40 | final1 = np.zeros((pop_size,2),dtype=object) 41 | final2 = np.zeros((pop_size,4),dtype=object) 42 | 43 | for i in range(pop_size): 44 | final1[i][0]=arrparents[i] 45 | final1[i][1]=arrparrerrs[i] 46 | final2[i][0]=tempchoice[i] 47 | final2[i][1]=arrchildren[i] 48 | final2[i][2]=arrchilderrors[i] 49 | final2[i][3]=arrchildrenmutated[i] 50 | 51 | 52 | headers1 = ["Population", "Population Errors"] 53 | headers2 = ["Parents", "Children", "Children Errors", "Mutated Children"] 54 | table1 = tabulate(final1, headers1, tablefmt="fancy_grid") 55 | table2 = tabulate(final2, headers2, tablefmt="fancy_grid") 56 | print(table1) 57 | print(table2) 58 | # print(arrparents,"\n\n*******\n\n",arrparrerrs,"\n\n*******\n\n",arrchoices,"\n\n*******\n\n",tempchoice,"\n\n*******\n\n",arrchildren,"\n\n*******\n\n",arrchildrenmutated,"\n\n*******\n\n",arrchilderrors) 59 | 60 | 61 | def mutateall(temp,prob, mutate_range): 62 | vector = np.copy(temp) 63 | for i in range(len(vector)): 64 | fact=random.uniform(-mutate_range, mutate_range) 65 | vector[i] = np.random.choice([vector[i]*(fact+1), vector[i]], p=[prob,1-prob]) 66 | if(vector[i]<-10) : 67 | vector[i]=-10 68 | elif(vector[i]>10) : 69 | vector[i]=10 70 | 71 | return vector 72 | 73 | 74 | def crossover(vector1, vector2, mutate_range,prob_mut_cross, index=-1): 75 | send1 = vector1.tolist() 76 | send2 = vector2.tolist() 77 | 78 | # a = np.random.choice(np.arange(0, 11), crossover_no, replace=False) 79 | a = np.random.choice(np.arange(0, 11),5, replace=False) 80 | 81 | for i in a.tolist(): 82 | send1[i] = np.copy(vector2[i]) 83 | send2[i] = np.copy(vector1[i]) 84 | 85 | return mutateall(send1,prob_mut_cross,mutate_range), mutateall(send2,prob_mut_cross,mutate_range),send1,send2 86 | 87 | 88 | def crossover_select2(parenterrors, num): 89 | return random.sample(range(num), 2) 90 | 91 | 92 | def check_match(vector1, vector2): 93 | # return 1 if match 94 | count = 0 95 | for i in range(MAX_DEG): 96 | if (abs(vector1[i]-vector2[i]) > 0.5): 97 | count += 1 98 | if count >= 5: 99 | return 0 100 | else: 101 | return 1 102 | 103 | 104 | def main(): 105 | 106 | mutate_range=0.1 107 | prob_mut_cross = 0.5 108 | print("pop_size:", pop_size, "iter:", iter, "cross_select_from",cross_select_from) 109 | print("select_sure",select_sure,"prob_mut_cross",prob_mut_cross,"mutate_range",mutate_range) 110 | 111 | vector_og = [-1e-19, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, - 112 | 6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] 113 | to_send = [-20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20] 114 | min_error = -1 115 | min_error1 = -1 116 | min_error2 = -1 117 | 118 | parenterrors = np.zeros(pop_size) 119 | parenterrors1 = np.zeros(pop_size) 120 | parenterrors2 = np.zeros(pop_size) 121 | # parentprobalities = np.zeros(pop_size) 122 | population = np.zeros((pop_size, MAX_DEG)) 123 | 124 | # generate the population 125 | for i in range(pop_size): 126 | temp = np.copy(vector_og) 127 | population[i] = np.copy(mutateall(temp,0.85,mutate_range)) 128 | 129 | # generate errors for each individual in the population 130 | for j in range(pop_size): 131 | # passing a list to the get_errors function 132 | temp = population[j].tolist() 133 | err = server.get_errors(key, temp) 134 | # adding the two errors and storing in parenterror - fitness function 135 | parenterrors[j] = np.copy((err[0]+1.5*err[1])) 136 | parenterrors1[j] = np.copy((err[0])) 137 | parenterrors2[j] = np.copy((err[1])) 138 | 139 | # have to change this to a while loop with appropriate condition later 140 | for iter_num in range(iter): 141 | 142 | if((iter_num)%6==0 and iter_num!=0): 143 | mutate_range-=0.01 144 | prob_mut_cross+=0.01 145 | # print("::::::::::::::::changing ", mutate_range) 146 | 147 | #parents with their errors 148 | arrparents=np.copy(population) 149 | arrparrerrs=np.copy(parenterrors) 150 | 151 | #has popsize/2 pairs, each is a set of parents used to generate two children 152 | arrchoices=np.zeros((int(pop_size/2),2)) 153 | 154 | #has the array of all the children 155 | arrchildren=np.zeros((pop_size,MAX_DEG)) 156 | arrchildrenmutated=np.zeros((pop_size,MAX_DEG)) 157 | arrchilderrors=np.zeros((pop_size)) 158 | 159 | # print("\n\n\n\n********"+str(iter_num)+"*********") 160 | 161 | parenerrorsinds = parenterrors.argsort() 162 | parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) 163 | parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) 164 | parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) 165 | population = np.copy(population[parenerrorsinds[::1]]) 166 | 167 | # debug statements 168 | # for j in range(pop_size): 169 | # print("person " + str(j)+" errorfunc" + str(parenterrors[j])) 170 | # print("person " + str(j)+" error1" + str(parenterrors1[j])) 171 | # print("person " + str(j)+" error2" + str(parenterrors2[j])) 172 | # print("\tvalues"+str(population[j])+"\n\n") 173 | 174 | # Assign probabilities to the population 175 | # parentprobalities = gen_parent_probabilities(pop_size) 176 | 177 | child_population = np.zeros((pop_size, MAX_DEG)) 178 | new_iter = 0 179 | 180 | while(new_iter < pop_size): 181 | 182 | # TODO: WE MAY HAVE TO CHOOSE BETWEEN THESE TWO OPTIONS 183 | # arr = crossover_select(parentprobalities) 184 | 185 | # TODO: Select randomly among top k parents (For now k =10) 186 | arr = crossover_select2(parenterrors, cross_select_from) 187 | 188 | # Sending parents for crossover 189 | temp = crossover(population[arr[0]], population[arr[1]],mutate_range,prob_mut_cross) 190 | if temp[0].tolist() == population[arr[0]].tolist() or temp[1].tolist() == population[arr[0]].tolist() or temp[0].tolist() == population[arr[1]].tolist() or temp[1].tolist() == population[arr[1]].tolist(): 191 | # print("repeated") 192 | # print("first", temp[0]) 193 | # print("Second", temp[1]) 194 | continue 195 | 196 | arrchoices[int(new_iter/2)][0]=np.copy(arr[0]) 197 | arrchoices[int(new_iter/2)][1]=np.copy(arr[1]) 198 | 199 | 200 | arrchildren[new_iter]=np.copy(temp[2]) 201 | arrchildrenmutated[new_iter]=np.copy(temp[0]) 202 | child_population[new_iter] = np.copy(temp[0]) 203 | new_iter += 1 204 | 205 | arrchildren[new_iter]=np.copy(temp[3]) 206 | arrchildrenmutated[new_iter]=np.copy(temp[1]) 207 | child_population[new_iter] = np.copy(temp[1]) 208 | new_iter += 1 209 | 210 | childerrors = np.zeros(pop_size) 211 | childerrors1 = np.zeros(pop_size) 212 | childerrors2 = np.zeros(pop_size) 213 | 214 | # generate errors for each child 215 | for j in range(pop_size): 216 | temp = child_population[j].tolist() 217 | err = server.get_errors(key, temp) 218 | 219 | # adding the two errors and storing in parenterror 220 | childerrors[j] = np.copy((err[0]+1.5*err[1])) 221 | childerrors1[j] = np.copy((err[0])) 222 | childerrors2[j] = np.copy((err[1])) 223 | arrchilderrors[j]=np.copy(childerrors[j]) 224 | 225 | # Sort children 226 | childinds = np.copy(childerrors.argsort()) 227 | childerrors = np.copy(childerrors[childinds[::1]]) 228 | childerrors1 = np.copy(childerrors1[childinds[::1]]) 229 | childerrors2 = np.copy(childerrors2[childinds[::1]]) 230 | child_population = np.copy(child_population[childinds[::1]]) 231 | # TODO: Select the best select_sure number of parents and chilren [select these many parents and children for sure] 232 | 233 | # now the children are sorted and stored in child and parents are sorted in population 234 | # we will now create a tempbank array to store top k parents, top k childs and rest being sorted taking from the top 235 | tempbankerr = np.zeros(pop_size) 236 | tempbankerr1 = np.zeros(pop_size) 237 | tempbankerr2 = np.zeros(pop_size) 238 | tempbank= np.zeros((pop_size, MAX_DEG)) 239 | 240 | for j in range(select_sure): 241 | 242 | #choosing the top jth parent and putting in the array 243 | tempbank[j]=np.copy(population[j]) 244 | tempbankerr[j]=np.copy(parenterrors[j]) 245 | tempbankerr1[j]=np.copy(parenterrors1[j]) 246 | tempbankerr2[j]=np.copy(parenterrors2[j]) 247 | 248 | #choosing the top jth child and putting it into the array 249 | tempbank[j+select_sure]=np.copy(child_population[j]) 250 | tempbankerr[j+select_sure]=np.copy(childerrors[j]) 251 | tempbankerr1[j+select_sure]=np.copy(childerrors1[j]) 252 | tempbankerr2[j+select_sure]=np.copy(childerrors2[j]) 253 | 254 | # combining parents and children into one array 255 | # TODO: Concatenating remaining parents and children and selecting from them 256 | candidates = np.copy(np.concatenate([population[select_sure:], child_population[select_sure:]])) 257 | candidate_errors = np.copy(np.concatenate([parenterrors[select_sure:], childerrors[select_sure:]])) 258 | candidate_errors1 = np.copy(np.concatenate([parenterrors1[select_sure:], childerrors1[select_sure:]])) 259 | candidate_errors2 = np.copy(np.concatenate([parenterrors2[select_sure:], childerrors2[select_sure:]])) 260 | 261 | # sorting all the candidates by error 262 | candidate_errors_inds = candidate_errors.argsort() 263 | candidate_errors = np.copy(candidate_errors[candidate_errors_inds[::1]]) 264 | candidate_errors1 = np.copy(candidate_errors1[candidate_errors_inds[::1]]) 265 | candidate_errors2 = np.copy(candidate_errors2[candidate_errors_inds[::1]]) 266 | candidates = np.copy(candidates[candidate_errors_inds[::1]]) 267 | 268 | # TODO: Select the best popsize - 2*(select_sure) 269 | cand_iter = 0 270 | 271 | while(cand_iter + 2*select_sure < pop_size): 272 | tempbank[cand_iter+2*select_sure] = np.copy(candidates[cand_iter]) 273 | tempbankerr[cand_iter+2*select_sure] = np.copy(candidate_errors[cand_iter]) 274 | tempbankerr1[cand_iter+2*select_sure] = np.copy(candidate_errors1[cand_iter]) 275 | tempbankerr2[cand_iter+2*select_sure] = np.copy(candidate_errors2[cand_iter]) 276 | cand_iter += 1 277 | 278 | 279 | #now setting the next population 280 | population=np.copy(tempbank) 281 | parenterrors=np.copy(tempbankerr) 282 | parenterrors1=np.copy(tempbankerr1) 283 | parenterrors2=np.copy(tempbankerr2) 284 | 285 | #we will now sort before updating min_error 286 | parenerrorsinds = parenterrors.argsort() 287 | parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) 288 | parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) 289 | parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) 290 | population = np.copy(population[parenerrorsinds[::1]]) 291 | 292 | 293 | if(min_error == -1 or min_error > parenterrors[0]): 294 | to_send = np.copy(population[0]) 295 | min_error = np.copy(parenterrors[0]) 296 | min_error1 = np.copy(parenterrors1[0]) 297 | min_error2 = np.copy(parenterrors2[0]) 298 | nochange=0 299 | 300 | else: 301 | print("no improvement!!!") 302 | nochange+=1 303 | # if(nochange>10): 304 | # print("Breaking") 305 | # print("-------------------------------------------------------------------------------\n") 306 | # print("Min error = ", min_error, "\n\n") 307 | # print("Min error1 = ", min_error1, "\n\n") 308 | # print("Min error2 = ", min_error2, "\n\n") 309 | # break 310 | print("-------------------------------------------------------------------------------\n") 311 | print("Min error = ", min_error, "\n\n") 312 | print("Min error1 = ", min_error1, "\n\n") 313 | print("Min error2 = ", min_error2, "\n\n") 314 | 315 | showtable(arrparents,arrparrerrs,arrchoices,arrchildren,arrchildrenmutated,arrchilderrors) 316 | 317 | # if(min_error<40000000): 318 | # print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) 319 | 320 | return to_send 321 | 322 | 323 | to_send = main() 324 | print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", 325 | server.submit(key, to_send.tolist())) 326 | print("Code finished") 327 | print("-------------------------------------------------------------------------------\n\n") 328 | -------------------------------------------------------------------------------- /A3/intermediate/tester.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | MAX_DEG=11 #number of features 4 | weights=np.array((1,2,3,4,5,1,-1,1,-7,1,1),float) 5 | 6 | hi=5 7 | lo=-5 8 | 9 | x_train=(hi-lo)*np.random.random_sample((1000,MAX_DEG))+lo 10 | y_train=np.sum(weights*x_train,axis=1) 11 | 12 | x_test=(hi-lo)*np.random.random_sample((1000,MAX_DEG))+lo 13 | y_test=np.sum(weights*x_test,axis=1) 14 | 15 | def get_errors(id,vector): 16 | 17 | for i in vector: assert -10<=abs(i)<=10 18 | assert len(vector) == MAX_DEG 19 | ans=[] 20 | y_out_train=np.sum(vector*x_train,axis=1) 21 | 22 | mse1=np.mean((y_out_train-y_train)*(y_out_train-y_train)) 23 | ans.append(mse1) 24 | 25 | y_out_test=np.sum(vector*x_test,axis=1) 26 | mse2=np.mean((y_out_test-y_test)*(y_out_test-y_test)) 27 | ans.append(mse2) 28 | 29 | return ans 30 | 31 | 32 | def submit(id, vector): 33 | """ 34 | used to make official submission of your weight vector 35 | returns string "successfully submitted" if properly submitted. 36 | """ 37 | for i in vector: assert -10<=abs(i)<=10 38 | assert len(vector) == MAX_DEG 39 | return 'successfully submitted' 40 | 41 | # print(get_errors('as',[1,1,3])) 42 | -------------------------------------------------------------------------------- /A3/intermediate/tester.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/tester.pyc -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im10_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im10_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im10_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im10_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im10_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im10_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im11_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im11_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im11_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im11_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im11_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im11_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im12_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im12_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im12_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im12_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im12_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im12_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im13_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im13_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im13_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im13_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im13_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im13_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im14_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im14_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im14_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im14_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im14_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im14_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im15_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im15_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im15_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im15_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im15_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im15_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im1_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im1_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im1_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im1_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im1_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im1_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im2_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im2_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im2_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im2_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im2_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im2_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im3_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im3_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im3_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im3_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im3_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im3_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im4_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im4_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im4_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im4_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im4_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im4_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im5_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im5_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im5_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im5_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im5_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im5_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im6_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im6_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im6_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im6_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im6_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im6_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im7_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im7_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im7_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im7_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im7_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im7_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im8_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im8_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im8_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im8_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im8_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im8_3.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im9_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im9_1.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im9_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im9_2.png -------------------------------------------------------------------------------- /A3/intermediate/traceImages/im9_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/intermediate/traceImages/im9_3.png -------------------------------------------------------------------------------- /A3/submission/Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A3/submission/Report.pdf -------------------------------------------------------------------------------- /A4/2018101041.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/A4/2018101041.pdf -------------------------------------------------------------------------------- /A4/2018101041.txt: -------------------------------------------------------------------------------- 1 | 2 2 | f[1]<170,Y 3 | f[1]>=170 and f[2]=='Long',N 4 | f[1]>=170 and f[2]=='Short' and f[0]=='Brown',N 5 | f[1]>=170 and f[2]=='Short' and f[0]=='Black',Y 6 | -------------------------------------------------------------------------------- /A4/Readme.md: -------------------------------------------------------------------------------- 1 | # Decision Trees 2 | 3 | This assigment constructs a decision tree for a given problem statement. In order to build the decision tree, we need to pick a deciding attribute at each stage so as to branch the examples from the data-set and place them in different buckets. 4 | 5 | ## Entropy: 6 | At each level in the tree, entropy is the measure of randomness in the data. It 7 | is a measure of the uncertainty of the random variable. Throughout this report the entropy 8 | is represented in terms of B(q) where q is the fraction of examples whose result is positive. 9 | 10 | ## Information Gain: 11 | For an attribute A is the expected reduction The attribute with the maximum information gain will help arrive at a decision faster. It is critical to select the right attribute for the partitioning since otherwise the depth of the tree may increase. 12 | 13 | This same process is repeated at every intermediate node to determine the best attribute based on which the examples at that node must be split. -------------------------------------------------------------------------------- /AssgnPDFs/Assignment1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/AssgnPDFs/Assignment1.pdf -------------------------------------------------------------------------------- /AssgnPDFs/Assignment_4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/AssgnPDFs/Assignment_4.pdf -------------------------------------------------------------------------------- /AssgnPDFs/MDL assgn-2 part-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/AssgnPDFs/MDL assgn-2 part-1.pdf -------------------------------------------------------------------------------- /AssgnPDFs/MDL assgn-2 part-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/AssgnPDFs/MDL assgn-2 part-2.pdf -------------------------------------------------------------------------------- /AssgnPDFs/MDL_Assignment_3/MDL_Assignment_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mallika2011/Machine-Data-Learning/27a0a321d21df8a322dda7f41b35e7218cdd5591/AssgnPDFs/MDL_Assignment_3/MDL_Assignment_3.pdf -------------------------------------------------------------------------------- /AssgnPDFs/MDL_Assignment_3/client.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | import numpy as np 4 | 5 | ######### DO NOT CHANGE ANYTHING IN THIS FILE ################## 6 | API_ENDPOINT = 'http://10.4.21.147' 7 | PORT = 3000 8 | MAX_DEG = 11 9 | 10 | #### functions that you can call 11 | def get_errors(id, vector): 12 | """ 13 | returns python array of length 2 14 | (train error and validation error) 15 | """ 16 | for i in vector: assert -1<=abs(i)<=1 17 | assert len(vector) == MAX_DEG 18 | 19 | return json.loads(send_request(id, vector, 'geterrors')) 20 | 21 | def submit(id, vector): 22 | """ 23 | used to make official submission of your weight vector 24 | returns string "successfully submitted" if properly submitted. 25 | """ 26 | for i in vector: assert -1<=abs(i)<=1 27 | assert len(vector) == MAX_DEG 28 | return send_request(id, vector, 'submit') 29 | 30 | #### utility functions 31 | def urljoin(root, port, path=''): 32 | root = root + ':' + str(port) 33 | if path: root = '/'.join([root.rstrip('/'), path.rstrip('/')]) 34 | return root 35 | 36 | def send_request(id, vector, path): 37 | api = urljoin(API_ENDPOINT, PORT, path) 38 | vector = json.dumps(vector) 39 | response = requests.post(api, data={'id':id, 'vector':vector}).text 40 | if "reported" in response: 41 | print(response) 42 | exit() 43 | 44 | return response 45 | 46 | 47 | if __name__ == "__main__": 48 | """ 49 | Replace "test" with your secret ID and just run this file 50 | to verify that the server is working for your ID. 51 | """ 52 | 53 | err = get_errors('test', list(-np.arange(0,1.1,0.1))) 54 | assert len(err) == 2 55 | 56 | submit_status = submit('test', list(-np.arange(0,1.1,0.1))) 57 | assert "submitted" in submit_status 58 | 59 | -------------------------------------------------------------------------------- /AssgnPDFs/MDL_Assignment_3/overfit_weights.txt: -------------------------------------------------------------------------------- 1 | [-0.00016927573251173823, 0.0010953590656607808, 0.003731869524518327, 0.08922889556431182, 0.03587507175384199, -0.0015634754169704097, -7.439827367266828e-05, 3.7168210026033343e-06, 1.555252501348866e-08, -2.2215895929103804e-09, 2.306783174308054e-11] 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machine Data and Learning 2 | 3 | This repository contains the assignments done for the Machine Data & Learning Course. 4 | 5 | ## Contents 6 | 7 |
    8 |
  1. Assigment 1 : Bias Variance Trade off - Understanding the notion of bias and variace with respect to a trained model and for different polunomial functions. 9 |
  2. Assigment 2 10 |
      11 |
    • Optimal policy and reward function 12 |
    • Value iteration algorithm 13 |
    14 |
  3. Assigment 3 : Genetic Algorithm - Appling the genetic algorithm in order to reduce the overfitting i.e. generalize the model so that the model performs better on unseen data. Algorithm performed given coefficients of features(a vector of size 10) corresponding to an overfit model and 15 |
  4. Assigment 4 : Decision Trees : Constructing a decision tree for a given problem statement 16 |
17 | 18 | ## Team 19 | 20 | * [Mallika Subramanian](https://github.com/mallika2011) 21 | * [Tanvi Karandikar](https://github.com/Tanvi141) 22 | --------------------------------------------------------------------------------