add recommender to repository

This commit is contained in:
hannes.kuchelmeister
2020-05-04 10:49:59 +02:00
parent 67ae83e0bd
commit 719f3d5ea7
51 changed files with 5792 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
from scoring.list_functions import Average, Product
import math
class TestListToValueFunctionAverage:
def test_simple_average(self):
function = Average()
list = [0.0, 1.0]
assert math.isclose(0.5, function.convertToFloat(list))
class TestListToValueFunctionProduct:
def test_simple_product(self):
function = Product()
list = [0.5, 0.5]
assert math.isclose(0.25, function.convertToFloat(list))

View File

@@ -0,0 +1,114 @@
from scoring.preferences_functions import PerUserPerFeatureDistanceAverageToListFunction, SimplePerUserToListFunction, PreferencesToListFunction, FlattenPreferencesToListFunction
from scoring.list_functions import Min
from model.configuration_model import ConfigurationModel
from model.preferences_model import Preferences
from model.product_structure_model import ProductStructureModel
preferences = Preferences({
'preferences': [
{
'user': "user0",
'ratings':[ {
'code': 'A1',
'value': 0
}, {
'code': 'A2',
'value': 1
}, {
'code': 'B1',
'value': 0.5
}
]
},
{
'user': "user1",
'ratings':[ {
'code': 'A1',
'value': 1
}, {
'code': 'B2',
'value': 1
}
]
}
]
})
toRate = ConfigurationModel({
'configuration': ['A1', 'B2'],
'variables': []
})
product_structure = ProductStructureModel({
'ProductStructure': [
{
'elementId': 'A',
'name': 'parent_element A',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'A1',
'name': 'child A1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'A2',
'name': 'child A2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},{
'elementId': 'B',
'name': 'parent_element B',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'B1',
'name': 'child B1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'B2',
'name': 'child B2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},
]
})
def float_lists_same(first, second):
for element in first:
if element not in second:
return False
second.remove(element)
if len(second) != 0:
return False
return True
class TestPreferencesToListFunction:
def test_should_be_empty_list(self):
function = PreferencesToListFunction()
assert len(function.convertToList(preferences, toRate)) == 0
class TestFlattenPreferencesToListFunction():
def test_simple_example(self):
function = FlattenPreferencesToListFunction()
assert float_lists_same([1.0,0.5,0.0,0.0,1.0], function.convertToList(preferences, toRate))
class TestSimplePerUserToListFunction():
def test_simple_example(self):
function = SimplePerUserToListFunction(Min())
assert float_lists_same([0.0, 1.0], function.convertToList(preferences, toRate))
class TestPerUserPerFeatureDistanceAverageToListFunction():
def test_simple_example(self):
function = PerUserPerFeatureDistanceAverageToListFunction(Min(), product_structure)
assert float_lists_same([0.25, 0.625], function.convertToList(preferences, toRate))

View File

@@ -0,0 +1,124 @@
from scoring.scoring_functions import PreferenceScoring, RatioCharacteristicConfigurationPenalty, WeightedFeaturePenalty, ReduceScoring
from scoring.value_functions import ValueToValueFunction
from model.configuration_model import ConfigurationModel
from model.preferences_model import Preferences
from scoring.list_functions import Min, Average
from scoring.preferences_functions import FlattenPreferencesToListFunction
from model.product_structure_model import ProductStructureModel
preferences = Preferences({
'preferences': [
{
'user': "user0",
'ratings':[ {
'code': 'A1',
'value': 0
}, {
'code': 'A2',
'value': 1
}, {
'code': 'B1',
'value': 0.5
}
]
},
{
'user': "user1",
'ratings':[ {
'code': 'A1',
'value': 1
}, {
'code': 'B2',
'value': 1
}
]
}
]
})
currentConfiguration = ConfigurationModel({
'configuration': ['A2', 'B2'],
'variables': []
})
toRate = ConfigurationModel({
'configuration': ['A1', 'B2'],
'variables': []
})
product_structure = ProductStructureModel({
'ProductStructure': [
{
'elementId': 'A',
'name': 'parent_element A',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'A1',
'name': 'child A1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'A2',
'name': 'child A2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},{
'elementId': 'B',
'name': 'parent_element B',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'B1',
'name': 'child B1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'B2',
'name': 'child B2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},
]
})
class TestRatioCharacteristicConfigurationPenalty:
def test_simple_example(self):
function = RatioCharacteristicConfigurationPenalty(product_structure, [ValueToValueFunction()])
assert 0.5 == function.calc_score(currentConfiguration, preferences, toRate)
class TestWeightedFeaturePenalty:
def test_simple_example(self):
function = WeightedFeaturePenalty(product_structure, Min(), Average())
assert 0.375 == function.calc_score(currentConfiguration, preferences, toRate)
class TestReduceScoring:
def test_combined(self):
function = ReduceScoring([
RatioCharacteristicConfigurationPenalty(product_structure, [ValueToValueFunction()]),
WeightedFeaturePenalty(product_structure, Min(), Average())
])
assert 0.875 == function.calc_score(currentConfiguration, preferences, toRate)
def test_none(self):
function = ReduceScoring([])
assert 0 == function.calc_score(currentConfiguration, preferences, toRate)
class TestPreferenceScoring:
def test_simple_example(self):
function = PreferenceScoring(
FlattenPreferencesToListFunction(),
Min()
)
assert 0 == function.calc_score(currentConfiguration, preferences, toRate)

View File

@@ -0,0 +1,44 @@
from scoring.value_functions import MapToPercent, ValueToValueFunction, HighpassFilterFunction, LowpassFilterFunction, PowerFunction
import math
class TestMapToPercent:
def test_range_conversion(self):
function = MapToPercent(-20, -40)
assert math.isclose(1, function.applyToValue(-20))
assert math.isclose(0, function.applyToValue(-40))
assert math.isclose(0.5, function.applyToValue(-30))
class TestValueToValueFunction:
def test_same_value(self):
function = ValueToValueFunction()
assert math.isclose(10, function.applyToValue(10))
class TestHighpassFilterFunction:
def test_higher_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0.8, function.applyToValue(0.8))
def test_lower_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0, function.applyToValue(0.3))
def test_same_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0.5, function.applyToValue(0.5))
class TestLowpassFilterFunction:
def test_higher_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(1, function.applyToValue(0.8))
def test_lower_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(0.3, function.applyToValue(0.3))
def test_same_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(0.5, function.applyToValue(0.5))
class TestPowerFunction:
def test_power_one(self):
function = PowerFunction(10)
assert math.isclose(1, function.applyToValue(1))
def test_power_small_number(self):
function = PowerFunction(4)
assert math.isclose(0.0001, function.applyToValue(0.1))