Код: Выделить всё
# Memoization dictionary to store results of subproblems
memo = {}
# Recursive function to maximize trades
def maximize_trades_recursive(t, n, M, alpha, pi, T):
# Base case: At the last period, we must trade all remaining shares
if t == T - 1:
return math.ceil((1 - alpha * M**pi) * n)
# If the result for this state is already computed, return it
if (t, n, M) in memo:
return memo[(t, n, M)]
# Recursive case: Try trading different amounts of shares and maximize the result
max_traded_shares = 0
optimal_n_t = 0
# Iterate over all possible amounts of shares to trade at this period (0
Подробнее здесь: [url]https://stackoverflow.com/questions/79019902/recursive-method-to-optimize-trading-schedule[/url]