import numpy as np from collections import defaultdict import itertools 1. Verilerin…
- User
import numpy as np from collections import defaultdict import itertools
1. Verilerin Hazırlanması
Örnek olarak, veri yüklemeyi ve işlemeyi göstereceğim.
Gerçek verilerinizle bu kısımları güncellemeniz gerekecek.
varsayımsal veri yükleme fonksiyonu
def load_data(): # Veri yapısı: [(durum, eylem, sonraki_durum), ...] # Durumlar ve eylemler uygun şekilde kodlanmalıdır. data = [] # Burada verilerinizi yükleyip data listesine ekleyin return data
data = load_data()
2. MDP'nin Tanımlanması
Durum uzayı
indicator_signals = [-2, -1, 0, 1, 2] roc_values = [-1, 0, 1] states = list(itertools.product(indicator_signals, roc_values)) state_indices = {state: idx for idx, state in enumerate(states)}
Eylem uzayı
actions = [-1, 0, 1] action_indices = {action: idx for idx, action in enumerate(actions)}
3. Özellik Fonksiyonlarının Tanımlanması
Her durum için one-hot özellik vektörü kullanacağız
feature_matrix = np.eye(len(states))
4. Gözlemlenen Özellik Beklentilerinin Hesaplanması
def calculate_empirical_feature_expectations(data): feature_expectations = np.zeros(len(states)) for (state, action, next_state) in data: idx = state_indices[state] feature_expectations[idx] += 1 feature_expectations /= len(data) return feature_expectations
empirical_feature_expectations = calculate_empirical_feature_expectations(data)
5. MaxEnt IRL Algoritmasının Uygulanması
def softmax(x): e_x = np.exp(x - np.max(x)) # Sayısal kararlılık için return e_x / e_x.sum(axis=0)
def maxent_irl(feature_matrix, state_indices, data, empirical_feature_expectations, num_iterations=100, learning_rate=0.1):
code - Assistant