2017-11-18 8 views
-2

나는 A * 알고리즘을 구현하려고 시도했으며 위키 백과 의사 코드를 따라 이것을 작성했습니다.자바 스크립트에서 null이 아닌 객체를 funtion에 전달한 후 객체가 null이라고합니다.

내가 사전 정의 된 객체 픽셀을 getG() 함수에 전달하면 객체가 null이라는 메시지가 나타납니다. 미안하지만 특정 문제를 지적하지는 않지만 실제로 문제를 이름으로 지정하는 방법을 모르겠다. 나는 가독성을 높이기 위해 코드를 주석 처리 해 보았습니다.

전체 프로젝트의

자식 저장소 링크 - https://github.com/NirobNabil/WhirlWind

(일 내가 처음에 github에 사용하지 않았기 때문에 여기 좀 지저분하고 내가 문제를 게시 조금 전에 그것을 업로드)

[실제로 arduino에 의해 구동되는 봇의 경로를 찾기 위해 *를 사용하고 있습니다. 그게 왜 내가 involt를 사용하고 있는지. 여기서

코드,

$(function() { 
 
    // define the height, width and bot size in centemeter 
 
    total_width = 200; 
 
    total_height = 200; 
 
    bot_size = 20; 
 
    total_box = (total_height/bot_size) * (total_width/bot_size); 
 
    box_in_x = total_width/bot_size; 
 
    box_in_y = total_height/bot_size; 
 
    //populating the pixels array 
 
    populate(total_width/bot_size, total_height/bot_size, "UND"); 
 
    pathfind(pixels, pixels[13], pixels[pixels.length - 1]); 
 
}) 
 

 
var pixels = []; //an array to hold all the objects(the grid) 
 

 
var obstacles = []; //array to hold the obstacles 
 

 
function pixel(X, Y, obs) { 
 
    this.X_co_ordinate = X; 
 
    this.Y_co_ordinate = Y; 
 
    this.state = obs; //availale states OPN, UND, OBS, DIS, NULL 
 
    this.g = 0; 
 
    this.h = 0; 
 
    this.f = 0; 
 
    this.last = null; 
 
} //every block in the grid is a pixel 
 

 
//01719372596 
 

 
function populate(height, width, obs_val = "UND") { 
 

 
    pixels[0] = new pixel(0, 10, obs_val); 
 

 
    for (h = height, i = 0; h >= 0; h--) { 
 
    for (w = 0; w < width; w++, i++) { 
 
     var temp_obs = new pixel(w, h, obs_val); 
 
     temp_obs.last = pixels[0]; 
 
     pixels[i] = temp_obs; //saving temp_pixel object to pixels array 
 
    } 
 
    } 
 

 
} //populating the grid AKA pixels with pixel objects or blocks 
 

 
// this funtion is where the problem shows up 
 
function getG(current, start) { 
 
    let g = 1; 
 
    while (current != start && current.last != start && current) { 
 
    current = current.last; 
 
    g++; 
 
    } 
 

 
    return g; 
 
} //get the g val(cost to come to this pixel from the start) of the current pixel 
 

 

 
function getH(current, end) { 
 
    let I = Math.abs(current.X_co_ordinate - end.X_co_ordinate) + Math.abs(current.Y_co_ordinate - end.Y_co_ordinate); 
 
    return I; 
 
} //get the h val(heuristic) of the current pixel 
 

 
function getF(start, current, end) { 
 
    let G = getG(current, start); 
 
    let H = getH(current, end); 
 
    return G + H; 
 
} //get the f val(total) of the current pixel 
 

 
function lowFinArray(arr, start, end) { 
 
    // here arr is the grid/pixel 
 
    let current_low = arr[0]; 
 
    for (let i = 0; i < arr.length; i++) { 
 
    let getF1 = getF(start, current_low, end); 
 
    let getF2 = getF(start, arr[i], end); 
 
    if (getF1 < getF2) { 
 
     current_low = arr[i]; 
 
    } 
 
    } 
 

 
    console.log("current low"); 
 
    console.log(current_low); 
 

 
    return current_low; 
 
} 
 

 
function getneighbours(grid, current) { 
 

 
    let neightbours = []; 
 

 
    neightbours.push(grid[getIndex(current.X_co_ordinate - 1, current.Y_co_ordinate)]); 
 
    neightbours.push(grid[getIndex(current.X_co_ordinate + 1, current.Y_co_ordinate)]); 
 
    neightbours.push(grid[getIndex(current.X_co_ordinate, current.Y_co_ordinate - 1)]); 
 
    neightbours.push(grid[getIndex(current.X_co_ordinate, current.Y_co_ordinate + 1)]); 
 

 
    /* 
 
    for(i=0; i<neightbours.length; i++){ 
 
     neightbours[i].last = current; 
 
    }*/ 
 
    console.log("neightbours"); 
 
    console.log(neightbours); 
 
    return neightbours; 
 
} //get the neighbour pixels of the current pixel 
 

 

 

 
//main algo 
 
function pathfind(grid, start, end) { 
 

 
    let closedSet = []; 
 
    let openSet = []; 
 
    openSet.push(start); 
 
    let current = start; 
 

 
    //trying to debug 
 
    console.log("low F in arr"); 
 
    console.log(lowFinArray(grid, start, end)); 
 
    console.log(start); 
 
    console.log(current); 
 
    console.log(end); 
 
    console.log(grid); 
 

 

 
    let x = 0; 
 
    while (openSet.length > 0) { 
 

 
    //trying to debug 
 
    console.log("executed " + (x++)); 
 
    console.log("openset"); 
 
    console.log(openSet); 
 

 

 
    current = lowFinArray(grid, start, end); //assigning the pixel with lowest f val to current 
 
    console.log("current"); 
 
    console.log(current); 
 

 
    if (current == end) { 
 

 
     console.log(getPath(current)); 
 
    } 
 

 
    let neighbours = getneighbours(grid, current); 
 
    for (let i = 0; i < neighbours.length; i++) { 
 

 
     let neighbour = neighbours[i]; 
 

 
     if (closedSet.includes(neighbour)) { 
 
     continue; 
 
     } 
 

 
     if (!openSet.includes(neighbours)) { 
 
     openSet.push(neighbours); 
 
     } 
 

 
     //console.log(current); 
 
     let getg = getG(current, start); 
 
     let geth = getH(current, end); 
 
     //console.log(getg); 
 
     let tGscore = getg + geth; //here getH is being used as a distance funtion 
 

 
     if (tGscore >= getg) { 
 
     continue; 
 
     } 
 

 
     neighbour.last = current; 
 
     neighbour.g = tGscore; 
 
     neighbour.f = getF(neighbour); 
 

 
    } 
 
    if (x > 10) { 
 
     return 0; 
 
    }; //the loop was running forever so i tried this to stop the loop after 10 iterations 
 
    } 
 

 
} 
 

 
function getPath(current) { 
 

 
    let path = [current]; 
 
    while (current.last != null) { 
 
    path.push(current.last); 
 
    } 
 

 
    return path; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

을 여기에 콘솔이 말씀입니다, 당신은 잘못된 순서로 수표를하고있는

Uncaught TypeError: Cannot read property 'last' of null 
    at getG (app.js:226) 
    at getF (app.js:241) 
    at lowFinArray (app.js:249) 
    at pathfind (app.js:292) 
    at HTMLDocument.<anonymous> (app.js:92) 
    at mightThrow (jquery-3.1.1.js:3570) 
    at process (jquery-3.1.1.js:3638) 
+1

문제의 [mcve] 표현으로 축척 해주세요. – charlietfl

답변

2

간다 :

while (current != start && current.last != start && current) { 

current.last을 이미 사용한 후에는 && current을 사용할 필요가 없습니다.

아마도 순서를 변경하면 문제가 해결됩니다. 그것은 적어도 현재의 오류를 제거합니다

while (current && current != start && current.last != start) { 

이 질문의 제목에 대해서 :

그것은 아주 잘 시간의 null 이외의 100 %가 될 수

In javascript, after i pass a non null object to a funtion it says the object is null

당신이로 통과 함수를 사용하지만 함수 을 반복해서 덮어 쓰면 함수가이되므로 모든 베팅이 해제됩니다.

+0

감사합니다. 실제로 그것은 타이핑 실수였습니다. 그것을 눈치 채지 못하는 것은 절름발이입니다. 그런 절름발이 실수로 시간을 낭비해서 죄송합니다. ( – Nabil