Which is the best way to add segment penality based on elevation?

We are developing an algorithm to identify the route with the least possible slope, but the formula we are currently using is not yielding satisfactory results:

slope_gradient = (targetData.datum - sourceData.datum) / segment.distance
if slope_gradient > 0 then
  segment.weight = segment.weight * (1.0 + (slope_gradient * 6.0))
elseif slope_gradient < 0 then
  segment.weight = segment.weight / (1.0 + (math.abs(slope_gradient) * 6.0))
end

Since we are unable to conduct tests, we are asking if there are any specific formulas or other suggestions for our goal.

Do you really want to minimize the slope? That means always staying at the same height if possible, even if it means a huge detour. You don’t want to minimize energy or time? Which mode of transport? Foot, bicycle, car, …?

It’s a prototype for an electric car routing algorithm. So it’s supposed to to take advantage of the height to recharge the vehicle.
The routes will in any case take into consideration all the other OSRM penalties.

This may be useful for you:

Note that with OSRM you will need to set properties.force_split_edges=true for elevation penalties to function correctly: Add split_edges flag by oxidase · Pull Request #3874 · Project-OSRM/osrm-backend · GitHub