-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
341 lines (296 loc) · 15.7 KB
/
config.py
File metadata and controls
341 lines (296 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
#
# Name: config.py
# Purpose: This script includes all configuration parameters
# for an optimization run
#
# Author: Carola Paetzold, Christian Schweitzer, Michael Strauch
# Contact: michael.strauch@ufz.de
#
# Helmholtz Centre for Environmental Research - UFZ
# Department Computational Landscape Ecology - CLE
# Permoserstrasse 15
# D-04318 Leipzig, Germany
# http://www.ufz.de
#
# Created: Mo Apr 14 2014
#
# Copyright: (c) Carola Paetzold / Christian Schweitzer / Michael Strauch 2017
#
# Licence: This program is free software:
# you can redistribute it and/or modify it under the terms
# of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version. This program is
# distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General
# Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------
import os
from distutils.util import strtobool
try:
import ConfigParser
except ImportError:
# for Python 3
import configparser as ConfigParser
import requirements as req
# get absolute path of this file
wrkDir = os.path.abspath(".")
#-------------------------------------------------------------------------------------
# Read ini file
#-------------------------------------------------------------------------------------
def read_ini_file():
"""Read the ini file."""
# RawConfigParser(Default): default value dictionary are set for all sections
config = ConfigParser.RawConfigParser()
# dict_default holds the default values of the configuration parameters
dict_default_model = {}
# default model settings
# custom settings under [config_model] in ini file
dict_default_model.update({'file_path_r' : "C:\\Program Files\\R\\R-3.0.1\\bin\\x64\\R.exe"})
dict_default_model.update({'file_path_python' : "C:\\Python27\\Python.exe"})
dict_default_model.update({'opt_algorithm' : 'NSGA2'})
dict_default_model.update({'rpy2_available' : 'False'})
dict_default_model.update({'map' : 'False'})
dict_default_model.update({'del_help_folders' : 'False'})
dict_default_model.update({'update_files1' : 'None'})
dict_default_model.update({'update_files2' : 'None'})
dict_default_model.update({'update_files3' : 'None'})
dict_default_model.update({'update_files4' : 'None'})
dict_default_alg = {}
# default optimization settings
# custom settings under [config_optimization_algorithm] in ini file
dict_default_alg.update({'pop_size' : '100'})
dict_default_alg.update({'max_generations' : '1'})
dict_default_alg.update({'max_repair_trials' : '0'})
dict_default_alg.update({'write_tabu_memory' : 'False'})
dict_default_alg.update({'plot_results' : 'False'})
dict_default_alg.update({'maximize' : 'True'})
dict_default_alg.update({'selector' : 'default_selection'})
dict_default_alg.update({'variator' : 'default_variation'})
dict_default_alg.update({'replacer' : 'default_replacement'})
dict_default_alg.update({'migrator' : 'default_migration'})
dict_default_alg.update({'archiver' : 'default_archiver'})
dict_default_alg.update({'observer' : 'file_observer'})
dict_default_alg.update({'terminator' : 'default_termination'})
dict_default_alg.update({'crossover_rate' : '1.0'})
dict_default_alg.update({'priority' : 'True'})
dict_default_alg.update({'feasible_first_pop' : 'False'})
dict_default_alg.update({'extreme_seeds' : 'False'})
dict_default_alg.update({'num_crossover_points' : '1'})
dict_default_alg.update({'mutation_rate' : '0.1'})
dict_default_alg.update({'num_elites' : '0'})
dict_default_alg.update({'min_diversity' : '0.001'})
dict_default_alg.update({'tournament_size' : '2'})
dict_default_alg.update({'num_selected' : '1'})
dict_default_alg.update({'crowding_distance' : '2'})
dict_default_alg.update({'penalty_function' : '0'})
# default map analysis settings
# custom settings under [config_map_analysis] in ini file
dict_default_map = {}
dict_default_map.update({'file_patch_map' : 'None'})
dict_default_map.update({'file_landuse_map' : 'None'})
dict_default_map.update({'file_hru' : 'None'})
dict_default_map.update({'file_transition' : 'None'})
dict_default_map.update({'file_area' : 'None'})
dict_default_map.update({'four_neighbours' : 'False'})
dict_default_map.update({'file_worst_fitness' : 'None'})
# dict_model, dict_alg and dict_map hold all configuration parameters and their values as dictionaries
dict_model = {}
dict_alg = {}
dict_map = {}
try:
# read ini and transfer section list into dictionaries
config.read(os.path.join(wrkDir,"config.ini"))
config_model = config.items('config_model')
config_opti_alg = config.items('config_optimization_algorithm')
config_map = config.items('config_map_analysis')
for element in config_model:
dict_model[element[0]] = element[1]
for element in config_opti_alg:
dict_alg[element[0]] = element[1]
for element in config_map:
dict_map[element[0]] = element[1]
# default exceptions
try:
dict_default_alg.update({'max_evaluations' : dict_alg['pop_size']})
except KeyError: # if no pop_size is given in ini file
dict_alg.update({'pop_size' : dict_default_alg['pop_size']})
dict_default_alg.update({'max_evaluations' : dict_alg['pop_size']})
if dict_model['opt_algorithm'] == 'NSGA2':
dict_default_alg['selector'] = 'tournament_selection'
dict_default_alg['replacer'] = 'nsga_replacement'
dict_default_alg['archiver'] = 'best_archiver'
dict_default_alg['num_selected'] = dict_alg['pop_size']
elif dict_model['opt_algorithm'] == 'GA':
dict_default_alg['selector'] = 'rank_selection'
dict_default_alg['replacer'] = 'generational_replacement'
dict_default_alg['variator'] = 'n_point_crossover,bit_flip_mutation'
dict_default_alg['num_selected'] = dict_alg['pop_size']
dict_default_alg['archiver'] = 'best_archiver'
try:
if dict_alg['selector'] == 'truncation_selection':
dict_default_alg['num_selected'] = dict_alg['pop_size']
except KeyError: # troubleshooting if no value for selector is given in the ini file
pass
# check if default values should be added to the dictionaries
for element in dict_default_model.keys():
if element not in dict_model:
dict_model.update({element : dict_default_model[element]})
for element in dict_default_alg.keys():
if element not in dict_alg:
dict_alg.update({element : dict_default_alg[element]})
for element in dict_default_map.keys():
if element not in dict_map:
dict_map.update({element : dict_default_map[element]})
except ConfigParser.NoSectionError:
print("An error occurred when the program tries to read the ini file. Exists a config.ini file in the main folder? Exist a config_model and a config_optimization_algorithm section in the config.ini?")
req.close_window()
return dict_alg, dict_model, dict_map
# read the ini file
# ini_list holds dict_alg, dict_model and dict_map from read_ini_file
ini_list = read_ini_file()
dict_alg = ini_list[0]
dict_model = ini_list[1]
dict_map = ini_list[2]
#------------------------------------------------------------------------------
# Config model
#------------------------------------------------------------------------------
class ModelConfig:
def __init__(self):
"""Configure the model settings.
Variables: Description:
---------- ------------
file_path_R | file path for R
file_path_Python | file path for Python
modelx_folder | folder name of model x (1 <= x <= 4)
file_modelx | file name of the model x script
file_outputx | file name of the output file from model x
file_outputx | file names from model x which should be updated
| in the helping folders at the start of the tool
max_range | maximum number of possible land use options
opt_algorithm (string) | definition of the optimization algorithm,
available choices are GA or NSGA2
RPy_available (string) | if RPy2 is available then True, False otherwise
map | if True then transfer individuals as ascii maps into the model folders,
| else save the individuals as string of integers in a csv file
del_help_folders | if True than delete and create the helping folders at the start of the process,
| else update only the changed files in the existing helping folders
"""
# set current working directory
self.file_ini = wrkDir
# set up file path for R (not necessary for RPy2)
self.file_path_R = dict_model['file_path_r']
# set up file path for Python
self.file_path_python = dict_model['file_path_python']
# should the helping folders be deleted and created for each optimization?
self.del_help_folders = dict_model['del_help_folders']
# folder of the first model - path will be generated dynamically
self.model1_folder = dict_model['model1_folder']
# file of the first model script
self.file_model1 = dict_model['file_model1']
# file with resulting fitness values
self.file_output1 = dict_model['file_output1']
# file with the files which should be updated in the helping folders
self.update_files1 = dict_model['update_files1']
self.update_files2 = dict_model['update_files2']
self.update_files3 = dict_model['update_files3']
self.update_files4 = dict_model['update_files4']
try:
# folder of the second model
self.model2_folder = dict_model['model2_folder']
# file of the second model script
self.file_model2 = dict_model['file_model2']
# file with resulting fitness values
self.file_output2 = dict_model['file_output2']
# folder of the third model
self.model3_folder = dict_model['model3_folder']
# file of the third model script
self.file_model3 = dict_model['file_model3']
# file with resulting fitness values
self.file_output3 = dict_model['file_output3']
# folder of the fourth model
self.model4_folder = dict_model['model4_folder']
# file of the fourth model script
self.file_model4 = dict_model['file_model4']
# file with resulting fitness values
self.file_output4 = dict_model['file_output4']
except:
pass
# maximum number of possible land use options
self.max_range = int(dict_model['max_range'])
# optimization algorithm
self.opt_algorithm = dict_model['opt_algorithm']
# RPy2 is available
self.RPy2_available = dict_model['rpy2_available']
# individual transfer to model folder as string (False) or as ascii map (True)
self.map = dict_model['map']
modelConfig = ModelConfig()
#------------------------------------------------------------------------------
# Optimization algorithm configuration
#------------------------------------------------------------------------------
class EaConfig:
"""Parameter settings for the evolutionary algorithm"""
def __init__(self):
self.pop_size = int(dict_alg['pop_size'])
self.maximize = dict_alg['maximize']
self.selector = dict_alg['selector']
self.variator = dict_alg['variator']
self.replacer = dict_alg['replacer']
self.migrator = dict_alg['migrator']
self.archiver = dict_alg['archiver']
self.observer = dict_alg['observer']
self.terminator = dict_alg['terminator']
self.crossover_rate = float(dict_alg['crossover_rate'])
self.priority = dict_alg['priority']
self.feasible_first_pop = dict_alg['feasible_first_pop']
self.extreme_seeds = strtobool(dict_alg['extreme_seeds'])
self.num_crossover_points = int(dict_alg['num_crossover_points'])
self.mutation_rate = float(dict_alg['mutation_rate'])
self.num_elites = int(dict_alg['num_elites'])
self.min_diversity = float(dict_alg['min_diversity'])
self.num_selected = int(dict_alg['num_selected'])
self.crowding_distance = int(dict_alg['crowding_distance'])
self.tournament_size = int(dict_alg['tournament_size'])
self.penalty_function = int(dict_alg['penalty_function'])
self.max_generations = int(dict_alg['max_generations'])
self.max_evaluations = int(dict_alg['max_evaluations'])
self.max_repair_trials = int(dict_alg['max_repair_trials'])
self.write_tabu_memory = strtobool(dict_alg['write_tabu_memory'])
self.plot_results = strtobool(dict_alg['plot_results'])
ea = EaConfig()
#------------------------------------------------------------------------------
# Map analysis configuration
#------------------------------------------------------------------------------
class MapConfig:
"""Parameter settings for the map analysis"""
def __init__(self):
# file with transition matrix defining which land use class can change into which other
self.file_transformation = dict_map['file_transition']
# file with total area (min-max) constraints
self.file_difference = dict_map['file_area']
# optional file with HRUs (start with HRUs as basis for the genom instead of a map)
self.file_HRU = dict_map['file_hru']
# file for the original ASCII map
self.file_ASCII_map = dict_map['file_landuse_map']
# file for the patch ID map
self.file_ID_map = dict_map['file_patch_map']
# should 4 or 8 neighbouring cells be considered for patch generation
self.four_neighbours = dict_map['four_neighbours']
# file for the default worst fitness values
self.file_worst_fitness = dict_map['file_worst_fitness']
mapConfig = MapConfig()
#------------------------------------------------------------------------------
#
# EOF
#
#------------------------------------------------------------------------------