metacountregressor 0.1.73__py3-none-any.whl → 0.1.83__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,8 +15,14 @@ from datetime import datetime
15
15
  import numpy as np
16
16
  import pandas as pd
17
17
 
18
- from .pareto_file import Pareto, Solution
19
- from .solution import ObjectiveFunction
18
+ try:
19
+ from .pareto_file import Pareto, Solution
20
+ from .solution import ObjectiveFunction
21
+ except:
22
+ print('Exception relative import')
23
+ from pareto_file import Pareto, Solution
24
+ from solution import ObjectiveFunction
25
+
20
26
 
21
27
  HarmonySearchResults = namedtuple('HarmonySearchResults',
22
28
  ['elapsed_time', 'best_harmony', 'best_fitness', 'harmony_memories',
@@ -32,7 +38,7 @@ DifferentialEvolutionMulti = namedtuple('DifferentialEvolutionMulti',
32
38
  ['elapsed_time', 'best_solutions', 'population_solutions'])
33
39
 
34
40
 
35
- #helper function to plot the bic
41
+ # helper function to plot the bic
36
42
  def _plot(x, y, z, xlabel=None, ylabel=None, zlabel=None, filename=None):
37
43
  from matplotlib import pyplot as plt
38
44
 
@@ -54,7 +60,8 @@ def _plot(x, y, z, xlabel=None, ylabel=None, zlabel=None, filename=None):
54
60
  plt.savefig('bic.png')
55
61
  plt.show()
56
62
 
57
- #helper function to grab dictionary means
63
+
64
+ # helper function to grab dictionary means
58
65
  def dict_mean(dict_list,
59
66
  ignore=None):
60
67
  if ignore is None:
@@ -65,7 +72,7 @@ def dict_mean(dict_list,
65
72
  mean_dict[key] = sum(d[key] for d in dict_list) / len(dict_list)
66
73
  return mean_dict
67
74
  else:
68
-
75
+ mean_dict = {}
69
76
  for key in dict_list[0].keys():
70
77
  if key in ignore:
71
78
  continue
@@ -204,8 +211,7 @@ def different_evolution(objective_function, initial_slns=None, **kwargs):
204
211
 
205
212
 
206
213
  def differential_evolution(objective_function, initial_slns=None, **kwargs):
207
- if not isinstance(objective_function, ObjectiveFunction):
208
- raise Exception
214
+
209
215
  start = datetime.now()
210
216
 
211
217
  man = None
@@ -220,11 +226,8 @@ def differential_evolution(objective_function, initial_slns=None, **kwargs):
220
226
  de = Mutlithreaded_Meta(objective_function, **kwargs)
221
227
  best, pare = de.run_mp(initial_slns=initial_slns, mod_init=man)
222
228
  else:
223
-
224
229
  print('Not Multi Threaded')
225
-
226
230
  de = DifferentialEvolution(objective_function, **kwargs)
227
-
228
231
  best, pare = de.differential_evolution_run(initial_slns=initial_slns, mod_init=man)
229
232
 
230
233
  end = datetime.now()
@@ -393,20 +396,19 @@ class DifferentialEvolution(object):
393
396
  """
394
397
 
395
398
  def __init__(self, objective_function, **kwargs):
396
-
397
- if not isinstance(objective_function, ObjectiveFunction):
398
- raise TypeError
399
399
  self._obj_fun = objective_function
400
400
  if self._obj_fun._obj_1 is None:
401
- raise Exception
401
+ print('no objective found, automatically selecting BIC')
402
+ self._obj_fun._obj_1 = 'bic'
402
403
 
403
404
  self._pop_size = kwargs.get('_pop_size', 20)
405
+ print('Population size is', self._pop_size)
404
406
  if not isinstance(self._pop_size, int):
405
407
  raise ValueError("_pop_size must be an integer")
406
408
  elif self._pop_size <= 3:
407
409
  raise ValueError("_pop_size must be greater than 4")
408
410
 
409
- self.F = kwargs.get('_AI', 2) # mustation scale
411
+ self.F = kwargs.get('_AI', 2) # mutation scale
410
412
  self.iter = kwargs.get('_max_iter', 10000)
411
413
  self.cr = kwargs.get('_crossover_perc') or kwargs.get('_cr', 0.2)
412
414
  self.instance_number = str(kwargs.get('instance_number', 1))
@@ -415,12 +417,9 @@ class DifferentialEvolution(object):
415
417
  self._population = list()
416
418
  self.it_process = 1
417
419
  if objective_function.is_multi:
418
-
419
420
  self.obj_1 = objective_function._obj_1
420
421
  self.obj_2 = objective_function._obj_2
421
-
422
422
  self.pf = Pareto(self.obj_1, self.obj_2, True)
423
-
424
423
  self._pareto_population = list()
425
424
  else:
426
425
  self.obj_1 = objective_function._obj_1
@@ -555,7 +554,6 @@ class DifferentialEvolution(object):
555
554
  average_iteration = 0
556
555
  iterations_without_improvement = 0
557
556
 
558
-
559
557
  start_time = datetime.now()
560
558
  if self._obj_fun.use_random_seed():
561
559
  self._obj_fun.set_random_seed()
@@ -621,7 +619,7 @@ class DifferentialEvolution(object):
621
619
  1)
622
620
 
623
621
  if len(self._pareto_population) == 1:
624
- print('the size of the population is only 1')
622
+ print('Pareto Population Size is only 1')
625
623
  if self.pf.check_dominance([obj_trial[self.pf.obj_key_1], obj_trial[self.pf.obj_key_2]],
626
624
  [self._population[j][self.pf.obj_key_1], self._population[j][
627
625
  self.pf.obj_key_2]]): # if solution dominates existing #FIXME some error here true but not entering
@@ -790,7 +788,7 @@ class SimulatedAnnealing(object):
790
788
  self.accept = 0
791
789
  self.profiler = []
792
790
  self.update_t = self.cooling_linear_m
793
- self.get_direcotory()
791
+ self.get_directory()
794
792
  self._crossover_perc = float(kwargs.get('_crossover_perc', 0.2)) or float(kwargs.get('_cr', 0.2))
795
793
  self._obj_fun = objective_function
796
794
  if objective_function.is_multi: # TODO Define more specific objectives in the intialiser
@@ -804,7 +802,7 @@ class SimulatedAnnealing(object):
804
802
  self.pf = Pareto(self.obj_1, self.obj_2, False)
805
803
  self._sa_memory = list()
806
804
 
807
- def get_direcotory(self):
805
+ def get_directory(self):
808
806
  # checking if the directory demo_folder2
809
807
  # exist or not.
810
808
  if not os.path.isdir(self.instance_number):
@@ -949,10 +947,9 @@ class SimulatedAnnealing(object):
949
947
  output_step.append(a)
950
948
  output_energy.append(b)
951
949
  output_best_energy.append(c)
952
-
953
950
 
954
- return {'elapsed_time': elapsed_time, 'Iteration': iteration} #TODO make this reachavble
955
- #return output_step, output_energy, output_best_energy, self.best_energy, self.best_struct
951
+ return {'elapsed_time': elapsed_time, 'Iteration': iteration} # TODO make this reachavble
952
+ # return output_step, output_energy, output_best_energy, self.best_energy, self.best_struct
956
953
 
957
954
  def _get_neighbour(self, current, mutations=None):
958
955
  neighbour = copy.deepcopy(current)
@@ -963,7 +960,6 @@ class SimulatedAnnealing(object):
963
960
 
964
961
  # number of paramaters in the model #TODO get the last value if 2
965
962
 
966
-
967
963
  num_of_changeablePARMs = 0
968
964
 
969
965
  self._obj_fun.nbr_routine(current)
@@ -1009,7 +1005,7 @@ class SimulatedAnnealing(object):
1009
1005
  elif num_of_changeablePARMs == 0:
1010
1006
  rdm_i = random.choice(range(len(prmVect)))
1011
1007
  if self._obj_fun.get_num_discrete_values(rdm_i) <= 1:
1012
- print('hold gimct')
1008
+ print('retry')
1013
1009
 
1014
1010
  while self._obj_fun.get_num_discrete_values(rdm_i) <= 1:
1015
1011
  rdm_i = random.randint(0, self._obj_fun.get_num_parameters() - 1)
@@ -1051,7 +1047,7 @@ class SimulatedAnnealing(object):
1051
1047
  get_rdm_j = random.randint(0, self._obj_fun.get_num_discrete_values(rdm_i) - 1)
1052
1048
  if (self._obj_fun.get_num_discrete_values(
1053
1049
  rdm_i) - 1) < 1: # TODO: remove this is just a test
1054
- print('fucking fix this sln algorithm')
1050
+
1055
1051
  break
1056
1052
  new_nbr_i = self._obj_fun.get_value(rdm_i, get_rdm_j)
1057
1053
  neighbour[rdm_i] = new_nbr_i
@@ -1242,14 +1238,25 @@ class HarmonySearch(object):
1242
1238
  Initialize HS with the specified objective function. Note that this objective function must implement ObjectiveFunctionInterface.
1243
1239
  """
1244
1240
  self._obj_fun = objective_function
1241
+ ## NEW CODE, TRYING TO EXCTACT OUT THE PARAMATERS
1242
+ self._hms = kwargs.get('_hms', 20)
1243
+ self._par = kwargs.get('_par', .30)
1244
+ self.F = kwargs.get('_AI', 2) # mutation scale
1245
+ self.iter = kwargs.get('_max_iter', 10000)
1246
+ self.cr = kwargs.get('_crossover_perc') or kwargs.get('_cr', 0.2)
1247
+ self.instance_number = str(kwargs.get('instance_number', 1))
1248
+
1245
1249
 
1250
+
1251
+ # for printing basics metrics
1252
+ self.print_verbose = kwargs.get('verbose', False)
1246
1253
  # harmony_memory stores the best hms harmonies
1247
1254
  self._harmony_memory = list()
1248
1255
  # harmony_history stores all hms harmonies every nth improvisations (i.e., one 'generation')
1249
1256
  self._harmony_history = list()
1250
1257
  # saves the best fitness
1251
1258
  self.instance_number = str(objective_function.instance_number)
1252
- self.get_direcotory()
1259
+ self.get_directory()
1253
1260
  self._harmony_trace_best = list()
1254
1261
  self._harmony_trace_incumbent = list()
1255
1262
  if self._obj_fun.is_multi: # TODO Define more specific objectives in the intialiser
@@ -1265,7 +1272,7 @@ class HarmonySearch(object):
1265
1272
 
1266
1273
  self.pf = Pareto(self.obj_1, self.obj_2, False)
1267
1274
 
1268
- def get_direcotory(self):
1275
+ def get_directory(self):
1269
1276
  # checking if the directory demo_folder2
1270
1277
  # exist or not.
1271
1278
  if not os.path.isdir(self.instance_number):
@@ -1294,7 +1301,7 @@ class HarmonySearch(object):
1294
1301
  def does_it_appear(self, new):
1295
1302
  for d in self._harmony_memory:
1296
1303
  if self.mixed_list_chescker(d['layout'], new):
1297
- #print('same sln appears in population')
1304
+ # print('same sln appears in population')
1298
1305
  return True
1299
1306
 
1300
1307
  return False
@@ -1314,6 +1321,7 @@ class HarmonySearch(object):
1314
1321
  self._obj_fun.set_random_seed()
1315
1322
  # fill harmony_memory using random parameter values by default, but with initial_harmonies if provided
1316
1323
  self._initialize(initial_harmonies, mod_init)
1324
+ if self.print_verbose: print('Initialization complete')
1317
1325
  if self.pf.get_objective_is_multi():
1318
1326
  self._pareto_harmony_memory = self.pf.non_dominant_sorting(self._harmony_memory)
1319
1327
  generation_best = self._pareto_harmony_memory[0]
@@ -1333,6 +1341,9 @@ class HarmonySearch(object):
1333
1341
  iterations_without_improvement < self._obj_fun.get_termination_iter()):
1334
1342
  # generate new harmony
1335
1343
  elapsed_time = (datetime.now() - start_time).total_seconds()
1344
+ if self.print_verbose:
1345
+ print('Time: ', elapsed_time)
1346
+ print('Improvisation: ', num_imp)
1336
1347
  harmony = list()
1337
1348
 
1338
1349
  for i in range(0, self._obj_fun.get_num_parameters()):
@@ -1374,6 +1385,7 @@ class HarmonySearch(object):
1374
1385
  self.pf.get_objective_is_multi())
1375
1386
  num_imp += 1
1376
1387
  if iterations_without_improvement == 0: # if there is any kind of improvement updae the logs
1388
+ if self.print_verbose: print('improvement found at improvisation', num_imp)
1377
1389
  if self.pf.get_objective_is_multi():
1378
1390
  try:
1379
1391
  logger(num_imp, fitness, self._harmony_memory, True, self.get_instance_name(),
@@ -8,7 +8,7 @@ with codecs.open("README.rst", encoding='utf8') as fh:
8
8
  setuptools.setup(name='metacountregressor',
9
9
  version='0.1.63',
10
10
  description='Extensions for a Python package for \
11
- GPU-accelerated estimation of mixed logit models.',
11
+ estimation of data count models.',
12
12
  long_description=long_description,
13
13
  long_description_content_type="text/x-rst",
14
14
  url='https://github.com/zahern/CountDataEstimation',
@@ -20,5 +20,6 @@ setuptools.setup(name='metacountregressor',
20
20
  python_requires='>=3.10',
21
21
  install_requires=[
22
22
  'numpy>=1.13.1',
23
- 'scipy>=1.0.0'
23
+ 'scipy>=1.0.0',
24
+ 'latextable'
24
25
  ])