Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Python Forum


You are currently viewing our Python Forum as a guest. Please register to participate.
Login



Reply
Combinatorial Optimisation (Evolutionary computation)
Old 07-07-2011, 04:18 PM Combinatorial Optimisation (Evolutionary computation)
DmitryS's Avatar
Skilled Talker

Posts: 84
Trades: 0
Hello, I've come back to using python again. I've been making a trading bot with python. One element to the bot is the machine learning part. I've made some code which executes the results from combinations of parameters to a function. The results can then be used as input to further functions.

The idea is to then find the combination with the greatest "fitness".

Here is the code with an example of how to use it. The example is useless and serves only the purpose to demonstrate the usage.

It's only a simple solution to evolutionary computation. It could almost be considered brute force but you can remove redundancy by feeding the results from one function into another.

Code:
class ParamSet():
   def __init__(self,list):
      self.results = list
      self.successful_result = None
   def set_successful_results(self,i):
      self.successful_result = self.results[i]
class CombinationExecuter():
   def __init__(self,function,*combination_params):
      self.function = function
      self.combination_params = combination_params
      self.argument_numbers = [len(x.results) for x in combination_params]
      combinations = 1
      for number in self.argument_numbers:
         combinations *= number
      self.results = []
      for x in xrange(combinations):
         params = self.params_at_index(x)
         self.results.append(self.execute_with_params(params))
      self.successful_result = None
   def params_at_index(self,i):
      arguments = []
      combinations = 1
      for number in self.argument_numbers:
         arguments.append((i/combinations) % number)
         combinations *= number
      return arguments
   def execute_with_params(self,params):
      return self.function(*[self.combination_params[x].results[params[x]] for x in xrange(len(self.combination_params))])
   def set_successful_results(self,i):
      self.successful_result = self.results[i]
      param_indicies = self.params_at_index(i)
      for x in xrange(len(param_indicies)):
         self.combination_params[x].set_successful_results(param_indicies[x])
def multiply(x,y):
   return x*y
multiply_combinations = CombinationExecuter(multiply,ParamSet(range(2)),ParamSet(range(3)))
print multiply_combinations.results
exponentiate_combinations = CombinationExecuter(pow,ParamSet(range(3)),multiply_combinations)
print exponentiate_combinations.results
for x in xrange(len(exponentiate_combinations.results)):
   if exponentiate_combinations.results[x] == 2: #Pretent 2 is the ideal "fitness"
      exponentiate_combinations.set_successful_results(x)
      break
print exponentiate_combinations.successful_result
print multiply_combinations.successful_result
__________________
»
Please login or register to view this content. Registration is FREE
for web developers
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
DmitryS is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to Combinatorial Optimisation (Evolutionary computation)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10700 seconds with 11 queries