Пример #1
0
def travelling_salesman():

    N = 50
    random = Random()
    random.setSeed(0)
    points = [[0 for x in xrange(2)] for x in xrange(N)]
    for i in range(0, len(points)):
        points[i][0] = random.nextDouble()
        points[i][1] = random.nextDouble()
    fill = [N] * N
    ranges = array('i', fill)

    odd_mimic = DiscreteUniformDistribution(ranges)
    odd = DiscretePermutationDistribution(N)

    ef = TravelingSalesmanRouteEvaluationFunction(points)
    nf = SwapNeighbor()
    mf = SwapMutation()
    cf = TravelingSalesmanCrossOver(ef)

    rhc_generic("TSPrhc50", ef, odd, nf, 1.0, 10000, 10, 5)
    sa_generic("TSPsa50", ef, odd, nf, 1.0, 10000, 10, 5,
               ([1E12, 1E6], [0.999, 0.99, 0.95]))
    ga_generic("TSPga50", ef, odd, mf, cf, 50.0, 10000, 10, 1,
               ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02]))
    mimic_discrete("TSPmimic50", ef, odd_mimic, ranges, 300.0, 10000, 10, 1,
                   ([200], [100], [0.1, 0.5, 0.9]))

    print "TSP all done"
Пример #2
0
class VectorUHash:
	def __init__(self, seed):
		self.random = Random(seed)
		self.seed = seed

	def __hash(self, b):
		self.random.setSeed(self.seed)

		p = 0
		sum = 0

		while p < len(b):
			c = 0
			d = [0,0,0,0]
			while p < len(b) and c < 4:
				d[c] = ord(b[p])
				c += 1
				p += 1

			x1 = (int(d[0]) & 0xff) + ((int(d[1]) & 0xff) << 8) +\
					((int(d[2]) & 0xff) << 16) + ((int(d[3]) & 0xff) << 24)

			c = 0
			e = [0,0,0,0]
			while p < len(b) and c < 4:
				e[c] = ord(b[p])
				c += 1
				p += 1

			x2 = (int(e[0]) & 0xff) + ((int(e[1]) & 0xff) << 8) +\
					((int(e[2]) & 0xff) << 16) + ((int(e[3]) & 0xff) << 24)

			a1 = self.random.nextLong()
			a2 = self.random.nextLong()

			a1 |= 1
			a2 |= 1

			x1 += a1
			x2 += a2

			if x2 != 0:
				x1 *= x2
			sum += x1
		return int(sum & 0x7fffffff)

	def hashbytes(self, bytes):
		return self.__hash(bytes)
Пример #3
0
def knapsack():

    # Random number generator */
    random = Random()
    random.setSeed(0)
    NUM_ITEMS = 40  # The number of items
    COPIES_EACH = 4  # The number of copies each
    MAX_WEIGHT = 50  # The maximum weight for a single element
    MAX_VOLUME = 50  # The maximum volume for a single element
    KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4  # The volume of the knapsack

    # create copies
    fill = [COPIES_EACH] * NUM_ITEMS
    copies = array('i', fill)
    # create weights and volumes
    fill = [0] * NUM_ITEMS
    weights = array('d', fill)
    volumes = array('d', fill)
    for i in range(0, NUM_ITEMS):
        weights[i] = random.nextDouble() * MAX_WEIGHT
        volumes[i] = random.nextDouble() * MAX_VOLUME
    # create range
    fill = [COPIES_EACH + 1] * NUM_ITEMS
    ranges = array('i', fill)

    ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = UniformCrossOver()

    rhc_generic("KnSrhc50", ef, odd, nf, 1.0, 10000, 10, 5)
    sa_generic("KnSsa50", ef, odd, nf, 1.0, 10000, 10, 5,
               ([1E12, 1E6], [0.999, 0.99, 0.95]))
    ga_generic("KnSga50", ef, odd, mf, cf, 50.0, 10000, 10, 1,
               ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02]))
    mimic_discrete("KnSmimic50", ef, odd, ranges, 300.0, 10000, 10, 1,
                   ([200], [100], [0.1, 0.5, 0.9]))

    print "KnS all done"
Пример #4
0
# The maximum weight for a single element
MAX_WEIGHT = 50
# The maximum volume for a single element
MAX_VOLUME = 50
# The volume of the knapsack
KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4

# create copies
fill = [COPIES_EACH] * NUM_ITEMS
copies = array('i', fill)

# create weights and volumes
fill = [0] * NUM_ITEMS
weights = array('d', fill)
volumes = array('d', fill)
random.setSeed(1)  # !!!!!
for i in range(0, NUM_ITEMS):
    weights[i] = random.nextDouble() * MAX_WEIGHT
    volumes[i] = random.nextDouble() * MAX_VOLUME

# create range
fill = [COPIES_EACH + 1] * NUM_ITEMS
ranges = array('i', fill)
ef = KnapsackEvaluationFunction(
    weights, volumes, KNAPSACK_VOLUME,
    copies)  # value, volume, max_volume, the number of copies per element
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
mf = DiscreteChangeOneMutation(ranges)
cf = UniformCrossOver()
df = DiscreteDependencyTree(.1, ranges)
Пример #5
0
from common import mock_feature
from trustyai.explainers import LimeConfig, LimeExplainer
from trustyai.utils import TestUtils
from trustyai.model import (
    PerturbationContext,
    PredictionInput,
    FeatureFactory,
    SimplePrediction,
)
from java.util import Random
from org.kie.kogito.explainability.local import (
    LocalExplanationException, )

jrandom = Random()
jrandom.setSeed(0)


def test_empty_prediction():
    """Check if the explanation returned is not null"""
    config = (LimeConfig().withPerturbationContext(
        PerturbationContext(jrandom,
                            DEFAULT_NO_OF_PERTURBATIONS)).withSamples(10))
    lime_explainer = LimeExplainer(config)
    input_ = PredictionInput([])
    model = TestUtils.getSumSkipModel(0)
    output = model.predictAsync([input_]).get().get(0)
    prediction = SimplePrediction(input_, output)
    with pytest.raises(LocalExplanationException):
        lime_explainer.explain(prediction, model)