Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] add GFACS and replace PyVRP with HGS for CVRP local search #236

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions configs/experiment/routing/deepaco.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,51 @@ logger:
name: deepaco-${env.name}${env.generator_params.num_loc}

model:
batch_size: 512
val_batch_size: 1000
test_batch_size: 1000
train_data_size: 128_000
val_data_size: 10_000
test_data_size: 10_000
batch_size: 20
val_batch_size: 20
test_batch_size: 20
train_data_size: 400
val_data_size: 20
test_data_size: 100
optimizer: "AdamW"
optimizer_kwargs:
lr: 1e-4
lr: 1e-3
weight_decay: 0
lr_scheduler:
"MultiStepLR"
lr_scheduler_kwargs:
milestones: [25, 35]
milestones: [15, 35]
gamma: 0.1

train_with_local_search: True
ls_reward_aug_W: 0.99

policy_kwargs:
n_ants:
train: 50
val: 20
test: 20
train: 30
val: 30
test: 100
n_iterations:
train: 1 # unused value
val: 20
test: 20
val: 5
test: 10
temperature: 1.0
top_p: 0.0
top_k: 0
aco_kwargs:
alpha: 1.0
beta: 1.0
decay: 0.95

use_local_search: True
use_nls: True
n_perturbations: 5
local_search_params:
max_iterations: 1000
perturbation_params:
max_iterations: 20
k_sparse: 5 # this should be adjusted based on the `num_loc` value

trainer:
max_epochs: 40
max_epochs: 50

seed: 1234
seed: 1234
73 changes: 73 additions & 0 deletions configs/experiment/routing/gfacs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# @package _global_

defaults:
- override /model: gfacs.yaml
- override /env: tsp.yaml
- override /callbacks: default.yaml
- override /trainer: default.yaml
- override /logger: wandb.yaml

env:
generator_params:
num_loc: 50

logger:
wandb:
project: "rl4co"
tags: ["gfacs", "${env.name}"]
group: ${env.name}${env.generator_params.num_loc}
name: gfacs-${env.name}${env.generator_params.num_loc}

model:
batch_size: 20
val_batch_size: 20
test_batch_size: 20
train_data_size: 400
val_data_size: 20
test_data_size: 100
optimizer: "AdamW"
optimizer_kwargs:
lr: 1e-3
weight_decay: 0
lr_scheduler:
"MultiStepLR"
lr_scheduler_kwargs:
milestones: [15, 35]
gamma: 0.1

train_with_local_search: True
ls_reward_aug_W: 0.99

policy_kwargs:
n_ants:
train: 30
val: 30
test: 100
n_iterations:
train: 1 # unused value
val: 5
test: 10
temperature: 1.0
top_p: 0.0
top_k: 0
aco_kwargs:
alpha: 1.0
beta: 1.0
decay: 0.95
use_local_search: True
use_nls: True
n_perturbations: 5
local_search_params:
max_iterations: 1000
perturbation_params:
max_iterations: 20
k_sparse: 5 # this should be adjusted based on the `num_loc` value

beta_min: 100
beta_max: 500
beta_flat_epochs: 5

trainer:
max_epochs: 50

seed: 1234
2 changes: 1 addition & 1 deletion configs/model/deepaco.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
_target_: rl4co.models.DeepACO

baseline: "exponential"
baseline: "no"
3 changes: 3 additions & 0 deletions configs/model/gfacs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_target_: rl4co.models.GFACS

baseline: "no"
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dev = [
"pytest",
"pytest-cov",
]

graph = ["torch_geometric"]
routing = [
"numba>=0.58.1",
Expand Down
55 changes: 55 additions & 0 deletions rl4co/envs/routing/cvrp/HGS-CVRP/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

# Test build files
Test/CMakeFiles/
Test/CMakeCache.txt

# build files
build/

# IDE files
.vscode
.idea
cmake-build-*
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
[Xx]64/
[Xx]86/
*.user
.vs/


# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

41 changes: 41 additions & 0 deletions rl4co/envs/routing/cvrp/HGS-CVRP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.15)
project(HGS_CVRP)
set(CMAKE_CXX_STANDARD 17)

set(
src_files
Program/Genetic.cpp
Program/Individual.cpp
Program/LocalSearch.cpp
Program/Params.cpp
Program/Population.cpp
Program/Split.cpp
Program/InstanceCVRPLIB.cpp
Program/AlgorithmParameters.cpp
Program/C_Interface.cpp)

if (MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif (MSVC)

include_directories(Program)

# Build Executable
add_executable(bin
Program/main.cpp
${src_files})

set_target_properties(bin PROPERTIES OUTPUT_NAME hgs)

# Build Library
add_library(lib SHARED ${src_files})
set_target_properties(lib PROPERTIES OUTPUT_NAME hgscvrp)


# Install
install(TARGETS lib
DESTINATION lib)
install(TARGETS bin
DESTINATION bin)
install(FILES Program/AlgorithmParameters.h Program/C_Interface.h
DESTINATION include)
21 changes: 21 additions & 0 deletions rl4co/envs/routing/cvrp/HGS-CVRP/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Thibaut Vidal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions rl4co/envs/routing/cvrp/HGS-CVRP/Program/AlgorithmParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by chkwon on 3/23/22.
//

#include "AlgorithmParameters.h"
#include <iostream>

extern "C"
struct AlgorithmParameters default_algorithm_parameters() {
struct AlgorithmParameters ap{};

ap.nbGranular = 20;
ap.mu = 25;
ap.lambda = 40;
ap.nbElite = 4;
ap.nbClose = 5;

ap.nbIterPenaltyManagement = 100;
ap.targetFeasible = 0.2;
ap.penaltyDecrease = 0.85;
ap.penaltyIncrease = 1.2;

ap.seed = 0;
ap.nbIter = 20000;
ap.nbIterTraces = 500;
ap.timeLimit = 0;
ap.useSwapStar = 1;

return ap;
}

void print_algorithm_parameters(const AlgorithmParameters & ap)
{
std::cout << "=========== Algorithm Parameters =================" << std::endl;
std::cout << "---- nbGranular is set to " << ap.nbGranular << std::endl;
std::cout << "---- mu is set to " << ap.mu << std::endl;
std::cout << "---- lambda is set to " << ap.lambda << std::endl;
std::cout << "---- nbElite is set to " << ap.nbElite << std::endl;
std::cout << "---- nbClose is set to " << ap.nbClose << std::endl;
std::cout << "---- nbIterPenaltyManagement is set to " << ap.nbIterPenaltyManagement << std::endl;
std::cout << "---- targetFeasible is set to " << ap.targetFeasible << std::endl;
std::cout << "---- penaltyDecrease is set to " << ap.penaltyDecrease << std::endl;
std::cout << "---- penaltyIncrease is set to " << ap.penaltyIncrease << std::endl;
std::cout << "---- seed is set to " << ap.seed << std::endl;
std::cout << "---- nbIter is set to " << ap.nbIter << std::endl;
std::cout << "---- nbIterTraces is set to " << ap.nbIterTraces << std::endl;
std::cout << "---- timeLimit is set to " << ap.timeLimit << std::endl;
std::cout << "---- useSwapStar is set to " << ap.useSwapStar << std::endl;
std::cout << "==================================================" << std::endl;
}
38 changes: 38 additions & 0 deletions rl4co/envs/routing/cvrp/HGS-CVRP/Program/AlgorithmParameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by chkwon on 3/23/22.
//

// This header file must be readable in C.

#ifndef ALGORITHMPARAMETERS_H
#define ALGORITHMPARAMETERS_H

struct AlgorithmParameters {
int nbGranular; // Granular search parameter, limits the number of moves in the RI local search
int mu; // Minimum population size
int lambda; // Number of solutions created before reaching the maximum population size (i.e., generation size)
int nbElite; // Number of elite individuals
int nbClose; // Number of closest solutions/individuals considered when calculating diversity contribution

int nbIterPenaltyManagement; // Number of iterations between penalty updates
double targetFeasible; // Reference proportion for the number of feasible individuals, used for the adaptation of the penalty parameters
double penaltyDecrease; // Multiplier used to decrease penalty parameters if there are sufficient feasible individuals
double penaltyIncrease; // Multiplier used to increase penalty parameters if there are insufficient feasible individuals

int seed; // Random seed. Default value: 0
int nbIter; // Nb iterations without improvement until termination (or restart if a time limit is specified). Default value: 20,000 iterations
int nbIterTraces; // Number of iterations between traces display during HGS execution
double timeLimit; // CPU time limit until termination in seconds. Default value: 0 (i.e., inactive)
int useSwapStar; // Use SWAP* local search or not. Default value: 1. Only available when coordinates are provided.
};

#ifdef __cplusplus
extern "C"
#endif
struct AlgorithmParameters default_algorithm_parameters();

#ifdef __cplusplus
void print_algorithm_parameters(const AlgorithmParameters & ap);
#endif

#endif //ALGORITHMPARAMETERS_H
Loading
Loading