Package dsc_suite :: Package opt :: Module monte_carlo_adjusted
[hide private]
[frames] | no frames]

Source Code for Module dsc_suite.opt.monte_carlo_adjusted

 1  ''' 
 2  Created on 08.06.2010 
 3   
 4  @author: Robert Fischbach, (adepted to GUI by Tobias Heimpold) 
 5   
 6  Generating solutions randomly distributed over the solution space 
 7  without any dependencies between them. 
 8  ''' 
 9   
10  import numpy, os 
11  from dsc_suite.tools.toolpaths import FOLDER_FOR_DATA 
12  from time import strftime, time 
13   
14   
15  #TODO: What is with future parameters that are not considered now. How do they find a way into the gui. 
16  #TODO: Multi-threading support. Splitting of samples into sub-tasks. Combination-Routines required. 
17  #TODO: required time estimation 
18 -def generate_monte_carlo_data(functions, parameters, file_info, time_check = False):
19 """ Sample solution space randomly, evenly distributed and without dependencies. 20 parameter: 21 - functions: dictionary with functions from datastructure 22 essential keys: 23 - "randomSolution" : returns a random representation 24 - "costEvaluation" : returns a list of the calculated costs for the given 25 representation 26 27 - parameters : dictionary with algorithm parameters 28 essential keys: 29 - "samples" : number of generated random representations 30 31 - file_info: dictionary with information about the file names of the data files 32 essential keys: 33 - "trial_name" : name of trial given in GUI 34 - "file_name_list" : additional information included in file name (can be empty string) 35 36 - time_check: boolean 37 True: estimates runtime with reduced samples 38 False: full calculation with saving data to files 39 """ 40 import psyco #@UnresolvedImport 41 psyco.full() 42 start_time = time() 43 #create data list for saving 44 representation = functions["randomSolution"]() 45 costs = functions["costEvaluation"](representation) 46 cost_data = [] 47 for cost in costs: 48 cost_data.append([]) 49 #reduce sample for runtime estimation 50 if time_check: 51 samples = parameters["samples"] 52 parameters["samples"] = 100 53 #start random generation 54 i = 0 55 while i < parameters["samples"]: 56 i += 1 57 #generate solution 58 representation = functions["randomSolution"]() 59 #calculate cost criteria 60 costs = functions["costEvaluation"](representation) 61 #adding cost to saving list 62 for cost in costs: 63 cost_data[costs.index(cost)].append(cost) 64 #save data to files 65 trial_name = file_info["trial_name"] 66 file_name_list = file_info["file_name_list"] 67 path = '/' + trial_name + '/Monte Carlo/' 68 if not os.path.exists(FOLDER_FOR_DATA + path): 69 os.makedirs(FOLDER_FOR_DATA + path) 70 filename_list = [] 71 for list in cost_data: 72 file_name_begin = file_name_list[cost_data.index(list)] 73 i = 1 74 filename = file_name_begin + ' %02i S%i.npy' % (i, parameters["samples"]) 75 while os.path.exists(FOLDER_FOR_DATA + path + filename): 76 i += 1 77 filename = file_name_begin + ' %02i S%i.npy' % (i, parameters["samples"]) 78 filename_list.append(path + filename) 79 if not time_check: 80 numpy.save(FOLDER_FOR_DATA + path + filename, numpy.float64(list)) 81 end_time = time() 82 psyco.stop() 83 #return either runtime or filename list 84 if time_check: 85 runtime = (end_time - start_time) / parameters["samples"] * samples 86 parameters["samples"] = samples 87 return runtime 88 else: 89 return filename_list
90