From 35a377709c58747217ce2a6cb4bff80cb162458e Mon Sep 17 00:00:00 2001 From: jj1g18 <jj1g18@soton.ac.uk> Date: Fri, 21 May 2021 14:25:45 +0100 Subject: [PATCH] Added linear programming alg with bar charts --- main.py | 86 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index fd371a7..3cc5ef6 100644 --- a/main.py +++ b/main.py @@ -4,25 +4,52 @@ # What I need to do is create a model that solves for the user's task energy usage and minimizes their cost # by using their task times from the excel sheet and guideline pricing data from the TrainingData and TestingData texts +from pulp import * import pandas as pd from sklearn import svm from sklearn.model_selection import train_test_split - -#function reads from given file and adds all values to a list, removing newline and spacing -#Takes param: -#filename of type String -#Returns: -#list_of_datasets of type List of List of String -def read_from_file(filename) : - file = open(filename, "r") - list_of_datasets = [] - for line in file: - separated = line.split(",") - lastItems = separated[len(separated)-1].split("\n") - separated.pop(len(separated)-1) - separated.append(lastItems[0].strip()) - list_of_datasets.append(separated) - return list_of_datasets +import matplotlib.pyplot as plt + + +def performLinearFunc(k, inputx) : + multiVars = [] + hourList = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] + costProb = LpProblem(f"costProblem", LpMinimize) + for i in range(0, 50): + x = {j: LpVariable(f"userTask{i}Hr{j}", 0, inputx.T[i]["Maximum scheduled energy per hour"]) for j in + range(inputx.T[i]["Ready Time"], inputx.T[i]["Deadline"] + 1)} + costProb += lpSum(x.values()) == inputx.T[i]["Energy Demand"] + tempEqus = [] + for l in range(0, len(x)): + #print(x[inputx.T[i]["Ready Time"] + l]) + #print(k[inputx.T[i]["Ready Time"]]) + #print(inputx.T[i]["Ready Time"] + l) + #print(hourList[inputx.T[i]["Ready Time"] + l]) + #print(x[inputx.T[i]["Ready Time"] + l]) + hourList[inputx.T[i]["Ready Time"] + l].append(x[inputx.T[i]["Ready Time"] + l]) + tempEqus.append(k[inputx.T[i]["Ready Time"] + l] * x[inputx.T[i]["Ready Time"] + l]) + multiVars.append(lpSum(tempEqus)) + #print(hourList) + objective = lpSum(multiVars) + costProb += objective + total = costProb.solve() + total = value(costProb.objective) + totalHours = [] + for hours in range(0,24): + sumList = [] + for u in range(0, len(hourList[hours])): + sumList.append(value(hourList[hours][u])) + totalHours.append(sum(sumList)) + + xAxis = [hour for hour in range(0,24)] + xPos = [v for v, _ in enumerate(xAxis)] + plt.bar(xPos, totalHours, color="blue") + plt.xlabel("Hour of Day") + plt.ylabel("Total Energy Consumed") + plt.title("Total energy consumption at each hour for 5 users") + plt.xticks(xPos, xAxis) + plt.show() + return total #print(read_from_file("TestingData.txt")) data = pd.read_csv("TrainingData.txt", header=None) @@ -69,7 +96,7 @@ x_train["Var"] = trainVar x_train["Median"] = trainMed x_train["LowQ"] = trainLQ x_train["UpQ"] = trainUQ -print(x_train) +#print(x_train) x_test["Mean"] = testMean x_test["Std"] = testStd @@ -78,15 +105,15 @@ x_test["Var"] = testVar x_test["Median"] = testMed x_test["LowQ"] = testLQ x_test["UpQ"] = testUQ -print(x_test) +#print(x_test) clfSvm = svm.SVC() clfSvm.fit(x_train, y_train) -print(clfSvm.score(x_test, y_test)) +#print(clfSvm.score(x_test, y_test)) newTestData = pd.read_csv("TestingData.txt", header=None) testData = pd.read_csv("TestingData.txt", header=None) -print(newTestData) +#print(newTestData) testMean = testData.T.mean() testStd = testData.T.std() @@ -105,6 +132,21 @@ testData["UpQ"] = testUQ y_testData = clfSvm.predict(testData) newTestData[24] = y_testData -print(newTestData) +#print(newTestData) + +newTestData.to_csv(path_or_buf="TestingResults.txt", header=False, index=False) + +inputx = pd.read_excel("COMP3217CW2Input.xlsx") +#print(inputx) + +isAbnorm = newTestData[24] == 1 +testAbnorm = newTestData[isAbnorm] +print(testAbnorm) + +#solve for all hours for task*max scheduled = energy demand +#Then solve for minimize total cost so all hours' energy usage * cost for that hour + +#for k in range(0, testAbnorm.shape[0]): -newTestData.to_csv(path_or_buf="TestingResults.txt", header=False, index=False) \ No newline at end of file +for index, row in testAbnorm.iterrows(): + print(performLinearFunc(row, inputx)) -- GitLab