Skip to content

Commit

Permalink
low level weights utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Apr 4, 2017
1 parent 92fcd92 commit 89a0711
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 0 deletions.
122 changes: 122 additions & 0 deletions skcriteria/tests/test_weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/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


# =============================================================================
# DOC
# =============================================================================

__doc__ = """Test normalization functionalities"""


# =============================================================================
# IMPORTS
# =============================================================================

import numpy as np

from . import core

from .. import weights, norm


# =============================================================================
# BASE
# =============================================================================

class Divergence(core.SKCriteriaTestCase):

def setUp(self):
# Data from:
# Diakoulaki, D., Mavrotas, G., & Papayannakis, L. (1995).
# Determining objective weights in multiple criteria problems:
# The critic method. Computers & Operations Research, 22(7), 763-770.
self.nmtx = norm.ideal_point([
[61, 1.08, 4.33],
[20.7, 0.26, 4.34],
[16.3, 1.98, 2.53],
[9, 3.29, 1.65],
[5.4, 2.77, 2.33],
[4, 4.12, 1.21],
[-6.1, 3.52, 2.10],
[-34.6, 3.31, 0.98]
], axis=0)
self.expected = [0.27908306, 0.34092628, 0.37999065]

def test_divergence(self):
result = weights.divergence(self.nmtx, np.std)
self.assertAllClose(result, self.expected)


class Equal(core.SKCriteriaTestCase):

def setUp(self):
self.nmtx = np.array([
[1, 1],
[2, 2]
])
self.expected = [0.5, 0.5]

def test_equal(self):
result = weights.equal(self.nmtx)
self.assertAllClose(result, self.expected)


class Critic(core.SKCriteriaTestCase):

def setUp(self):
# Data from:
# Diakoulaki, D., Mavrotas, G., & Papayannakis, L. (1995).
# Determining objective weights in multiple criteria problems:
# The critic method. Computers & Operations Research, 22(7), 763-770.
self.nmtx = norm.ideal_point([
[61, 1.08, 4.33],
[20.7, 0.26, 4.34],
[16.3, 1.98, 2.53],
[9, 3.29, 1.65],
[5.4, 2.77, 2.33],
[4, 4.12, 1.21],
[-6.1, 3.52, 2.10],
[-34.6, 3.31, 0.98]
], axis=0)
self.expected = [0.20222554, 0.48090173, 0.31687273]

def test_equal(self):
result = weights.critic(self.nmtx, np.std, np.corrcoef)
self.assertAllClose(result, self.expected)
78 changes: 78 additions & 0 deletions skcriteria/weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/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
# =============================================================================

"""This module contains functions for calculate and compare ranks (ordinal
series)
"""


# =============================================================================
# IMPORTS
# =============================================================================

import numpy as np


# =============================================================================
# WEIGHTS
# =============================================================================

def divergence(nmtx, dfunction):
dindex = dfunction(nmtx, axis=0)
return dindex / dindex.sum()


def equal(nmtx):
m = nmtx.shape[1]
weights = 1. / m
return np.tile(weights, m)


def critic(nmtx, dfunction, cfunction):
dindex = dfunction(nmtx, axis=0)
corr_m1 = 1 - cfunction(nmtx.T)
uweights = dindex * corr_m1.sum(axis=0)
weights = uweights / uweights.sum()
return weights

0 comments on commit 89a0711

Please sign in to comment.