If you know the “cost” key is not nil you can do if cost < best_so_far["cost"].not_nil!.
Depends on what you are trying to do, Hash might not be the best type to use here. Maybe a class with explicitly named properties might be better, something like:
class BestSoFar
property dems : NoIdeaWhatTypeYouWant? = nil
property solver : NoIdeaWhatTypeYouWant? = nil
property cost = 9999999.9
end