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
|