Skip to content

Commit 79324b2

Browse files
committed
no more using of torch while exact solving with Google OR
1 parent 5b20e82 commit 79324b2

11 files changed

+34
-33
lines changed

common.py

-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import pickle
22
import os
33
import resource
4-
import torch
54
from typing import Union, Any, Dict
6-
from model.instance import Instance
7-
from torch import Tensor
85

96
# =====================================================================
107
# =*= COMMON TOOLS (objects and functions) USED ACCROSS THE PROJECT =*=
@@ -27,13 +24,6 @@ def __init__(self, data: str, instances: str, models: str, out: str, scripts: st
2724
all_types_feature = Union[int, float, bool, list]
2825
generic_object = Union[object, Dict[Any, Any]]
2926

30-
def add_into_tensor(tensor_list: Tensor | None, tensor_val: Tensor):
31-
if tensor_list is None:
32-
tensor_list = tensor_val
33-
else:
34-
tensor_list = torch.cat((tensor_list, tensor_val), dim=0)
35-
return tensor_list
36-
3727
def load_instance(path: str):
3828
with open(path, 'rb') as file:
3929
return pickle.load(file)
@@ -49,9 +39,6 @@ def load_instances(path: str):
4939
print("end of loading!")
5040
return instances
5141

52-
def to_binary(booleanVal: bool):
53-
return 1 if booleanVal else 0
54-
5542
def set_memory_limit(max_ram_bytes: num_feature):
5643
_, hard = resource.getrlimit(resource.RLIMIT_AS)
5744
resource.setrlimit(resource.RLIMIT_AS, (max_ram_bytes * 1024 * 1024 * 1024, hard))
@@ -77,15 +64,6 @@ def init_several_3D(a: int, b: int, c: int, default_value: all_types_feature, nb
7764
def to_bool(s: str):
7865
return s.lower() in ['true', '1', 't', 'y', 'yes']
7966

80-
def features2tensor(features: list, device: str):
81-
for f in features:
82-
if isinstance(f, bool):
83-
f = to_binary(f)
84-
return torch.tensor([[f for f in features]], dtype=torch.float, device=device)
85-
86-
def id2tensor(id1: int, id2: int, device: str):
87-
return torch.tensor([[id1], [id2]], dtype=torch.long, device=device)
88-
8967
def search_object_by_id(objects: list[generic_object], id: int):
9068
for obj in objects:
9169
if obj['id'] == id:

exact_solver.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import argparse
2-
import pickle
3-
import os
42
from common import init_several_1D, init_2D, init_several_2D, init_3D, load_instance, directory
53
from model.instance import Instance
64
from model.solution import Solution

jobs/exact_builder.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import argparse
32
import os
43

@@ -44,7 +43,7 @@
4443
f.write("virtualenv --no-download $SLURM_TMPDIR/env\n")
4544
f.write("source $SLURM_TMPDIR/env/bin/activate\n")
4645
f.write("pip install --upgrade pip --no-index\n")
47-
f.write("pip install --no-index -r "+BASIC_PATH+"requirements.txt\n")
46+
f.write("pip install --no-index -r "+BASIC_PATH+"requirements_or.txt\n")
4847
f.write(f"python {BASIC_PATH}exact_solver.py --mode=prod --size={size} --number={instance} --path="+BASIC_PATH+" \n")
4948
f.write("desactivate\n")
5049
f.close()

jobs/gns_builder.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import argparse
32

43
# =====================================================

model/agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Tuple
55
import numpy as np
66
from torch.nn import Module
7-
from common import add_into_tensor
7+
from tensors import add_into_tensor
88

99
# ===========================================================
1010
# =*= DATA MODEL FOR PPO AGENT CONFIGURATION AND RESULTS =*=

model/gnn.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from torch_geometric.data.storage import EdgeStorage
33
from torch.nn import Sequential, Linear, ELU, Tanh, Parameter, LeakyReLU, Module, ModuleList
44
import torch.nn.functional as F
5-
from torch_geometric.nn import global_mean_pool
65
from torch import Tensor
76
from .graph import FeatureConfiguration, State
87
from torch_geometric.utils import scatter

model/graph.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import copy
21
import torch
32
from torch_geometric.data import HeteroData
43
from torch_geometric.data.storage import EdgeStorage
54
from torch_geometric.utils import to_dense_adj
65
from torch import Tensor
7-
from common import features2tensor, id2tensor, num_feature
6+
from common import num_feature
7+
from tensors import features2tensor, id2tensor
88
from .instance import Instance
99

1010
# =============================================================

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ortools
21
numpy
32
pandas
43
seaborn

requirements_or.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ortools
2+
pandas

results_analysis.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from model.agent import MAPPO_Losses
88
from torch import Tensor
99
import matplotlib.pyplot as plt
10-
import numpy as np
1110

1211
# =====================================================
1312
# =*= EXPERIMENTS: RESULTS ANALYSIS =*=

tensors.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import torch
2+
from torch import Tensor
3+
4+
# =====================================================
5+
# =*= TENSOR-RELATED TOOLS USED ACCROSS THE PROJECT =*=
6+
# ======================================================
7+
__author__ = "Anas Neumann - [email protected]"
8+
__version__ = "1.0.0"
9+
__license__ = "Apache 2.0 License"
10+
11+
def add_into_tensor(tensor_list: Tensor | None, tensor_val: Tensor):
12+
if tensor_list is None:
13+
tensor_list = tensor_val
14+
else:
15+
tensor_list = torch.cat((tensor_list, tensor_val), dim=0)
16+
return tensor_list
17+
18+
def to_binary(booleanVal: bool):
19+
return 1 if booleanVal else 0
20+
21+
def features2tensor(features: list, device: str):
22+
for f in features:
23+
if isinstance(f, bool):
24+
f = to_binary(f)
25+
return torch.tensor([[f for f in features]], dtype=torch.float, device=device)
26+
27+
def id2tensor(id1: int, id2: int, device: str):
28+
return torch.tensor([[id1], [id2]], dtype=torch.long, device=device)

0 commit comments

Comments
 (0)