# Lighttools API透過RL找尋最佳參數
## Lighttools模型與API
### 需先型設計出所需模型,了解如何使用API抓取設定參數
此例為Lighttools抓取及調整參數的函式
```
getting_data = lt.DbGet(full_key, "VALUE")
setting_data = lt.DbSet("COMPONENTS[Components].Z", "VALUE",value)
```

## RL模型(DDPG)
### 1.根據所需,選出最佳化模型(RL)中,欲調整的參數及獎勵
以此Lighttools專案為例
第一塊鏡片及第二塊鏡片的Z軸,分別做為調整對象
而調整後的入射效率即為Reward
```
模擬成功完成!檢查參數值: cube_Z=(3148.3, 0),Z_len1=(1249.2377724310904, 0),Z_len0=(352.8084822513212, 0)
Coupling Efficiency (%): 66.83872101881248, Absolute Efficiency (%): 53.75448626865992
```
### 2.此專案中使用DDPG模型
#### 模型挑選可根據專案類型進行挑選
Deep Deterministic Policy Gradient (DDPG)算法主要用于解決練續控制問題的線性(on-line)深度強化學習
在決定好模型即將進入訓練過程時
其中需決定迭代次數也就是學習次數(episode)
通常,一開始會隨機蒐集不同reward所對應資料,隨著epsilon下降後,隨機比例會減少,而使用Agent的比例會拉高
```
for episode in range(100):
epsilon = max(0.1, 1.0 - episode / 100) # 減少探索的機會
if epsilon > 0.1 :
brain_lens_middle_z = np.random.uniform(1245, 1255)
brain_lens_front_z = np.random.uniform(345, 355)
else:
# 根據 actor 策略選擇
print(state)
state_tensor = torch.FloatTensor([float(x) for x in state]) # 預設初始化 state
print(state_tensor)
brain_lens_middle_z = agent.actor(state_tensor)[0].item()
brain_lens_front_z = agent.actor(state_tensor)[1].item()
print("I'm using brain")
```
在程式碼完成後,通常訓練過程有部分參數可做調整
ex. 1. episode : 相當於訓練次數