1#! /usr/bin/env python
2# -.- coding: utf-8 -.-
3
4# Zeitgeist
5#
6# Copyright © 2011 Collabora Ltd.
7#                  By Trever Fischer <trever.fischer@collabora.co.uk>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU Lesser General Public License as published by
11# the Free Software Foundation, either version 2.1 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU Lesser General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21import random
22from pyevolve import G1DList
23from pyevolve import GSimpleGA
24from zeitgeist.datamodel import TimeRange, StorageState, ResultType
25from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
26import benchmark as engine
27import time
28
29# Chromosome to data mapping:
30# 0, 1 - Timerange begin and end. If both are zero, we use timerange.always()
31# 2 - The search type. Anything over 30 is a dead individual.
32# 3-5 - Specify template properties. Even is yes, odd is no
33# 3 - Specify a subject interpretation
34# 4 - Specify a subject manifestation
35# 5 - Specify an event actor
36def buildQuery(chromosome):
37  storage = StorageState.Any
38  numResults = 10
39  if chromosome[0] == 0 or chromosome[1] == 0:
40    timerange = TimeRange.always()
41  else:
42    timerange = (chromosome[0]*60*60*24, chromosome[1]*60*60*24)
43    if timerange[0] > timerange[1]:
44       timerange = (timerange[1], timerange[0])
45  searchType = chromosome[2]%30
46
47  eventTemplate = {}
48  subjectTemplate = {}
49
50  if chromosome[3]%2 == 0:
51    subjectTemplate['interpretation'] = random.choice(list(Interpretation.EVENT_INTERPRETATION.get_children()))
52  if chromosome[4]%2 == 0:
53    subjectTemplate['manifestation'] = random.choice(list(Manifestation.EVENT_MANIFESTATION.get_children()))
54  if chromosome[5]%2 == 0:
55    eventTemplate['actor'] = "application://google-chrome.desktop"
56  if chromosome[6]%2 == 0:
57    subjectTemplate['origin'] = "http://google.com"
58  if chromosome[7]%2 == 0:
59    subjectTemplate['uri'] = "http://google.com"
60  if chromosome[8]%2 == 0:
61    subjectTemplate['mimetype'] = "text/html"
62  if chromosome[9]%2 == 0:
63    subjectTemplate['text'] = "fish"
64  if chromosome[10]%2 == 0:
65    eventTemplate['manifestation'] = random.choice(list(Manifestation.EVENT_MANIFESTATION.get_children()))
66  if chromosome[11]%2 == 0:
67    eventTemplate['interpretation'] = random.choice(list(Interpretation.EVENT_INTERPRETATION.get_children()))
68  templates = [Event.new_for_values(subjects=[Subject.new_for_values(**subjectTemplate)], **eventTemplate)]
69
70  return (timerange, templates, storage, numResults, searchType)
71
72def eval_func(chromosome):
73  query = buildQuery(chromosome)
74  if query is None:
75    return 0
76
77  start = time.time()
78  results = engine.find_events(*query)
79  overall = (time.time() - start)
80  return (results["find_events"]*2+results["find_event_ids"]*4+results["get_events"])*1000
81
82genome = G1DList.G1DList(12)
83genome.evaluator.set(eval_func)
84ga = GSimpleGA.GSimpleGA(genome)
85ga.evolve(freq_stats = 1)
86query = buildQuery(ga.bestIndividual())
87print ga.bestIndividual()
88print query
89print "Result count: %d"%(len(engine.find_events(*query)))
90