Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
Price Attack Classifier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jj1g18
Price Attack Classifier
Commits
35a37770
Commit
35a37770
authored
May 21, 2021
by
jj1g18
Browse files
Options
Downloads
Patches
Plain Diff
Added linear programming alg with bar charts
parent
c7232ee5
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+64
-22
64 additions, 22 deletions
main.py
with
64 additions
and
22 deletions
main.py
+
64
−
22
View file @
35a37770
...
@@ -4,25 +4,52 @@
...
@@ -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
# 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
# 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
import
pandas
as
pd
from
sklearn
import
svm
from
sklearn
import
svm
from
sklearn.model_selection
import
train_test_split
from
sklearn.model_selection
import
train_test_split
import
matplotlib.pyplot
as
plt
#function reads from given file and adds all values to a list, removing newline and spacing
#Takes param:
#filename of type String
def
performLinearFunc
(
k
,
inputx
)
:
#Returns:
multiVars
=
[]
#list_of_datasets of type List of List of String
hourList
=
[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
def
read_from_file
(
filename
)
:
costProb
=
LpProblem
(
f
"
costProblem
"
,
LpMinimize
)
file
=
open
(
filename
,
"
r
"
)
for
i
in
range
(
0
,
50
):
list_of_datasets
=
[]
x
=
{
j
:
LpVariable
(
f
"
userTask
{
i
}
Hr
{
j
}
"
,
0
,
inputx
.
T
[
i
][
"
Maximum scheduled energy per hour
"
])
for
j
in
for
line
in
file
:
range
(
inputx
.
T
[
i
][
"
Ready Time
"
],
inputx
.
T
[
i
][
"
Deadline
"
]
+
1
)}
separated
=
line
.
split
(
"
,
"
)
costProb
+=
lpSum
(
x
.
values
())
==
inputx
.
T
[
i
][
"
Energy Demand
"
]
lastItems
=
separated
[
len
(
separated
)
-
1
].
split
(
"
\n
"
)
tempEqus
=
[]
separated
.
pop
(
len
(
separated
)
-
1
)
for
l
in
range
(
0
,
len
(
x
)):
separated
.
append
(
lastItems
[
0
].
strip
())
#print(x[inputx.T[i]["Ready Time"] + l])
list_of_datasets
.
append
(
separated
)
#print(k[inputx.T[i]["Ready Time"]])
return
list_of_datasets
#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"))
#print(read_from_file("TestingData.txt"))
data
=
pd
.
read_csv
(
"
TrainingData.txt
"
,
header
=
None
)
data
=
pd
.
read_csv
(
"
TrainingData.txt
"
,
header
=
None
)
...
@@ -69,7 +96,7 @@ x_train["Var"] = trainVar
...
@@ -69,7 +96,7 @@ x_train["Var"] = trainVar
x_train
[
"
Median
"
]
=
trainMed
x_train
[
"
Median
"
]
=
trainMed
x_train
[
"
LowQ
"
]
=
trainLQ
x_train
[
"
LowQ
"
]
=
trainLQ
x_train
[
"
UpQ
"
]
=
trainUQ
x_train
[
"
UpQ
"
]
=
trainUQ
print
(
x_train
)
#
print(x_train)
x_test
[
"
Mean
"
]
=
testMean
x_test
[
"
Mean
"
]
=
testMean
x_test
[
"
Std
"
]
=
testStd
x_test
[
"
Std
"
]
=
testStd
...
@@ -78,15 +105,15 @@ x_test["Var"] = testVar
...
@@ -78,15 +105,15 @@ x_test["Var"] = testVar
x_test
[
"
Median
"
]
=
testMed
x_test
[
"
Median
"
]
=
testMed
x_test
[
"
LowQ
"
]
=
testLQ
x_test
[
"
LowQ
"
]
=
testLQ
x_test
[
"
UpQ
"
]
=
testUQ
x_test
[
"
UpQ
"
]
=
testUQ
print
(
x_test
)
#
print(x_test)
clfSvm
=
svm
.
SVC
()
clfSvm
=
svm
.
SVC
()
clfSvm
.
fit
(
x_train
,
y_train
)
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
)
newTestData
=
pd
.
read_csv
(
"
TestingData.txt
"
,
header
=
None
)
testData
=
pd
.
read_csv
(
"
TestingData.txt
"
,
header
=
None
)
testData
=
pd
.
read_csv
(
"
TestingData.txt
"
,
header
=
None
)
print
(
newTestData
)
#
print(newTestData)
testMean
=
testData
.
T
.
mean
()
testMean
=
testData
.
T
.
mean
()
testStd
=
testData
.
T
.
std
()
testStd
=
testData
.
T
.
std
()
...
@@ -105,6 +132,21 @@ testData["UpQ"] = testUQ
...
@@ -105,6 +132,21 @@ testData["UpQ"] = testUQ
y_testData
=
clfSvm
.
predict
(
testData
)
y_testData
=
clfSvm
.
predict
(
testData
)
newTestData
[
24
]
=
y_testData
newTestData
[
24
]
=
y_testData
print
(
newTestData
)
#
print(newTestData)
newTestData
.
to_csv
(
path_or_buf
=
"
TestingResults.txt
"
,
header
=
False
,
index
=
False
)
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]):
for
index
,
row
in
testAbnorm
.
iterrows
():
print
(
performLinearFunc
(
row
,
inputx
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment