fe944750 |
import {
find,
map,
propEq,
reduce,
range,
times, |
8c7841d1 |
last, filter, identity |
fe944750 |
} from 'rambda';
import type { JobId } from '../../types';
import { createSchedule as johnson, updateCTime } from '../johnson'; |
b681b265 |
import { max } from 'ramda'; |
fe944750 |
type JobOperations = {
jobId: JobId, |
8c7841d1 |
operations: Array<{ t: number, name: string }> |
fe944750 |
};
type JobOperationsWithC = {
jobId: JobId, |
8c7841d1 |
operations: Array<{ t: number, c: number, name: string }> |
fe944750 |
};
|
8c7841d1 |
export const fillCorrupted = (jobsOperations: JobOperations[], processorsCount) =>
map( |
b681b265 |
({ operations, ...rest }) => {
const fixedOperations = [...operations]; |
8c7841d1 |
fixedOperations.length = processorsCount; |
b681b265 |
return {
...rest, |
8c7841d1 |
operations: map(o => o || { t: 0 }, fixedOperations) |
b681b265 |
};
},
jobsOperations |
8c7841d1 |
); |
b681b265 |
|
fe944750 |
export const createSchedule = (jobsOperations: JobOperations[], processorsCount: number): JobOperationsWithC[] => { |
8c7841d1 |
jobsOperations = fillCorrupted(jobsOperations, processorsCount); |
b681b265 |
|
fe944750 |
const allSchedules = map(k => {
const jobsOperationsForK = map((jobsOperation: JobOperations) => {
const t1 = reduce((acc, i) => acc + jobsOperation.operations[i].t, 0, range(0, k));
const t2 = reduce((acc, i) => acc + jobsOperation.operations[i].t, 0, range(processorsCount - k, processorsCount));
const operations = [
{ t: t1 },
{ t: t2 }
];
const newJobsOperations = { jobId: jobsOperation.jobId, operations };
return newJobsOperations;
}, jobsOperations);
const jobsOperationsOrderTemplate = johnson(jobsOperationsForK);
|
8c7841d1 |
const jobsOperationsReordered = map(jobOperationTemplate => { |
fe944750 |
const { operations } = find(propEq('jobId', jobOperationTemplate.jobId), jobsOperations);
return {
jobId: jobOperationTemplate.jobId,
operations
};
}, jobsOperationsOrderTemplate);
const jobsOperationsReorderedWithC = updateCTime(jobsOperationsReordered, processorsCount);
return jobsOperationsReorderedWithC;
}, range(1, processorsCount));
const bestSchedule = reduce((acc, schedule) => {
const cmaxAcc = last(last(acc).operations).c;
const cmax = last(last(schedule).operations).c;
return (cmax < cmaxAcc) ? schedule : acc;
}, [{ operations: [{ c: Infinity }] }], allSchedules);
return bestSchedule;
};
export default (jobsOperations: JobOperations[], processorsCount: number) => {
const schedule = createSchedule(jobsOperations, processorsCount);
return times(i =>
map((jobTime: JobOperationsWithC) => ({
processor: i + 1,
startTime: jobTime.operations[i].c - jobTime.operations[i].t,
endTime: jobTime.operations[i].c, |
8c7841d1 |
name: jobTime.operations[i].name |
fe944750 |
}), schedule)
, processorsCount);
}; |