Example #1
0
    def value_of(self, resource):
        ''' Given a resource, return the total value
        of this resource to the current user.

        :params resource: The resource to get the value of
        :returns: The total value of the items
        '''
        (x0, span) = resource.value
        value = integrate(self.function, x0, x0 + span, self.resolution)
        return value / self.total
Example #2
0
    def __init__(self, user, function, resolution=1000):
        ''' Initialize a new preference class

        :param user: The name or id of the participant
        :param function: The function that describes the user's preference
        :param resolution: The number of steps we will take in the integral
        '''
        self.user = user or self._get_user()
        self.function = function
        self.resolution = resolution
        self.total = integrate(self.function, F(0), F(1), self.resolution)
Example #3
0
#!/usr/bin/env python
from cakery.preference import ContinuousPreference
from cakery.utilities import integrate

#------------------------------------------------------------ 
# settings
#------------------------------------------------------------ 
tolerance  = 0.005  # +/- off of 1 we accept
iterations = 1000   # the number of rounds to run
resolution = 1000   # higher means more precise integration

#------------------------------------------------------------ 
# initialize the test data
#------------------------------------------------------------ 
ps = [ContinuousPreference.random() for _ in range(iterations)]
ls = [integrate(p.function, 0.0, 0.5, resolution) for p in ps]
rs = [integrate(p.function, 0.5, 1.0, resolution) for p in ps]
ss = [l + r for l, r in zip(ls, rs)]

#------------------------------------------------------------ 
# test that the constraints hold
#------------------------------------------------------------ 
print "\n","=" * 60
print "Integration Stress Test"
print "=" * 60,"\n"

for left, right in zip(ls, rs):
    print "left[%f]\t<= right[%f]" % (left, right)
    assert(left != right)
    assert(left > right or left < right)
    assert(1.0 - tolerance < left + right < 1.0 + tolerance)
Example #4
0
 def test_integrate(self):
     ''' test that integrate method works correctly '''
     self.assertEqual(5, int(integrate(lambda x: 1, 0.0, 5.0, 100)))
     self.assertEqual(25, int(integrate(lambda x: 2 * x, 0.0, 5.0, 100)))
     self.assertEqual(41, int(integrate(lambda x: x * x, 0.0, 5.0, 100)))