import act4e_interfaces as I class SolCurrencyOptimization(I.CurrencyOptimization): def compute_optimal_conversion( self, available: I.SemiCategory[I.RichObject[str], I.RichMorphism[I.CurrencyExchanger]], source: str, amount: float, target: str, ) -> I.OptimalSolution: source, target = available.get_object(source), available.get_object(target) results = {} for l in range(available.objects().size() + 1): paths = available.hom(source, target, l) for p in paths.elements(): # Allow to go through exchanges only once names = p.label.split(";") if any([names.count(n) > 1 for n in names]): continue rate, commission = p.mordata.rate, p.mordata.commission res = amount * rate - commission results[res] = p final = max(results.keys()) path = results[final].label.split(";") return I.OptimalSolution(path, final)