-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
293 additions
and
27 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
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
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,166 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2016-2017, Cabral, Juan; Luczywo, Nadia | ||
# All rights reserved. | ||
|
||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
|
||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
|
||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
|
||
# * Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
|
||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
# ============================================================================= | ||
# FUTURE | ||
# ============================================================================= | ||
|
||
from __future__ import unicode_literals | ||
|
||
|
||
# ============================================================================= | ||
# DOCS | ||
# ============================================================================= | ||
|
||
__doc__ = """Simus methods""" | ||
|
||
__all__ = [ | ||
"SIMUS"] | ||
|
||
|
||
# ============================================================================= | ||
# IMPORTS | ||
# ============================================================================= | ||
|
||
import operator | ||
|
||
import numpy as np | ||
|
||
import joblib | ||
|
||
from ..validate import MAX, MIN | ||
from ..utils import lp | ||
from ..utils.doc_inherit import doc_inherit | ||
|
||
from ._dmaker import DecisionMaker | ||
|
||
|
||
# ============================================================================= | ||
# FUNCTIONS | ||
# ============================================================================= | ||
|
||
def make_stage(mtx, b, senses, zindex, solver): | ||
# retrieve the problem class | ||
problem = lp.Minimize if senses[zindex] == MIN else lp.Maximize | ||
|
||
# create the variables | ||
xs = tuple( | ||
lp.Float("x{}".format(idx), low=0) for idx in range(mtx.shape[1])) | ||
|
||
# create the objective function | ||
z_coef = mtx[zindex] | ||
z = sum(c * x for c, x in zip(z_coef, xs)) | ||
|
||
# the conditions | ||
conditions = [] | ||
for idx in range(mtx.shape[0]): | ||
if idx == zindex: | ||
continue | ||
coef = mtx[idx] | ||
|
||
left = sum(c * x for c, x in zip(coef, xs)) | ||
op = operator.le if senses[idx] == MAX else operator.ge | ||
right = b[idx] | ||
|
||
condition = op(left, right) | ||
conditions.append(condition) | ||
stage = problem(z=z, solver=solver).sa(*conditions) | ||
return stage | ||
|
||
|
||
def solve(stage): | ||
return stage.solve() | ||
|
||
|
||
def simus(nmtx, ncriteria, nweights, b=None, solver="pulp", njobs=None): | ||
|
||
t_nmtx = nmtx.T | ||
|
||
b = np.asarray(b) | ||
if None in b: | ||
mins = np.min(t_nmtx, axis=1) | ||
maxs = np.max(t_nmtx, axis=1) | ||
|
||
auto_b = np.where(ncriteria == MAX, maxs, mins) | ||
b = np.where(b.astype(bool), b, auto_b) | ||
|
||
njobs = njobs or joblib.cpu_count() | ||
with joblib.Parallel(n_jobs=njobs) as jobs: | ||
stages = jobs( | ||
joblib.delayed(make_stage)( | ||
mtx=t_nmtx, b=b, senses=ncriteria, zindex=idx, solver=solver) | ||
for idx in range(t_nmtx.shape[0])) | ||
|
||
results = jobs( | ||
joblib.delayed(solve)(stage) for stage in stages) | ||
|
||
return None, results | ||
|
||
|
||
# ============================================================================= | ||
# OO | ||
# ============================================================================= | ||
|
||
class SIMUS(DecisionMaker): | ||
r""" | ||
""" | ||
|
||
def __init__(self, mnorm="none", wnorm="none", solver="pulp", njobs=None): | ||
super(SIMUS, self).__init__(mnorm=mnorm, wnorm=wnorm) | ||
self._solver = solver | ||
self._njobs = njobs | ||
|
||
@doc_inherit | ||
def solve(self, ndata, b): | ||
nmtx, ncriteria, nweights = ndata.mtx, ndata.criteria, ndata.weights | ||
rank, points = simus( | ||
nmtx, ncriteria, nweights, | ||
b=b, solver=self._solver, njobs=self._njobs) | ||
return None, rank, {"points": points} | ||
|
||
@property | ||
def solver(self): | ||
return self._solver | ||
|
||
@property | ||
def njobs(self): | ||
return self._njobs | ||
|
||
|
||
# ============================================================================= | ||
# MAIN | ||
# ============================================================================= | ||
|
||
if __name__ == "__main__": | ||
print(__doc__) |
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,84 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2016-2017-2018, Cabral, Juan; Luczywo, Nadia | ||
# All rights reserved. | ||
|
||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
|
||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
|
||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
|
||
# * Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
|
||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
# ============================================================================= | ||
# FUTURE | ||
# ============================================================================= | ||
|
||
from __future__ import unicode_literals | ||
|
||
|
||
# ============================================================================= | ||
# DOC | ||
# ============================================================================= | ||
|
||
__doc__ = """test simple methods""" | ||
|
||
|
||
# ============================================================================= | ||
# IMPORTS | ||
# ============================================================================= | ||
|
||
from skcriteria import Data | ||
from ...madm import simus | ||
|
||
from ..tcore import SKCriteriaTestCase | ||
|
||
|
||
# ============================================================================= | ||
# Tests | ||
# ============================================================================= | ||
|
||
class SimusTest(SKCriteriaTestCase): | ||
mnorm = "sum" | ||
wnorm = "sum" | ||
|
||
def setUp(self): | ||
# Data From: | ||
# Munier, N., Carignano, C., & Alberto, C. | ||
# UN MÉTODO DE PROGRAMACIÓN MULTIOBJETIVO. | ||
# Revista de la Escuela de Perfeccionamiento en Investigación | ||
# Operativa, 24(39). | ||
|
||
self.data = Data( | ||
mtx=[[250, 120, 20, 800], | ||
[130, 200, 40, 1000], | ||
[350, 340, 15, 600]], | ||
criteria=[max, max, min, max], | ||
anames=["Proyecto 1", "Proyecto 2", "Proyecto 3"], | ||
cnames=["Criterio 1", "Criterio 2", "Criterio 3", "Criterio 4"]) | ||
self.b = [None, 500, None, None] | ||
|
||
def test_simus(self): | ||
dm = simus.SIMUS() | ||
dm.decide(self.data, b=self.b) |
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.