Refactor schedule solver to solver per weekday and add charts and tables to template #409

This commit is contained in:
Kendrick Chan 2020-06-14 14:12:32 +08:00
parent d94e2353b0
commit a2fdb346b3
3 changed files with 337 additions and 120 deletions

View file

@ -24,6 +24,53 @@ use App\Entity\Hub;
class AnalyticsController extends Controller
{
protected $weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
protected $day_shifts = [
['Mon - Sat', 0, 1, 2, 3, 4, 5], // Mon - Sat
['Tue - Sun', 1, 2, 3, 4, 5, 6], // Tue - Sun
['Wed - Mon', 2, 3, 4, 5, 6, 0], // Wed - Mon
['Thu - Tue', 3, 4, 5, 6, 0, 1], // Thu - Tue
['Fri - Wed', 4, 5, 6, 0, 1, 2], // Fri - Wed
['Sat - Thu', 5, 6, 0, 1, 2, 3], // Sat - Thu
['Sun - Fri', 6, 0, 1, 2, 3, 4], // Sun - Fri
];
protected $hour_shifts = [
['00:00 - 09:00', 0, 1, 2, 3, 4, 5, 6, 7, 8],
['01:00 - 10:00', 1, 2, 3, 4, 5, 6, 7, 8, 9],
['02:00 - 11:00', 2, 3, 4, 5, 6, 7, 8, 9, 10],
['03:00 - 12:00', 3, 4, 5, 6, 7, 8, 9, 10, 11],
['04:00 - 13:00', 4, 5, 6, 7, 8, 9, 10, 11, 12],
['05:00 - 14:00', 5, 6, 7, 8, 9, 10, 11, 12, 13],
['06:00 - 15:00', 6, 7, 8, 9, 10, 11, 12, 13, 14],
['07:00 - 16:00', 7, 8, 9, 10, 11, 12, 13, 14, 15],
['08:00 - 17:00', 8, 9, 10, 11, 12, 13, 14, 15, 16],
['09:00 - 18:00', 9, 10, 11, 12, 13, 14, 15, 16, 17],
['10:00 - 19:00', 10, 11, 12, 13, 14, 15, 16, 17, 18],
['11:00 - 20:00', 11, 12, 13, 14, 15, 16, 17, 18, 19],
['12:00 - 21:00', 12, 13, 14, 15, 16, 17, 18, 19, 20],
['13:00 - 22:00', 13, 14, 15, 16, 17, 18, 19, 20, 21],
['14:00 - 23:00', 14, 15, 16, 17, 18, 19, 20, 21, 22],
['15:00 - 00:00', 15, 16, 17, 18, 19, 20, 21, 22, 23],
['16:00 - 01:00', 16, 17, 18, 19, 20, 21, 22, 23, 0],
['17:00 - 02:00', 17, 18, 19, 20, 21, 22, 23, 0, 1],
['18:00 - 03:00', 18, 19, 20, 21, 22, 23, 0, 1, 2],
['19:00 - 04:00', 19, 20, 21, 22, 23, 0, 1, 2, 3],
['20:00 - 05:00', 20, 21, 22, 23, 0, 1, 2, 3, 4],
['21:00 - 06:00', 21, 22, 23, 0, 1, 2, 3, 4, 5],
['22:00 - 07:00', 22, 23, 0, 1, 2, 3, 4, 5, 6],
['23:00 - 08:00', 23, 0, 1, 2, 3, 4, 5, 6, 7],
];
/**
* @Menu(selected="analytics_forecast")
* @IsGranted("analytics.forecast")
@ -58,7 +105,8 @@ class AnalyticsController extends Controller
$hub_list = $req->request->get('hub_ids', []);
$distances = $req->request->get('distances', []);
$today = DateTime::createFromFormat('d M Y', $req->request->get('date'));
error_log(print_r($hub_list, true));
$month = $today->format('m');
// error_log(print_r($hub_list, true));
// $hub_list = [ 6, 4, 36, 7, 8, 126, 127, 18, 12, 9, 60, 10, 21, 135 ];
@ -88,17 +136,29 @@ class AnalyticsController extends Controller
$chart_all_weekdays = $this->generateAllWeekData($c_weekday, $today, $overlaps);
// figure out the rider schedules based on the max hour values
$hour_max_values = $chart_all_weekdays['hour_max_values'];
ksort($hour_max_values);
error_log(print_r($hour_max_values, true));
unset($chart_all_weekdays['hour_max_values']);
$scheduler_data = $chart_all_weekdays['scheduler_data'];
// error_log(print_r($scheduler_data, true));
unset($chart_all_weekdays['scheduler_data']);
// run scheduler
$shift = $this->runScheduler($hour_max_values);
// send 2018 + month data
$sched_res = $this->runScheduler($scheduler_data['2018'][$month]);
// tally total JOs for the month
$total_jos = 0;
foreach ($scheduler_data['2018'][$month] as $sday_data)
{
foreach ($sday_data as $shour_data)
{
$total_jos += $shour_data;
}
}
$hub_data[$hub_id]['data_weekday'] = $chart_weekday;
$hub_data[$hub_id]['data_all_weekdays'] = $chart_all_weekdays;
$hub_data[$hub_id]['data_shift'] = $shift;
$hub_data[$hub_id]['data_shift'] = $sched_res['weekday_shifts'];
$hub_data[$hub_id]['total_jos'] = $total_jos;
$hub_data[$hub_id]['total_riders'] = $sched_res['total_riders'];
unset($hub_data[$hub_id]['c_weekday']);
}
@ -118,18 +178,26 @@ class AnalyticsController extends Controller
return $this->render('analytics/forecast_submit.html.twig', $params);
}
protected function runScheduler($hour_data)
protected function runScheduler($scheduler_data)
{
// run python script to solve scheduling for riders
$arg_string = implode('-', $hour_data);
$python_cmd = '/usr/bin/python3';
$sched_script = __DIR__ . '/../../utils/schedule_solver/solver.py';
error_log('running...' . $sched_script);
// go through the days
$args = [
$python_cmd,
$sched_script,
];
foreach ($scheduler_data as $weekday_data)
$args[] = implode('-', $weekday_data);
$proc = new Process([$python_cmd, $sched_script, $arg_string]);
error_log(print_r($args, true));
// error_log('running...' . $sched_script);
$proc = new Process($args);
$proc->run();
if (!$proc->isSuccessful())
@ -139,28 +207,74 @@ class AnalyticsController extends Controller
error_log($res);
$shifts = [];
$res_lines = explode("\n", $res);
foreach ($res_lines as $line)
// segregate into weekdays
$day_data = [];
$i = 0;
foreach ($scheduler_data as $weekday_data)
{
$hour_data = explode('-', $line);
if (count($hour_data) != 2)
continue;
$start_hour = $hour_data[0];
$rider_count = $hour_data[1];
$display_shift = sprintf('%02d:00', $start_hour);
error_log('allocating ' . $rider_count . ' for ' . $display_shift);
$shifts[] = [
'label' => $display_shift,
'count' => $rider_count,
$total_jos = 0;
foreach ($weekday_data as $hourly_jo)
$total_jos += $hourly_jo;
$day_data[$i] = [
'weekday' => $this->weekdays[$i],
'total_jos' => $total_jos,
'total_riders' => 0,
'shifts' => [],
];
$i++;
}
return $shifts;
$shifts = [];
$res_lines = explode("\n", $res);
$total_riders = 0;
foreach ($res_lines as $line)
{
// format is day shift - hour shift - number of riders
$shift_data = explode('-', $line);
if (count($shift_data) != 3)
continue;
$day_shift_index = $shift_data[0];
$hour_shift_index = $shift_data[1];
$rider_count = $shift_data[2];
$total_riders += $rider_count;
$label = $this->day_shifts[$day_shift_index][0] . ' ' . $this->hour_shifts[$hour_shift_index][0];
// initialize hours
$rider_hours = [];
for ($i = 0; $i < 24; $i++)
$rider_hours[$i] = 0;
for ($i = 1; $i < count($this->hour_shifts[$hour_shift_index]); $i++)
$rider_hours[$this->hour_shifts[$hour_shift_index][$i]] = 1;
error_log('allocating ' . $rider_count . ' for ' . $label);
// add shifts to the weekday
for ($i = 1; $i < count($this->day_shifts[$day_shift_index]); $i++)
{
$day = $this->day_shifts[$day_shift_index][$i];
$day_data[$day]['shifts'][] = [
'label' => $label,
'count' => $rider_count,
'hours' => $rider_hours,
];
$day_data[$day]['total_riders'] += $rider_count;
}
}
$data = [
'weekday_shifts' => $day_data,
'total_riders' => $total_riders,
];
return $data;
}
protected function generateHubData($em, $hub, $distance_limit, DateTime $today, &$overlaps)
@ -305,21 +419,8 @@ class AnalyticsController extends Controller
protected function generateAllWeekData($all_weekday_data, $today, $overlaps)
{
// generate all weekdays, not just one weekday
$data = [];
// build wekdays
$weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
// build hours
$hours = [];
for ($i = 0; $i < 24; $i++)
@ -327,12 +428,10 @@ class AnalyticsController extends Controller
$month = $today->format('m');
$year_data = [];
$scheduler_data = [];
// gather maximum for each hour
// TODO: make it for all years, for now we only track 2018
$hour_max_values = [];
foreach ($weekdays as $weekday)
foreach ($this->weekdays as $weekday)
{
foreach ($all_weekday_data as $year => $year_data)
{
@ -346,11 +445,10 @@ class AnalyticsController extends Controller
];
// get hour data
$year_id = 'y' . $year;
$prefix = $year_id . '_' . $weekday;
if (isset($year_data[$month][$weekday][$hour]))
{
$year_id = 'y' . $year;
$prefix = $year_id . '_' . $weekday;
// calculate the rider value for each JO and use that score as basis
$total_rv = $this->calculateTotalRiderValue($year_data[$month][$weekday][$hour]['jos'], $overlaps);
$rv_average = ceil($total_rv / $year_data[$month][$weekday][$hour]['count']);
@ -360,19 +458,24 @@ class AnalyticsController extends Controller
$data[$id][$prefix . '_average'] = ceil($year_data[$month][$weekday][$hour]['total'] / $year_data[$month][$weekday][$hour]['count']);
$data[$id][$prefix . '_rv_average'] = $rv_average;
// set maximum hour data
if (!isset($hour_max_values[$hour]) || $rv_average > $hour_max_values[$hour])
$hour_max_values[$hour] = $rv_average;
// assign scheduler data
$scheduler_data[$year][$month][$weekday][$hour] = $rv_average;
}
else
{
if (!isset($scheduler_data[$year][$month][$weekday][$hour]))
{
$data[$id][$prefix . '_rv_average'] = 0;
$scheduler_data[$year][$month][$weekday][$hour] = 0;
}
}
}
}
}
$data['scheduler_data'] = $scheduler_data;
// error_log(print_r($data, true));
$data['hour_max_values'] = $hour_max_values;
return $data;
}

View file

@ -2,6 +2,26 @@
{% block stylesheets %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
<style>
.sched_col {
width: 3.5%;
}
.marked {
background-color: #ff0000;
}
.shift-table {
margin-top: 20px;
margin-left: 20px;
margin-right: 20px;
}
.shift-table th {
text-align: center;
}
.shift-table td {
padding: 5px 10px 5px 10px;
border: 1px solid black;
}
</style>
{% endblock %}
{% block body %}
@ -45,24 +65,83 @@
<div id="month-all-weekday-chart-{{ hub.id }}" style="height: 400px;">
</div>
{% for day_data in hub.data_shift %}
<div id="shift-table-{{ hub.id }}">
<table>
<table class="shift-table">
<thead>
<tr>
<th>Shift</th>
<th>Riders</th>
<th>{{ day_data.weekday }}</th>
<th>00</th>
<th>01</th>
<th>02</th>
<th>03</th>
<th>04</th>
<th>05</th>
<th>06</th>
<th>07</th>
<th>08</th>
<th>09</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
<th>18</th>
<th>19</th>
<th>20</th>
<th>21</th>
<th>22</th>
<th>23</th>
</tr>
</thead>
<tbody>
{% for shift_data in hub.data_shift %}
{% for shift_data in day_data.shifts %}
{% for i in 1..shift_data.count %}
<tr>
<td>{{ shift_data.label }}</td>
<td>{{ shift_data.count }}</td>
<td style="width: 250px;">{{ shift_data.label }}</td>
{% for hour_coverage in shift_data.hours %}
{% if hour_coverage %}
<td class="sched_col marked"></td>
{% else %}
<td class="sched_col"></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
<table class="shift-table">
<thead>
<tr>
<th style="width: 250px;">Day</th>
<th style="width: 100px;"># JO</th>
<th style="width: 100px;"># Rider</th>
<th style="width: 100px;">JO per Rider</th>
</tr>
</thead>
<tbody>
{% for i in 0..6 %}
<tr>
<td>{{ hub.data_shift[i].weekday }}</td>
<td>{{ hub.data_shift[i].total_jos }}</td>
<td>{{ hub.data_shift[i].total_riders }}</td>
<td>{{ (hub.data_shift[i].total_jos / hub.data_shift[i].total_riders) | round(1, 'common') }}</td>
</tr>
{% endfor %}
<tr>
<td>Overall</td>
<td>{{ hub.total_jos }}</td>
<td>{{ hub.total_riders }}</td>
<td>{{ (hub.total_jos / hub.total_riders) | round(1, 'common') }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
@ -144,6 +223,7 @@ s2020.dataFields.categoryX = "date";
var chart2 = am4core.create("month-weekday-chart-{{ hub.id }}", am4charts.XYChart);
chart2.data = {{ hub.data_weekday|json_encode|raw }};
chart2.legend = new am4charts.Legend();
var xAxis = chart2.xAxes.push(new am4charts.CategoryAxis());
xAxis.dataFields.category = "hour";
@ -170,6 +250,7 @@ l2019.dataFields.categoryX = "hour";
var chart2 = am4core.create("month-weekday-chart-rv-{{ hub.id }}", am4charts.XYChart);
chart2.data = {{ hub.data_weekday|json_encode|raw }};
chart2.legend = new am4charts.Legend();
var xAxis = chart2.xAxes.push(new am4charts.CategoryAxis());
xAxis.dataFields.category = "hour";

View file

@ -3,44 +3,50 @@ from ortools.linear_solver import pywraplp
import sys
def main():
# get arguments
hours_string = sys.argv[1]
hours_data = hours_string.split('-')
# initialize hours
# days
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# hours
hours = [
['00', 0],
['01', 0],
['02', 0],
['03', 0],
['04', 0],
['05', 0],
['06', 0],
['07', 0],
['08', 0],
['09', 0],
['10', 0],
['11', 0],
['12', 0],
['13', 0],
['14', 0],
['15', 0],
['16', 0],
['17', 0],
['18', 0],
['19', 0],
['20', 0],
['21', 0],
['22', 0],
['23', 0]]
'00',
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23']
# set hours from argument
for i in range(0, len(hours_data)):
hours[i][1] = int(hours_data[i])
# initialize required hours - req_hours[days][hours]
req_hours = [[0 for x in range(len(hours))] for y in range(len(days))]
# get arguments
# there will be 7 arguments, monday to sunday schedule
for day_index in range(0, len(days)):
hours_string = sys.argv[day_index + 1]
hours_data = hours_string.split('-')
for hour_index in range(0, len(hours)):
req_hours[day_index][hour_index] = int(hours_data[hour_index])
# all shifts available
shifts = [
# all hour shifts available
hour_shifts = [
['00 - 09', 0, 1, 2, 3, 4, 5, 6, 7, 8],
['01 - 10', 1, 2, 3, 4, 5, 6, 7, 8, 9],
['02 - 11', 2, 3, 4, 5, 6, 7, 8, 9, 10],
@ -66,48 +72,75 @@ def main():
['22 - 07', 22, 23, 0, 1, 2, 3, 4, 5, 6],
['23 - 08', 23, 0, 1, 2, 3, 4, 5, 6, 7]]
# all possible days riders come in
day_shifts = [
['Mon - Sat', 0, 1, 2, 3, 4, 5], # Mon - Sat
['Tue - Sun', 1, 2, 3, 4, 5, 6], # Tue - Sun
['Wed - Mon', 2, 3, 4, 5, 6, 0], # Wed - Mon
['Thu - Tue', 3, 4, 5, 6, 0, 1], # Thu - Tue
['Fri - Wed', 4, 5, 6, 0, 1, 2], # Fri - Wed
['Sat - Thu', 5, 6, 0, 1, 2, 3], # Sat - Thu
['Sun - Fri', 6, 0, 1, 2, 3, 4]] # Sun - Fri
# build shift lookup index
# instantiate glop solver
solver = pywraplp.Solver('SolveSchedule', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
# solver variables
solv_shifts = [[]] * len(shifts)
solv_shifts = [[0 for x in range(len(hour_shifts))] for y in range(len(day_shifts))]
# objective
objective = solver.Objective()
# variables for shifts
for i in range(0, len(shifts)):
solv_shifts[i] = solver.IntVar(0, solver.infinity(), shifts[i][0])
# objective is to minimize number of shifts
objective.SetCoefficient(solv_shifts[i], 1)
for day_index in range(0, len(day_shifts)):
for hour_index in range(0, len(hour_shifts)):
solv_shifts[day_index][hour_index] = solver.IntVar(0, solver.infinity(), day_shifts[day_index][0] + ' ' + hour_shifts[hour_index][0])
# objective is to minimize number of shifts
objective.SetCoefficient(solv_shifts[day_index][hour_index], 1)
objective.SetMinimization()
# set constraints
constraints = [0] * len(hours)
for hour_index in range(0, len(hours)):
# hour personnel should be equal or less than shift personnel that covers those hours
constraints[hour_index] = solver.Constraint(hours[hour_index][1], solver.infinity())
for shift_index in range(0, len(shifts)):
# get each shift's hour coverage
for shift_hour_index in range(1, len(shifts[shift_index])):
# check if shift covers that hour
# NOTE: this can still be optimized later via indexing
if shifts[shift_index][shift_hour_index] == hour_index:
# print('hour', hour_index, 'in shift index -', shift_index)
constraints[hour_index].SetCoefficient(solv_shifts[shift_index], 1)
constraints = [[0 for x in range(len(hours))] for y in range(len(days))]
# go through all days
for day_index in range(0, len(days)):
# go through all hours
for hour_index in range(0, len(hours)):
# hour personnel should be equal or less than shift personnel that covers those hours
# set the required manpower for that day + hour combo
# print('setting constraint for', day_index, '-', hour_index, '=', req_hours[day_index][hour_index])
constraints[day_index][hour_index] = solver.Constraint(req_hours[day_index][hour_index], solver.infinity())
# go through all day shifts
for shift_day in range(0, len(day_shifts)):
# go through days inside day shift
for shift_day_index in range(1, len(day_shifts[shift_day])):
# is day shift part of that day?
if day_index == day_shifts[shift_day][shift_day_index]:
# go through all hour shifts
for shift_hour in range(0, len(hour_shifts)):
# go through all hours inside hour shift
for shift_hour_index in range(1, len(hour_shifts[shift_hour])):
# is hour shift part of the hour
if hour_index == hour_shifts[shift_hour][shift_hour_index]:
# print(day_index, ' - ', hour_index, ' vs ', day_shifts[shift_day][shift_day_index], ' - ', hour_shifts[shift_hour][shift_hour_index])
# add it to constraint
constraints[day_index][hour_index].SetCoefficient(solv_shifts[shift_day][shift_hour], 1)
# solve it!
status = solver.Solve()
#print('Number of variables =', solver.NumVariables())
#print('Number of constraints =', solver.NumConstraints())
if status == solver.OPTIMAL:
#print('Optimal solution found!')
#print('Number of variables =', solver.NumVariables())
#print('Number of constraints =', solver.NumConstraints())
for i in range(0, len(shifts)):
result = solv_shifts[i].solution_value()
if result > 0:
print(i, '-', int(solv_shifts[i].solution_value()), sep='')
for day_index in range(0, len(day_shifts)):
for hour_index in range(0, len(hour_shifts)):
result = solv_shifts[day_index][hour_index].solution_value()
if result > 0:
print(day_index, hour_index, int(solv_shifts[day_index][hour_index].solution_value()), sep='-')
#print(day_shifts[day_index][0], ' ', hour_shifts[hour_index][0], ' = ', int(solv_shifts[day_index][hour_index].solution_value()), sep='')
else:
if status == solver.FEASIBLE:
print('Feasible solution found!')