I may be dumb, but I'm not sure I understand exactly what you want so forgive me if that's totally off.
You have ports with freight inside, and freighters which can carry the freight.
For each good, you can compute the best cost you will get for it, by looking at the cost in every port, the difference of costs and then subtracting the distance cost. Since both cost of moving and upkeep cost are linear on distance/time, they are a well-defined unchanging feature of the path used.
Once you've computed that, you have a set of paths, each with a cost/revenue. A freighter would look at the nearest port, look at the best route from that port and choose it.
Better, a freighter would look for the best route in the world, look where the starting port is, then decide to pick the best route from where it is to the starting port of the best route.