re.order
Version:
An extremely fast pathfinder tool/algorithm to determine optimal order execution sequence.
103 lines (96 loc) • 2.46 kB
JavaScript
const expect = require('chai').expect;
const { createReOrder } = require('../re.order');
const orders = [
{
id: 1,
destination: 100,
dependencies: [],
startLatest: new Date(2017, 6, 5, 6, 10),
finishLatest: null,
load: 20,
jobDuration: 1200
},
{
id: 2,
destination: 110,
dependencies: [],
startLatest: null,
finishLatest: new Date(2017, 6, 5, 14, 10),
load: 30,
jobDuration: 1200
},
{
id: 3,
destination: 180,
dependencies: [1],
startLatest: new Date(2017, 6, 5, 9, 10),
finishLatest: null,
load: -5,
jobDuration: 600
},
{
id: 4,
destination: 220,
dependencies: [1, 2],
startLatest: new Date(2017, 6, 5, 12, 20),
finishLatest: null,
load: -25,
jobDuration: 1200
},
{
id: 5,
destination: 150,
dependencies: [1],
startLatest: null,
finishLatest: new Date(2017, 6, 5, 8, 10),
load: -5,
jobDuration: 600
},
{
id: 6,
destination: 220,
dependencies: [2],
startLatest: new Date(2017, 6, 5, 18, 10),
finishLatest: null,
load: -15,
jobDuration: 900
}
];
// We simplified distance as a mere absolute difference between out numeric positions:
const getDuration = (a, b, startTime) => Math.abs(a - b) * 60;
const getOrderIds = result => result.orders.map(o => o.id);
describe('Re.Order', () => {
it('should correctly reOrder', () => {
const options = {
loadCapacity: 40,
startTime: new Date(2017, 6, 5, 5, 10),
startPosition: 130,
getDuration
};
const run = createReOrder(options);
const result = run(orders);
expect(getOrderIds(result)).to.deep.equal([1, 5, 3, 2, 6, 4]);
});
it('should correctly reOrder, even if one pickup fails, continue with what we have', () => {
const options2 = {
loadCapacity: 40,
startTime: new Date(2017, 6, 5, 6, 10),
startPosition: 130,
getDuration
};
const run2 = createReOrder(options2);
const result2 = run2(orders);
expect(getOrderIds(result2)).to.deep.equal([2, 6]);
});
it('should correctly reOrder, if there is enough capacity', () => {
const options3 = {
loadCapacity: 60,
startTime: new Date(2017, 6, 5, 5, 10),
startPosition: 130,
getDuration
};
const run3 = createReOrder(options3);
const result3 = run3(orders);
expect(getOrderIds(result3)).to.deep.equal([1, 2, 5, 3, 6, 4]);
});
});