Fitting a curve to gdistance's accCost

Viewed 25

I'm trying to calculate a cost of my raster based on a TransitionLayer but in my case I have to map the costs to a polynomial curve.

For example if the following plot were normal accumulated cost graph, where x-axis is the current cost and y-axis is accumulated cost. This is possible via gdistance::accCost.

curve

However, consider the following plot. X-axis is our accumulated costs but now y-axis represents a battery discharge curve. Now everytime a new cost is calculated then it can insert the cost to the polynomial function and return a new cost. So I can simulate battery discharge over time.

second curve

I have tried to change the weight assignment in the following modified gdistance::accCost method to see if it had any effect but it didn't worked.

modaccCost <- function(x, fromCoords) {
        fromCoords <- .coordsToMatrix(fromCoords) 
        fromCells <- cellFromXY(x, fromCoords)
        if(!all(!is.na(fromCells))){
            warning("some coordinates not found and omitted")
            fromCells <- fromCells[!is.na(fromCells)]
        }
        tr <- transitionMatrix(x)
        tr <- rBind(tr,rep(0,nrow(tr)))
        tr <- cBind(tr,rep(0,nrow(tr)))
    
        startNode <- nrow(tr) #extra node to serve as origin
        adjP <- cbind(rep(startNode, times=length(fromCells)), fromCells)
    
        tr[adjP] <- Inf
    
        adjacencyGraph <- graph.adjacency(tr, mode="directed", weighted=TRUE)
        this line -> E(adjacencyGraph)$weight <- 1/(E(adjacencyGraph)$weight*2)
    
        shortestPaths <- shortest.paths(adjacencyGraph, v=startNode)[-startNode]

        result <- as(x, "RasterLayer")
        result <- setValues(result, shortestPaths)  
        return(result)
}

Do you know if there's a way to accomplish this?

0 Answers
Related