Próbuję zaimplementować jeden z algorytmów heurystycznych w języku JavaScript, jednak napotkałem na dosyć błahy problem, którego rozwiązania nie widzę...
Podświetlona linia powoduje w konsoli następujący błąd:
Uncaught TypeError: Cannot read property '0' of undefined
at countFunctionCost (algorithm.js:50)
at useSimulatedAnnealing (algorithm.js:58)
at algorithm.js:91
Kod na obecną chwilę jest następujący:
function createEmptyMatrix(numberOfProducts, numberOfMachines) {
var matrix = new Array(numberOfProducts);
for (var i = 0; i < numberOfProducts; i++) {
matrix[i] = new Array(numberOfMachines);
for (var j = 0; j < numberOfMachines; j++)
matrix[i][j] = 0;
}
return matrix;
}
function createRandomMatrix(numberOfProducts, numberOfMachines) {
var matrix = new Array(numberOfProducts);
for (var i = 0; i < numberOfProducts; i++) {
matrix[i] = new Array(numberOfMachines);
for (var j = 0; j < numberOfMachines; j++)
matrix[i][j] = Math.floor((Math.random() * 10) + 20);
}
console.log(matrix);
return matrix;
}
function createRandomVector(numberOfProducts) {
var vector = new Array(numberOfProducts);
for (var i = 0; i < numberOfProducts; i++)
vector[i] = Math.floor((Math.random() * 50) + 80);
console.log(vector);
return vector;
}
function createRandomSolution(limits, numberOfProducts, numberOfMachines) {
var solution = createEmptyMatrix(numberOfProducts, numberOfMachines);
for (var i = 0; i < numberOfProducts; i++) {
var remaining = limits[i];
for (var j = 0; j < numberOfMachines; j++) {
if (j !== numberOfMachines - 1) {
var amount = Math.floor((Math.random() * (remaining + 1)));
solution[i][j] = amount;
remaining -= amount;
} else
solution[i][j] = remaining;
}
}
console.log(solution);
}
function countFunctionCost(solution, prices, times) {
var sum = 0;
for (var i = 0; i < prices.length; i++) {
for (var j = 0; j < prices[0].length; j++) {
sum += solution[i][j] * prices[i][j] * times[i][j];
}
}
}
function useSimulatedAnnealing(temperatureStart, temperatureEnd, alpha, neighbourhoods, limits, prices, times) {
var temperature = temperatureStart;
var solution = createRandomSolution(limits, prices.length, prices[0].length);
var functionCost = countFunctionCost(solution, prices, times);
// return {
// solution: solution,
// functionCost: functionCost
// };
}
var prices = [
[4, 4, 2, 3],
[1, 2, 1, 3],
[1, 1, 1, 2],
[5, 4, 3, 3],
[7, 8, 7, 8],
[5, 5, 6, 7]
];
var times = [
[6, 7, 8, 7],
[6, 5, 3, 2],
[8, 7, 7, 6],
[1, 2, 2, 1],
[3, 2, 1, 1],
[2, 3, 3, 3]
];
var limits = [10, 15, 20, 15, 12, 16];
// var prices = createRandomMatrix(6, 4);
// var times = createRandomMatrix(6, 4);
// var limits = createRandomVector(6);
console.log(prices);
console.log(times);
var outputAlgorithm = useSimulatedAnnealing(400, 0.001, 0.999, 3, limits, prices, times);
// var solutionName = outputAlgorithm.solution;
// var functionCostName = outputAlgorithm.functionCost;
Wynika to z tablicy dwuwymiarowej solution, gdyż w funkcji countFunctionCost() mogę się odwołać do elementów prices i times.
Domyślam się, że chodzi o utworzenie solution w funkcji useSimulatedAnnealing(), jednak nie rozumiem co jest przyczyną tego błędu... Zasięg zmiennych w JS płata figle, nazewnictwo zmiennych, czy może źle przekazuję argumenty do funkcji?
Program poprawnie wyświetla wszystkie console.log()