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

Source Code for Module dsc_suite.opt.great_deluge_algorithm

  1  ''' module great_deluge_algorithm 
  2  - implements a Great Deluge optimization concept 
  3  - main function can be called either from console or from GUI(recommended) 
  4  - saves solution files in standard folder for data 
  5   
  6  @author: Tobias Heimpold 
  7  ''' 
  8  import random, numpy, os 
  9  from dsc_suite.tools.toolpaths import FOLDER_FOR_DATA 
 10  from time import strftime, time 
 11   
12 -def generate_great_deluge_data(functions, parameters, file_info, time_check = False):
13 '''generate_great_deluge_data function 14 parameter: 15 - functions: dictionary with functions from datastructure 16 essential keys: 17 - "randomSolution" : returns a random representation 18 - "changeSolution" : returns a function to change the given 19 representation 20 - "costEvaluation" : returns the calculated costs for the 21 given representation 22 23 - parameters : dictionary with algorithm parameters 24 essential keys: 25 - "vapor_samples" : number of tried solutions for one critical value 26 - "vapor_value" : reduce critical value with every better solution 27 28 - file_info: dictionary with information about the file names of the data files 29 essential keys: 30 - "trial_name" : name of trial given in GUI 31 - "file_name_list" : additional information included in file name (can be empty string) 32 33 - time_check: boolean 34 True: estimates runtime with reduced samples 35 False: full calculation with saving data to files 36 ''' 37 #psyco should speed python a little bit 38 import psyco 39 psyco.full() 40 #time estimation not yet working for great deluge 41 runtime = 0.0 42 43 if time_check: 44 return runtime 45 #-----------MAIN OPTIMIZATION PART---------------- 46 #init values 47 cycle = 0 48 critical_value = 1.0 49 #force optimization 50 while( cycle == 0): 51 #generate start solution 52 i = 0 53 solution = functions["randomSolution"]() 54 costs = functions["costEvaluation"](solution) 55 cost_data = [] 56 for cost in costs: 57 cost_data.append([]) 58 cost_data.append([]) 59 tabu_list = [] 60 #only until maximum value is reached 61 while (i < parameters["vapor_samples"]): 62 #create new solution 63 new_solution = functions["changeSolution"]()(solution) 64 if new_solution not in tabu_list: 65 tabu_list.append(new_solution) 66 else: 67 i +=1 68 continue 69 #evaluate solutions 70 old_costs = functions["costEvaluation"](solution) 71 sum_old = old_costs[len(old_costs)-1] 72 new_costs = functions["costEvaluation"](new_solution) 73 sum_new = new_costs[len(new_costs)-1] 74 #accept solution ? 75 if sum_new < critical_value and sum_new < sum_old: 76 #yes -- new solution is start solution 77 solution = new_solution 78 #save new solution 79 for cost in new_costs: 80 cost_data[new_costs.index(cost)].append(cost) 81 cost_data[len(cost_data)-1].append(cycle) 82 #reset abort condition 83 i = 0 84 #lower water surface 85 critical_value -= parameters["vapor_value"] 86 #raise cycle value 87 cycle += 1 88 else: 89 #no -- raise abort value 90 i += 1 91 92 #------------SAVING ROUTINE--------------------------- 93 #not to forget to stop speeding 94 psyco.stop() 95 #create new files 96 trial_name = file_info["trial_name"] 97 file_name_list = file_info["file_name_list"] 98 path = '/' + trial_name + '/Great Deluge Algorithm/' 99 if not os.path.exists(FOLDER_FOR_DATA + path): 100 os.makedirs(FOLDER_FOR_DATA + path) 101 filename_list = [] 102 for list in cost_data: 103 file_name_begin = file_name_list[cost_data.index(list)] 104 i = 1 105 filename = file_name_begin + ' %02i.npy' % (i) 106 while os.path.exists(FOLDER_FOR_DATA + path + filename): 107 i += 1 108 print i 109 filename = file_name_begin + ' %02i.npy' % (i) 110 filename_list.append(path + filename) 111 if not time_check: 112 numpy.save(FOLDER_FOR_DATA + path + filename, numpy.float64(list)) 113 #return list of created files 114 return filename_list
115