-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
148 lines (133 loc) · 4.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gymnasium as gym
import tianshou as ts
import clusterenv.envs
import torch
import numpy as np
from torch import nn
import logging
# logging.basicConfig(
# level=logging.DEBUG,
# format="%(asctime)s [%(levelname)s] %(filename)s: %(message)s",
# datefmt="%Y-%m-%d %H:%M:%S",
# )
# logging.getLogger("tianshou").setLevel(logging.WARNING)
# logging.getLogger("numba").setLevel(logging.WARNING)
class Net(nn.Module):
def __init__(self, machine_shape, job_shape, action_shape):
super().__init__()
# Number of channels from the resource dimension
machine_channels = machine_shape[1]
job_channels = job_shape[1]
# Sub-networks for machines and jobs
self.machine_net = nn.Sequential(
nn.Conv2d(
machine_channels,
32,
kernel_size=(
3,
3),
stride=1,
padding=1),
nn.ReLU(
inplace=True),
nn.Flatten(),
nn.Linear(
32 *
machine_shape[0] *
machine_shape[2],
128),
nn.ReLU(
inplace=True),
)
self.job_net = nn.Sequential(
nn.Conv2d(
job_channels,
32,
kernel_size=(
3,
3),
stride=1,
padding=1),
nn.ReLU(
inplace=True),
nn.Flatten(),
nn.Linear(
32 *
job_shape[0] *
job_shape[2],
128),
nn.ReLU(
inplace=True),
)
# Combine both outputs
self.combined_net = nn.Sequential(
nn.Linear(128 + 128, 256),
nn.ReLU(inplace=True),
nn.Linear(256, action_shape),
)
def forward(self, obs, state=None, info={}):
# Extract machines and jobs from the observation dictionary
if not isinstance(obs["machines"], torch.Tensor):
machines = torch.tensor(obs["machines"], dtype=torch.float)
else:
machines = obs["machines"]
if not isinstance(obs["jobs"], torch.Tensor):
jobs = torch.tensor(obs["jobs"], dtype=torch.float)
else:
jobs = obs["jobs"]
# Ensure proper shape for the convolutional layers
# (batch, channels, height, width)
machines = machines.permute(0, 2, 1, 3)
jobs = jobs.permute(0, 2, 1, 3) # (batch, channels, height, width)
# Process machines and jobs separately
machine_features = self.machine_net(machines)
job_features = self.job_net(jobs)
# Combine features and pass to final layer
combined_features = torch.cat([machine_features, job_features], dim=-1)
logits = self.combined_net(combined_features)
return logits, state
def main() -> None:
env_id = "Cluster-discrete-v0"
train_envs = ts.env.DummyVectorEnv(
[lambda: gym.make(env_id) for _ in range(10)])
test_envs = ts.env.DummyVectorEnv(
[lambda: gym.make(env_id) for _ in range(100)])
state_shape = train_envs.observation_space[0]
action_shape = train_envs.action_space[0]
net = Net(
state_shape["machines"].shape,
state_shape["jobs"].shape,
action_shape.n)
optim = torch.optim.Adam(net.parameters(), lr=1e-3)
policy = ts.policy.DQNPolicy(
model=net,
optim=optim,
action_space=action_shape,
discount_factor=0.9,
estimation_step=3,
target_update_freq=320,
)
train_collector = ts.data.Collector(
policy,
train_envs,
ts.data.VectorReplayBuffer(2_000, 200),
exploration_noise=True,
)
test_collector = ts.data.Collector(
policy, test_envs, exploration_noise=True)
result = ts.trainer.OffpolicyTrainer(
policy=policy,
train_collector=train_collector,
test_collector=test_collector,
max_epoch=10,
step_per_epoch=10_000,
step_per_collect=10,
update_per_step=0.1,
episode_per_test=100,
batch_size=64,
train_fn=lambda epoch, env_step: policy.set_eps(0.1),
test_fn=lambda epoch, env_step: policy.set_eps(0.05),
).run()
print(f'Finished training! Use {result["duration"]}')
if __name__ == "__main__":
main()