File size: 1,189 Bytes
a257639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Text

import numpy as np
import pulp
from spopt.locate import PMedian

from facility_location.env import EvalPMPEnv


class PMPSolver:
    def __init__(self, solver: Text, env: EvalPMPEnv):
        if solver == 'GUROBI':
            self._solver = pulp.GUROBI(msg=False)
        elif solver == 'GUROBI_CMD':
            self._solver = pulp.GUROBI_CMD(msg=False)
        elif solver == 'PULP_CBC_CMD':
            self._solver = pulp.PULP_CBC_CMD(msg=False)
        elif solver == 'GLPK_CMD':
            self._solver = pulp.GLPK_CMD(msg=False)
        elif solver == 'MOSEK':
            self._solver = pulp.MOSEK(msg=False)
        else:
            raise ValueError(f'Solver {solver} not supported.')

        self.env = env

    def solve(self):
        _, demands, _, p = self.env.get_instance()
        distance_matrix, _ = self.env.get_distance_and_cost()
        pmedian_from_cost_matrix = PMedian.from_cost_matrix(distance_matrix, demands, p_facilities=p)
        pmedian_from_cost_matrix = pmedian_from_cost_matrix.solve(self._solver)
        solution = np.array([len(temp) > 0 for temp in pmedian_from_cost_matrix.fac2cli], dtype=bool)
        return solution