forked from udeshmg/GTA-RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactoring of code, added VRP model, added Pointer Network
- Loading branch information
1 parent
4e4ca00
commit 1935bea
Showing
48 changed files
with
2,057 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import pickle | ||
|
||
|
||
def check_extension(filename): | ||
if os.path.splitext(filename)[1] != ".pkl": | ||
return filename + ".pkl" | ||
return filename | ||
|
||
|
||
def save_dataset(dataset, filename): | ||
|
||
filedir = os.path.split(filename)[0] | ||
|
||
if not os.path.isdir(filedir): | ||
os.makedirs(filedir) | ||
|
||
with open(check_extension(filename), 'wb') as f: | ||
pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL) | ||
|
||
|
||
def load_dataset(filename): | ||
|
||
with open(check_extension(filename), 'rb') as f: | ||
return pickle.load(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import argparse | ||
import os | ||
import numpy as np | ||
from data_utils import check_extension, save_dataset | ||
|
||
|
||
def generate_tsp_data(dataset_size, tsp_size): | ||
return np.random.uniform(size=(dataset_size, tsp_size, 2)).tolist() | ||
|
||
|
||
def generate_vrp_data(dataset_size, vrp_size): | ||
CAPACITIES = { | ||
10: 20., | ||
20: 30., | ||
50: 40., | ||
100: 50. | ||
} | ||
return list(zip( | ||
np.random.uniform(size=(dataset_size, 2)).tolist(), # Depot location | ||
np.random.uniform(size=(dataset_size, vrp_size, 2)).tolist(), # Node locations | ||
np.random.randint(1, 10, size=(dataset_size, vrp_size)), # Demand, uniform integer 1 ... 9 | ||
np.ones(dataset_size) * CAPACITIES[vrp_size] # Capacity, same for whole dataset | ||
)) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("dataset", help="Filename of the dataset to create") | ||
parser.add_argument("--problem", type=str, default='tsp', help="Problem, 'tsp' or 'vrp'") | ||
parser.add_argument("--dataset_size", type=int, default=10000, help="Size of the dataset") | ||
parser.add_argument('--graph_size', type=int, default=20, help="Size of problem instances") | ||
parser.add_argument("-f", action='store_true', help="Set true to overwrite") | ||
parser.add_argument('--seed', type=int, default=None, help="Random seed") | ||
|
||
opts = parser.parse_args() | ||
|
||
assert opts.f or not os.path.isfile(check_extension(opts.dataset)), \ | ||
"File already exists! Try running with -f option to overwrite." | ||
|
||
np.random.seed(opts.seed) | ||
if opts.problem == 'tsp': | ||
dataset = generate_tsp_data(opts.dataset_size, opts.graph_size) | ||
elif opts.problem == 'vrp': | ||
dataset = generate_vrp_data(opts.dataset_size, opts.graph_size) | ||
else: | ||
assert False, "Unknown problem: {}".format(opts.problem) | ||
|
||
print(dataset[0]) | ||
filename = check_extension(opts.dataset) | ||
|
||
save_dataset(dataset, opts.dataset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
from tensorboard_logger import log_value | ||
|
||
|
||
def log_values(cost, grad_norms, epoch, batch_id, step, | ||
log_likelihood, reinforce_loss, bl_loss, opts): | ||
log_likelihood, reinforce_loss, bl_loss, tb_logger, opts): | ||
avg_cost = cost.mean().data[0] | ||
grad_norms, grad_norms_clipped = grad_norms | ||
|
||
# Log values to screen | ||
print('epoch: {}, train_batch_id: {}, avg_cost: {}'.format(epoch, batch_id, avg_cost)) | ||
|
||
print('grad_norm: {}, clipped: {}'.format(grad_norms[0], grad_norms_clipped[0])) | ||
if opts.baseline == 'critic': | ||
print('grad_norm_critic: {}, clipped: {}'.format(grad_norms[1], grad_norms_clipped[1])) | ||
|
||
# Log values to tensorboard | ||
if not opts.no_tensorboard: | ||
log_value('avg_cost', avg_cost, step) | ||
tb_logger.log_value('avg_cost', avg_cost, step) | ||
|
||
log_value('actor_loss', reinforce_loss.data[0], step) | ||
log_value('nll', -log_likelihood.mean().data[0], step) | ||
tb_logger.log_value('actor_loss', reinforce_loss.data[0], step) | ||
tb_logger.log_value('nll', -log_likelihood.mean().data[0], step) | ||
|
||
log_value('grad_norm', grad_norms[0], step) | ||
log_value('grad_norm_clipped', grad_norms_clipped[0], step) | ||
tb_logger.log_value('grad_norm', grad_norms[0], step) | ||
tb_logger.log_value('grad_norm_clipped', grad_norms_clipped[0], step) | ||
|
||
if opts.baseline == 'critic': | ||
log_value('critic_loss', bl_loss.data[0], step) | ||
log_value('critic_grad_norm', grad_norms[1], step) | ||
log_value('critic_grad_norm_clipped', grad_norms_clipped[1], step) | ||
tb_logger.log_value('critic_loss', bl_loss.data[0], step) | ||
tb_logger.log_value('critic_grad_norm', grad_norms[1], step) | ||
tb_logger.log_value('critic_grad_norm_clipped', grad_norms_clipped[1], step) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.