Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
3
3217 cw2
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
kf2n21
3217 cw2
Commits
8b32a308
Commit
8b32a308
authored
3 years ago
by
kf2n21
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
f5d455f2
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
schedule.py
+154
-0
154 additions, 0 deletions
schedule.py
with
154 additions
and
0 deletions
schedule.py
0 → 100644
+
154
−
0
View file @
8b32a308
# coding=utf-8
import
os
import
pandas
as
pd
from
pulp
import
LpMinimize
,
LpProblem
,
lpSum
,
LpVariable
import
matplotlib.pyplot
as
plt
import
numpy
as
np
cur_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
"
__file__
"
))
charts_dir
=
os
.
path
.
join
(
cur_dir
,
"
charts
"
)
def
read_input_data
()
->
(
list
,
list
,
list
,
list
):
"""
Read user task information from Excel file
"""
excel_file
=
pd
.
read_excel
(
os
.
path
.
join
(
cur_dir
,
"
COMP3217CW2Input.xlsx
"
),
sheet_name
=
"
User & Task ID
"
)
task_name
=
excel_file
[
'
User & Task ID
'
].
tolist
()
ready_time
=
excel_file
[
'
Ready Time
'
].
tolist
()
deadline
=
excel_file
[
'
Deadline
'
].
tolist
()
max_energy_per_hour
=
excel_file
[
'
Maximum scheduled energy per hour
'
].
tolist
()
energy_demand
=
excel_file
[
'
Energy Demand
'
].
tolist
()
all_tasks
=
list
()
all_task_names
=
[]
for
k
in
range
(
len
(
ready_time
)):
task
=
list
()
task
.
append
(
ready_time
[
k
])
task
.
append
(
deadline
[
k
])
task
.
append
(
max_energy_per_hour
[
k
])
task
.
append
(
energy_demand
[
k
])
all_task_names
.
append
(
task_name
[
k
])
all_tasks
.
append
(
task
)
# Reading Testing Data Output
test_data
=
pd
.
read_csv
(
'
TestingResults.txt
'
,
header
=
None
)
y_labels
=
test_data
[
24
].
tolist
()
test_data
=
test_data
.
drop
(
24
,
axis
=
1
)
x_data
=
test_data
.
values
.
tolist
()
return
all_tasks
,
all_task_names
,
x_data
,
y_labels
def
create_LP_model
(
tasks
:
list
,
task_names
:
list
)
->
LpProblem
:
"""
Create an LP model for the problem
"""
# some variables
task_vars
=
[]
c
=
[]
eq
=
[]
# create LP problem model for Minimization
model
=
LpProblem
(
name
=
"
scheduling-problem
"
,
sense
=
LpMinimize
)
# Loop through list of tasks
for
i
,
task
in
enumerate
(
tasks
):
temp_list
=
[]
# Loop between ready_time and deadline for each task
# Creates LP variables with given constraints and unique names
for
j
in
range
(
task
[
0
],
task
[
1
]
+
1
):
x
=
LpVariable
(
name
=
task_names
[
i
]
+
'
_
'
+
str
(
j
),
lowBound
=
0
,
upBound
=
task
[
2
])
temp_list
.
append
(
x
)
task_vars
.
append
(
temp_list
)
# Create objective function for price (to minimize) and add to the model
for
i
,
task
in
enumerate
(
tasks
):
for
var
in
task_vars
[
i
]:
price
=
price_list
[
int
(
var
.
name
.
split
(
'
_
'
)[
2
])]
c
.
append
(
price
*
var
)
model
+=
lpSum
(
c
)
# Add additional constraints to the model
for
i
,
task
in
enumerate
(
tasks
):
temp_list
=
[]
for
var
in
task_vars
[
i
]:
temp_list
.
append
(
var
)
eq
.
append
(
temp_list
)
model
+=
lpSum
(
temp_list
)
==
task
[
3
]
# Return model to be solved in main function
return
model
def
plot
(
model
:
LpProblem
,
count
:
int
)
->
list
:
"""
Draw the charts: hourly energy usage for users
"""
hours
=
[
str
(
x
)
for
x
in
range
(
0
,
24
)]
pos
=
np
.
arange
(
len
(
hours
))
users
=
[
"
user1
"
,
"
user2
"
,
"
user3
"
,
"
user4
"
,
"
user5
"
]
plot_list
=
[]
# Create lists to plot usage
for
user
in
users
:
temp_list
=
[]
for
hour
in
hours
:
hour_list_temp
=
[]
task_count
=
0
for
var
in
model
.
variables
():
if
user
==
var
.
name
.
split
(
'
_
'
)[
0
]
and
str
(
hour
)
==
var
.
name
.
split
(
'
_
'
)[
2
]:
task_count
+=
1
hour_list_temp
.
append
(
var
.
value
())
temp_list
.
append
(
sum
(
hour_list_temp
))
plot_list
.
append
(
temp_list
)
# Show as bar stack chart
plt
.
bar
(
pos
,
plot_list
[
0
],
edgecolor
=
'
black
'
,
bottom
=
0
)
plt
.
bar
(
pos
,
plot_list
[
1
],
edgecolor
=
'
black
'
,
bottom
=
np
.
array
(
plot_list
[
0
]))
plt
.
bar
(
pos
,
plot_list
[
2
],
edgecolor
=
'
black
'
,
bottom
=
np
.
array
(
plot_list
[
0
])
+
np
.
array
(
plot_list
[
1
]))
plt
.
bar
(
pos
,
plot_list
[
3
],
edgecolor
=
'
black
'
,
bottom
=
np
.
array
(
plot_list
[
0
])
+
np
.
array
(
plot_list
[
1
])
+
np
.
array
(
plot_list
[
2
]))
plt
.
bar
(
pos
,
plot_list
[
4
],
edgecolor
=
'
black
'
,
bottom
=
np
.
array
(
plot_list
[
0
])
+
np
.
array
(
plot_list
[
1
])
+
np
.
array
(
plot_list
[
2
])
+
np
.
array
(
plot_list
[
3
]))
# Add sum value line chart
sum_value_list
=
list
()
for
i
,
_
in
enumerate
(
plot_list
[
0
]):
sum_value_list
.
append
(
plot_list
[
0
][
i
]
+
plot_list
[
1
][
i
]
+
plot_list
[
2
][
i
]
+
plot_list
[
3
][
i
]
+
plot_list
[
4
][
i
])
plt
.
xticks
(
pos
,
hours
)
plt
.
xlabel
(
"
Time [Hour]
"
)
plt
.
ylabel
(
"
Energy Usage [kW]
"
)
plt
.
title
(
"
Energy Usage Per Hour For All Five Users
\n
Line {}
"
.
format
(
count
))
plt
.
legend
(
users
,
loc
=
0
)
# plt.show()
plt
.
savefig
(
"
{}/bar_stack_{}.png
"
.
format
(
charts_dir
,
count
))
plt
.
clf
()
pos_str
=
[
str
(
p
)
for
p
in
pos
]
plt
.
plot
(
pos_str
,
sum_value_list
)
plt
.
xlabel
(
"
Time [Hour]
"
)
plt
.
ylabel
(
"
Energy Usage [kW]
"
)
plt
.
title
(
"
Energy Usage Per Hour For All Five Users
\n
Line {}
"
.
format
(
count
))
plt
.
savefig
(
"
{}/line_{}.png
"
.
format
(
charts_dir
,
count
))
plt
.
clf
()
return
plot_list
if
__name__
==
"
__main__
"
:
tasks
,
task_names
,
x_data
,
y_labels
=
read_input_data
()
for
ind
,
price_list
in
enumerate
(
x_data
):
# Schedule and plot abnormal guideline pricing curves
if
y_labels
[
ind
]
==
1
:
# Solve returned LP model for scheduling solution
model
=
create_LP_model
(
tasks
,
task_names
)
answer
=
model
.
solve
()
# draw hourly usage for scheduling solution
plot
(
model
,
ind
+
1
)
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