Skip to content

Commit

Permalink
Fix bug in PermutationVar space
Browse files Browse the repository at this point in the history
  • Loading branch information
thieu1995 committed Nov 5, 2023
1 parent e6f66de commit 1eb02e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
52 changes: 52 additions & 0 deletions examples/utils/run_cloud_computing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
# Created by "Thieu" at 09:34, 05/11/2023 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%

import numpy as np
from mealpy import PermutationVar, ACOR, Problem


class MyProblem(Problem):
def __init__(self, bounds, minmax="min", data=None, **kwargs):
self.data = data
super().__init__(bounds, minmax, **kwargs)

def obj_func(self, x):
order = self.decode_solution(x)["delta"]

t = np.zeros((self.data["n_jobs"], self.data["n_machines"]))
for job in range(self.data["n_jobs"]):
for machine in range(self.data["n_machines"]):
if machine==0 and job ==0:
t[job,machine] = self.data["p"][int(order[job]),machine]
elif machine==0:
t[job,machine]=t[job-1, machine]+self.data["p"][int(order[job]),machine]
elif job==0:
t[job,machine]=t[job, machine-1]+self.data["p"][int(order[job]),machine]
else:
t[job,machine]=max(t[job-1, machine],t[job, machine-1])+self.data["p"][int(order[job]),machine]
makespan=t[-1,-1]
return makespan


n_jobs = 5
n_machines = 4
data = {
"p": np.array([[4, 3, 6, 2], [1, 4, 3, 5], [2, 5, 2, 3], [5, 2, 4, 1], [3, 6, 1, 4]]),
"order": list(range(0, n_jobs)),
"n_jobs": n_jobs,
"machines": list(range(0, n_machines)),
"n_machines": n_machines,

}

problem = MyProblem(bounds=PermutationVar(valid_set=(0, 1, 2, 3, 4), name="delta"),
minmax="min", data=data, log_to="console")

model = ACOR.OriginalACOR(epoch=50, pop_size=20, sample_count = 25, intent_factor = 0.5, zeta = 1.0)
g_best = model.solve(problem)
print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}")
print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
print(f"The real solution: {problem.decode_solution(g_best.solution)['delta']}")
20 changes: 1 addition & 19 deletions mealpy/utils/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,6 @@ def _set_bounds(self, valid_set):
else:
raise TypeError(f"Invalid valid_set. It should be {self.SUPPORTED_ARRAY} and contains at least 2 variables")

@staticmethod
def min_max_scale(data, min_val, max_val):
"""
Scale a NumPy array of values to a specified range [min_val, max_val].
Parameters:
- data (numpy.ndarray): The NumPy array of values to be scaled.
- min_val (float): The minimum value in the desired range.
- max_val (float): The maximum value in the desired range.
Returns:
- scaled_data (numpy.ndarray): The scaled NumPy array of values.
"""
data_min, data_max = np.min(data), np.max(data)
scaled_data = (data - data_min) / (data_max - data_min) * (max_val - min_val) + min_val
return scaled_data

def encode(self, x):
return np.array(self.le.transform(x), dtype=float)

Expand All @@ -240,8 +223,7 @@ def decode(self, x):
return self.le.inverse_transform(x)

def correct(self, x):
x = np.clip(x, self.lb, self.ub)
return np.array(x, dtype=int)
return np.argsort(x)

def generate(self):
return self.generator.permutation(self.valid_set)
Expand Down

0 comments on commit 1eb02e5

Please sign in to comment.