-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.js
More file actions
174 lines (153 loc) · 6.44 KB
/
Copy pathalgorithms.js
File metadata and controls
174 lines (153 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function formatSequence(sequence) {
return sequence.map((value) => `Д${value + 1}`).join(' -> ');
}
function formatNumber(value) {
return Number(value).toFixed(2).replace(/\.00$/, '');
}
function formatPercent(value) {
return `${(value * 100).toFixed(1)}%`;
}
function formatSigned(value, suffix = '') {
const sign = value > 0 ? '+' : '';
return `${sign}${Number(value).toFixed(1).replace(/\.0$/, '')}${suffix}`;
}
function getScore(schedule, criterion) {
if (criterion === 'waiting') {
return schedule.totalIdle;
}
if (criterion === 'load') {
return -schedule.averageLoad;
}
return schedule.makespan;
}
class MatrixUtils {
static createEmptyMatrix(jobs, machines) {
return Array.from({ length: jobs }, () => Array.from({ length: machines }, () => 0));
}
static randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
class ScheduleBuilder {
static build(matrix, sequence, methodName) {
const machines = matrix[0].length;
const jobs = matrix.length;
const machineEnds = Array(machines).fill(0);
const jobEnds = Array.from({ length: jobs }, () => Array(machines).fill(0));
const machineBusy = Array(machines).fill(0);
const jobProcessing = Array(jobs).fill(0);
const jobStart = Array(jobs).fill(null);
const jobFinish = Array(jobs).fill(0);
const operations = [];
let totalWork = 0;
// проходим выбранную последовательность и строим операции по всем станкам
sequence.forEach((jobIndex) => {
for (let machineIndex = 0; machineIndex < machines; machineIndex += 1) {
const duration = Number(matrix[jobIndex][machineIndex]) || 0;
const previousMachineEnd = machineIndex === 0 ? 0 : jobEnds[jobIndex][machineIndex - 1];
const start = Math.max(machineEnds[machineIndex], previousMachineEnd);
const end = start + duration;
jobEnds[jobIndex][machineIndex] = end;
machineEnds[machineIndex] = end;
machineBusy[machineIndex] += duration;
jobProcessing[jobIndex] += duration;
totalWork += duration;
if (jobStart[jobIndex] === null || start < jobStart[jobIndex]) {
jobStart[jobIndex] = start;
}
if (end > jobFinish[jobIndex]) {
jobFinish[jobIndex] = end;
}
operations.push({
jobIndex,
machineIndex,
duration,
start,
end
});
}
});
const makespan = Math.max(...machineEnds);
const machineIdle = machineBusy.map((work) => Math.max(makespan - work, 0));
const jobStartSafe = jobStart.map((value) => (value === null ? 0 : value));
const jobFlow = jobFinish.map((finish, jobIndex) => Math.max(finish - jobStartSafe[jobIndex], 0));
const jobWaiting = jobFlow.map((flow, jobIndex) => Math.max(flow - jobProcessing[jobIndex], 0));
const totalIdle = machineIdle.reduce((sum, value) => sum + value, 0);
// считаем итоговые показатели для сравнения расписаний
return {
methodName,
sequence: [...sequence],
operations,
machineCount: machines,
jobCount: jobs,
makespan,
totalIdle,
averageLoad: makespan === 0 ? 0 : totalWork / (makespan * machines),
machineBusy,
machineIdle,
jobProcessing,
jobWaiting,
jobStart: jobStartSafe,
jobFinish,
jobFlow,
score: 0,
rank: 0
};
}
}
class JohnsonScheduler {
static getSchedules(matrix) {
const schedules = [];
schedules.push(
ScheduleBuilder.build(
matrix,
this.sortJobs(matrix, (row) => row[0], 'asc'),
'Обобщение Джонсона: по 1-му станку'
)
);
schedules.push(
ScheduleBuilder.build(
matrix,
this.sortJobs(matrix, (row) => row[row.length - 1], 'desc'),
'Обобщение Джонсона: по последнему станку'
)
);
return schedules;
}
// возвращаем порядок деталей по выбранному правилу Джонсона
static sortJobs(matrix, selector, direction) {
return matrix
.map((row, index) => ({ index, value: selector(row, index) }))
.sort((a, b) => {
if (direction === 'asc') {
return a.value - b.value || a.index - b.index;
}
return b.value - a.value || a.index - b.index;
})
.map((item) => item.index);
}
}
class PetrovSokolitsynScheduler {
static getSchedules(matrix) {
const firstSums = matrix.map((row) => row.slice(1).reduce((sum, value) => sum + Number(value || 0), 0));
const secondSums = matrix.map((row) => row.slice(0, -1).reduce((sum, value) => sum + Number(value || 0), 0));
const diff = firstSums.map((value, index) => value - secondSums[index]);
return [
ScheduleBuilder.build(matrix, this.sortByArray(firstSums, 'desc'), 'Петров-Соколицын: сумма без первого станка'),
ScheduleBuilder.build(matrix, this.sortByArray(secondSums, 'asc'), 'Петров-Соколицын: сумма без последнего станка'),
ScheduleBuilder.build(matrix, this.sortByArray(diff, 'desc'), 'Петров-Соколицын: по разности')
];
}
// строим последовательность по заранее посчитанному массиву значений
static sortByArray(values, direction) {
return values
.map((value, index) => ({ index, value }))
.sort((a, b) => {
if (direction === 'asc') {
return a.value - b.value || a.index - b.index;
}
return b.value - a.value || a.index - b.index;
})
.map((item) => item.index);
}
}