Skip to content

Commit

Permalink
Rearranged folders (#21)
Browse files Browse the repository at this point in the history
* fixed error in selection of time_sku features

* fixed error in force save agent

* fixed parameter name for dataset

* accelerated getitem method

* minor fix in predict function

* minor fix in predict function

* enables out-of-sample experiments

* print steps while testing

* fixed error in normalization for oos products

* fixed error in normalizing oos demand

* fixed error in normalization of oos SKUs

* fixed error in normalization of oos SKUs

* added option for run_experiment to return score

* fixed average calc tune!

* implemented xgb

* renaming

* renamed to ddopai

* updated requirements

* updated requirements

* moved some imports to extra cells

* rearranged
  • Loading branch information
majoma7 authored Sep 30, 2024
1 parent d98888e commit 83ca13b
Show file tree
Hide file tree
Showing 85 changed files with 1,573 additions and 3,111 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# ddopnew
# ddopai


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Install

``` sh
pip install ddopnew
pip install ddopai
```

## What is ddopnew?
## What is ddopai?

To be written.
25 changes: 14 additions & 11 deletions ddopnew/RL_approximators.py → ddopai/RL_approximators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
__all__ = ['RNNWrapper', 'BaseApproximator', 'BaseApproximatorMLP', 'RNNMLPHybrid', 'BaseApproximatorRNN', 'MLPStateAction',
'MLPState', 'MLPActor', 'RNNActor', 'RNNStateAction']

# %% ../nbs/60_approximators/21_critic_networks.ipynb 4
# %% ../nbs/60_approximators/21_critic_networks.ipynb 3
# import logging
# logging_level = logging.DEBUG

from abc import ABC, abstractmethod
from typing import Union, Tuple, List
import numpy as np
Expand All @@ -18,7 +21,7 @@

import time

# %% ../nbs/60_approximators/21_critic_networks.ipynb 5
# %% ../nbs/60_approximators/21_critic_networks.ipynb 4
class RNNWrapper(nn.Module):
def __init__(self, rnn_cell_class, *args, **kwargs):
"""
Expand Down Expand Up @@ -52,7 +55,7 @@ def __init__(self, *args, **kwargs):

return SpecificRNNWrapper

# %% ../nbs/60_approximators/21_critic_networks.ipynb 6
# %% ../nbs/60_approximators/21_critic_networks.ipynb 5
class BaseApproximator():

""" Some basic functions for approximators """
Expand Down Expand Up @@ -142,7 +145,7 @@ def forward(self, x):
""" Forward pass through the network - overwrite this if necessary """
return self.model(x)

# %% ../nbs/60_approximators/21_critic_networks.ipynb 7
# %% ../nbs/60_approximators/21_critic_networks.ipynb 6
class BaseApproximatorMLP(BaseApproximator, nn.Module):

""" Some basic functions for approximators """
Expand Down Expand Up @@ -195,7 +198,7 @@ def build_MLP( self,

self.model = model

# %% ../nbs/60_approximators/21_critic_networks.ipynb 8
# %% ../nbs/60_approximators/21_critic_networks.ipynb 7
class RNNMLPHybrid(nn.Module, BaseApproximator):

""" A hybrid model combining an RNN and an MLP """
Expand Down Expand Up @@ -294,7 +297,7 @@ def forward(self, x_rnn, x_mlp=None):

return x

# %% ../nbs/60_approximators/21_critic_networks.ipynb 9
# %% ../nbs/60_approximators/21_critic_networks.ipynb 8
class BaseApproximatorRNN(BaseApproximator, nn.Module):

""" Some basic functions for approximators """
Expand Down Expand Up @@ -351,7 +354,7 @@ def build_RNN( self,



# %% ../nbs/60_approximators/21_critic_networks.ipynb 10
# %% ../nbs/60_approximators/21_critic_networks.ipynb 9
class MLPStateAction(BaseApproximatorMLP):

"""Multilayer perceptron model for critic networks that take
Expand Down Expand Up @@ -396,7 +399,7 @@ def forward(self, state, action):
# return q
return torch.squeeze(q)

# %% ../nbs/60_approximators/21_critic_networks.ipynb 11
# %% ../nbs/60_approximators/21_critic_networks.ipynb 10
class MLPState(BaseApproximatorMLP):

"""Multilayer perceptron model for critic networks that take
Expand Down Expand Up @@ -436,7 +439,7 @@ def forward(self, state):
# return q.squeeze()
return q

# %% ../nbs/60_approximators/21_critic_networks.ipynb 12
# %% ../nbs/60_approximators/21_critic_networks.ipynb 11
class MLPActor(BaseApproximatorMLP):

"""Multilayer perceptron model for critic networks that take
Expand Down Expand Up @@ -475,7 +478,7 @@ def forward(self, state):

return a

# %% ../nbs/60_approximators/21_critic_networks.ipynb 13
# %% ../nbs/60_approximators/21_critic_networks.ipynb 12
class RNNActor(BaseApproximatorRNN):

"""Multilayer perceptron model for critic networks that take
Expand Down Expand Up @@ -577,7 +580,7 @@ def forward_without_batch(self, state):

return self.model(rnn_input, mlp_input)

# %% ../nbs/60_approximators/21_critic_networks.ipynb 14
# %% ../nbs/60_approximators/21_critic_networks.ipynb 13
class RNNStateAction(BaseApproximatorRNN):

"""Multilayer perceptron model for critic networks that take
Expand Down
File renamed without changes.
840 changes: 840 additions & 0 deletions ddopai/_modidx.py

Large diffs are not rendered by default.

File renamed without changes.
7 changes: 5 additions & 2 deletions ddopnew/agents/base.py → ddopai/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# %% auto 0
__all__ = ['BaseAgent']

# %% ../../nbs/40_base_agents/10_base_agents.ipynb 4
# %% ../../nbs/40_base_agents/10_base_agents.ipynb 3
# import logging
# logging_level = logging.DEBUG

from abc import ABC, abstractmethod
from typing import Union, Optional, List, Tuple
import numpy as np
Expand All @@ -18,7 +21,7 @@
# from sklearn.utils.validation import check_array
# import numbers

# %% ../../nbs/40_base_agents/10_base_agents.ipynb 5
# %% ../../nbs/40_base_agents/10_base_agents.ipynb 4
class BaseAgent():

"""
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions ddopai/agents/class_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Dict of agent classes and (standard) agent names to allow for dynamic loading of agents."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/40_base_agents/10_AGENT_CLASSES.ipynb.

# %% auto 0
__all__ = ['AGENT_CLASSES']

# %% ../../nbs/40_base_agents/10_AGENT_CLASSES.ipynb 3
AGENT_CLASSES = {
"RandomAgent": "ddopai.agents.saa.SAA",

"SAA": "ddopai.agents.newsvendor.saa.NewsvendorSAAagent",
"wSAA": "ddopai.agents.newsvendor.saa.NewsvendorRFwSAAagent",
"RFwSAA": "ddopai.agents.newsvendor.saa.NewsvendorRFwSAAagent",

"XGB": "ddopai.agents.newsvendor.erm.NewsvendorXGBAgent",

"lERM": "ddopai.agents.newsvendor.erm.NewsvendorlERMAgent",
"DLNV": "ddopai.agents.newsvendor.erm.NewsvendorDLAgent",
"DLNVRNN": "ddopai.agents.newsvendor.erm.NewsvendorDLRNNAgent",
"DLNVTransformer": "ddopai.agents.newsvendor.erm.NewsvendorDLTransformerAgent",

"lERMMeta": "ddopai.agents.newsvendor.erm.NewsvendorlERMMetaAgent",
"DLNVMeta": "ddopai.agents.newsvendor.erm.NewsvendorDLMetaAgent",
"DLNVRNNMeta": "ddopai.agents.newsvendor.erm.NewsvendorDLRNNMetaAgent",
"DLNVTransformerMeta": "ddopai.agents.newsvendor.erm.NewsvendorDLTransformerMetaAgent",

"SAC": "ddopai.agents.rl.sac.SACAgent",
"SACRNN": "ddopai.agents.rl.sac.SACRNNAgent",

"TD3": "ddopai.agents.rl.td3.TD3Agent",
"PPO": "ddopai.agents.rl.ppo.PPOAgent",
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def set_model(self, input_shape, output_shape):

"""Set the model for the agent to a linear model"""

from ddopnew.approximators import LinearModel
from ddopai.approximators import LinearModel

# flatten time dim of input
print("input shape", input_shape)
Expand All @@ -650,7 +650,7 @@ def set_model(self, input_shape, output_shape):

self.model = LinearModel(input_size=input_size, output_size=output_size, **self.model_params)

# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 30
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 31
class NewsvendorDLAgent(NVBaseAgent):

"""
Expand Down Expand Up @@ -719,10 +719,10 @@ def set_model(self, input_shape, output_shape):
input_size = np.prod(input_shape)
output_size = output_shape[0]

from ddopnew.approximators import MLP
from ddopai.approximators import MLP
self.model = MLP(input_size=input_size, output_size=output_size, **self.model_params)

# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 36
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 37
class BaseMetaAgent():

def set_meta_dataloader(
Expand All @@ -738,7 +738,7 @@ def set_meta_dataloader(

self.dataloader = torch.utils.data.DataLoader(dataset, **dataloader_params)

# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 37
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 38
class NewsvendorlERMMetaAgent(NewsvendorlERMAgent, BaseMetaAgent):

"""
Expand Down Expand Up @@ -793,7 +793,7 @@ def __init__(self,
loss_function=loss_function,
)

# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 38
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 39
class NewsvendorDLMetaAgent(NewsvendorDLAgent, BaseMetaAgent):

"""
Expand Down Expand Up @@ -852,7 +852,7 @@ def __init__(self,
)


# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 39
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 40
class NewsvendorDLTransformerAgent(NVBaseAgent):

"""
Expand Down Expand Up @@ -929,10 +929,10 @@ def set_model(self, input_shape, output_shape):

output_size = output_shape[0]

from ddopnew.approximators import Transformer
from ddopai.approximators import Transformer
self.model = Transformer(input_size=input_shape, output_size=output_size, **self.model_params)

# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 40
# %% ../../../nbs/41_NV_agents/11_NV_erm_agents.ipynb 41
class NewsvendorDLTransformerMetaAgent(NewsvendorDLTransformerAgent, BaseMetaAgent):

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from sklearn.ensemble import RandomForestRegressor
from sklearn.utils.validation import check_array


# %% ../../../nbs/41_NV_agents/10_NV_saa_agents.ipynb 4
class BaseSAAagent(BaseAgent):

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion ddopnew/agents/rl/sac.py → ddopai/agents/rl/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def __init__(self,
network_critic_params=network_critic_params,
)

# %% ../../../nbs/51_RL_agents/10_SAC_agents.ipynb 8
# %% ../../../nbs/51_RL_agents/10_SAC_agents.ipynb 9
class SACRNNAgent(SACBaseAgent):

"""
Expand Down
File renamed without changes.
23 changes: 13 additions & 10 deletions ddopnew/approximators.py → ddopai/approximators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
__all__ = ['BaseModule', 'LinearModel', 'MLP', 'Transformer', 'LlamaRotaryEmbedding', 'rotate_half', 'apply_rotary_pos_emb',
'CausalSelfAttention', 'find_multiple', 'MLP_block', 'RMSNorm', 'Block']

# %% ../nbs/60_approximators/11_approximators.ipynb 4
# %% ../nbs/60_approximators/11_approximators.ipynb 3
# import logging
# logging_level = logging.DEBUG

from abc import ABC, abstractmethod
from typing import Union, Dict, Literal
import numpy as np
Expand All @@ -18,7 +21,7 @@

import time

# %% ../nbs/60_approximators/11_approximators.ipynb 5
# %% ../nbs/60_approximators/11_approximators.ipynb 4
# TODO: Merge with base from RL networks to avoid duplicaiotn of code

class BaseModule(nn.Module):
Expand All @@ -44,7 +47,7 @@ def select_activation(activation):
else:
raise ValueError(f"Activation function {activation} not recognized")

# %% ../nbs/60_approximators/11_approximators.ipynb 6
# %% ../nbs/60_approximators/11_approximators.ipynb 5
class LinearModel(BaseModule):
"""Linear regression model"""

Expand All @@ -64,7 +67,7 @@ def forward(self,x):
out=self.final_activation(out)
return out

# %% ../nbs/60_approximators/11_approximators.ipynb 7
# %% ../nbs/60_approximators/11_approximators.ipynb 6
class MLP(BaseModule):

""" Multilayer perceptron model """
Expand Down Expand Up @@ -103,7 +106,7 @@ def __init__(self,
def forward(self, x):
return self.model(x)

# %% ../nbs/60_approximators/11_approximators.ipynb 8
# %% ../nbs/60_approximators/11_approximators.ipynb 7
class Transformer(BaseModule):

""" Multilayer perceptron model """
Expand Down Expand Up @@ -168,7 +171,7 @@ def forward( self,

return output

# %% ../nbs/60_approximators/11_approximators.ipynb 9
# %% ../nbs/60_approximators/11_approximators.ipynb 8
class LlamaRotaryEmbedding(torch.nn.Module):

"""
Expand Down Expand Up @@ -234,7 +237,7 @@ def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
k_embed = (k * cos) + (rotate_half(k) * sin)
return q_embed, k_embed

# %% ../nbs/60_approximators/11_approximators.ipynb 10
# %% ../nbs/60_approximators/11_approximators.ipynb 9
class CausalSelfAttention(nn.Module):

""" Causeal self-attention module
Expand Down Expand Up @@ -331,7 +334,7 @@ def find_multiple(n: int, k: int) -> int:
return n
return n + k - (n % k)

# %% ../nbs/60_approximators/11_approximators.ipynb 11
# %% ../nbs/60_approximators/11_approximators.ipynb 10
class MLP_block(nn.Module):
def __init__(self, n_embd_per_head, n_head, min_multiple = 256, gating = True) -> None:
super().__init__()
Expand Down Expand Up @@ -361,7 +364,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.c_proj(x)
return x

# %% ../nbs/60_approximators/11_approximators.ipynb 12
# %% ../nbs/60_approximators/11_approximators.ipynb 11
class RMSNorm(nn.Module):
"""Root Mean Square Layer Normalization as implemented in https://github.com/time-series-foundation-models/lag-llama.
Expand All @@ -381,7 +384,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
output = (self.scale * x_normed).type_as(x)
return output

# %% ../nbs/60_approximators/11_approximators.ipynb 13
# %% ../nbs/60_approximators/11_approximators.ipynb 12
class Block(nn.Module):
def __init__(self, n_embd_per_head, n_head, block_size, dropout, min_multiple = 256, gating=True) -> None:
super().__init__()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# %% ../../nbs/80_datasets/default_datasets.ipynb 6
def get_all_release_tags(token=None):
url = "https://api.github.com/repos/opimwue/ddopnew/releases"
url = "https://api.github.com/repos/opimwue/ddopai/releases"
headers = {'Authorization': f'Bearer {token}'} if token else {}
response = requests.get(url, headers=headers)

Expand All @@ -43,7 +43,7 @@ def get_release_tag(dataset_type, version, token=None):
return release_tag

def get_dataset_url(dataset_type, dataset_number, release_tag, token=None):
api_url = f"https://api.github.com/repos/opimwue/ddopnew/releases/tags/{release_tag}"
api_url = f"https://api.github.com/repos/opimwue/ddopai/releases/tags/{release_tag}"
headers = {'Authorization': f'Bearer {token}'} if token else {}
response = requests.get(api_url, headers=headers)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

# %% ../../../nbs/21_envs_inventory/00_inventory_utils.ipynb 4
class OrderPipeline():

"""
Class to handle the order pipeline in the inventory environments. It is used to keep track of the orders
that are placed. It can account for fixed and variable lead times.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def update_cu_co(self, cu=None, co=None):
self.set_param("sl", sl, shape=(self.num_SKUs[0],))


# %% ../../../nbs/21_envs_inventory/20_single_period_envs.ipynb 14
# %% ../../../nbs/21_envs_inventory/20_single_period_envs.ipynb 15
class NewsvendorEnvVariableSL(NewsvendorEnv, ABC):
def __init__(self,

Expand Down Expand Up @@ -301,7 +301,7 @@ def get_observation(self):

# print("env mode:", self.mode)
# print("dataloader mode:", self.dataloader.dataset_type)

X_item, Y_item = self.dataloader[self.index]

# check if any value in X_item is nan.
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 83ca13b

Please sign in to comment.