Integer, multi-objective optimization with Platypus (Python) - python

I am exploring the Platypus library for multi-objective optimization in Python. It appears to me that Platypus should support variables (optimization parameters) as integers out of the box, however this simple problem (two objectives, three variables, no constraints and Integer variables with SMPSO):
from platypus import *
def my_function(x):
""" Some objective function"""
return [-x[0] ** 2 - x[2] ** 2, x[1] - x[0]]
def AsInteger():
problem = Problem(3, 2) # define 3 inputs and 1 objective (and no constraints)
problem.directions[:] = Problem.MAXIMIZE
int1 = Integer(-50, 50)
int2 = Integer(-50, 50)
int3 = Integer(-50, 50)
problem.types[:] = [int1, int2, int3]
problem.function = my_function
algorithm = SMPSO(problem)
algorithm.run(10000)
Results into:
Traceback (most recent call last):
File "D:\MyProjects\Drilling\test_platypus.py", line 62, in
AsInteger()
File "D:\MyProjects\Drilling\test_platypus.py", line 19, in AsInteger
algorithm.run(10000)
File "build\bdist.win-amd64\egg\platypus\core.py", line 405, in run
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 820, in step
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 838, in iterate
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 1008, in _update_velocities
TypeError: unsupported operand type(s) for -: 'list' and 'list'
Similarly, if I try to use another optimization technique in Platypus (CMAES instead of SMPSO):
Traceback (most recent call last):
File "D:\MyProjects\Drilling\test_platypus.py", line 62, in
AsInteger()
File "D:\MyProjects\Drilling\test_platypus.py", line 19, in AsInteger
algorithm.run(10000)
File "build\bdist.win-amd64\egg\platypus\core.py", line 405, in run
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 1074, in step
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 1134, in initialize
File "build\bdist.win-amd64\egg\platypus\algorithms.py", line 1298, in iterate
File "build\bdist.win-amd64\egg\platypus\core.py", line 378, in evaluate_all
File "build\bdist.win-amd64\egg\platypus\evaluator.py", line 88, in evaluate_all
File "build\bdist.win-amd64\egg\platypus\evaluator.py", line 55, in run_job
File "build\bdist.win-amd64\egg\platypus\core.py", line 345, in run
File "build\bdist.win-amd64\egg\platypus\core.py", line 518, in evaluate
File "build\bdist.win-amd64\egg\platypus\core.py", line 160, in call
File "build\bdist.win-amd64\egg\platypus\types.py", line 147, in decode
File "build\bdist.win-amd64\egg\platypus\tools.py", line 521, in gray2bin
TypeError: 'float' object has no attribute 'getitem'
I get other types of error messages with other algorithms (OMOPSO, GDE3). While the algorithms NSGAIII, NSGAII, SPEA2, etc... appear to be working.
Has anyone ever encountered such issues? Maybe I am specifying the problem in te wrong way?
Thank you in advance for any suggestion.
Andrea.

try to change the way u add the problem type
problem.types[:] = [integer(-50,50),integer(-50,50),integer(-50,50)]
could work this way

Related

error on search image in python image_match library

I'm using python image_match library. I need to use search_image method of this library. but when I se this method I got the below error:
Traceback (most recent call last):
File "/var/www/html/Panel/test2.py", line 16, in <module>
ses.search_image('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg/687px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg')
File "/usr/local/lib/python3.10/site-packages/image_match/signature_database_base.py", line 268, in search_image
transformed_record = make_record(img, self.gis, self.k, self.N)
File "/usr/local/lib/python3.10/site-packages/image_match/signature_database_base.py", line 356, in make_record
signature = gis.generate_signature(path)
File "/usr/local/lib/python3.10/site-packages/image_match/goldberg.py", line 161, in generate_signature
im_array = self.preprocess_image(path_or_image, handle_mpo=self.handle_mpo, bytestream=bytestream)
File "/usr/local/lib/python3.10/site-packages/image_match/goldberg.py", line 257, in preprocess_image
return rgb2gray(image_or_path)
File "/usr/local/lib/python3.10/site-packages/skimage/_shared/utils.py", line 394, in fixed_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/skimage/color/colorconv.py", line 875, in rgb2gray
rgb = _prepare_colorarray(rgb)
File "/usr/local/lib/python3.10/site-packages/skimage/color/colorconv.py", line 140, in _prepare_colorarray
raise ValueError(msg)
ValueError: the input array must have size 3 along `channel_axis`, got (1024, 687)
Can you please help me?

How to resume from a saved snapshot in chainer using python

How to resume training from the saved snapshot in chainer.I was trying to implement DCGAN using chainer using the following github link:
https://github.com/chainer/chainer/blob/master/examples/dcgan/train_dcgan.py
When I try to give the --resume parameter it is showing shape mismatch error in the network.
In the python code there is option to give snaptshot from which we need to resume the training.These snapshots are automatically getting saved to result folder.That is also given as argument in the code.So I tried to resume training from the saved snapshot by giving the below command.
$ python train.py --resume 'snapshot.npz'
where train.py is the modified code with label for dcgan ciphar10 dataset.
The error I got by giving the above command is :
chainer.utils.type_check.InvalidType:
Invalid operation is performed in: LinearFunction (Forward)
Expect: x.shape[1] == W.shape[1]
Actual: 110 != 100
When I run the python file with the below command there is no error:
$ python train.py
Complete error trace:
Exception in main training loop:
Invalid operation is performed in: LinearFunction (Forward)
Expect: x.shape[1] == W.shape[1]
Actual: 110 != 100
Traceback (most recent call last):
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/training/trainer.py, line 315, in run
update()
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py, line 165, in update
self.update_core()
File /home/964769/Lakshmi/DCGAN/updater_with_label.py, line 50, in update_core
x_fake = gen(z,labels)
File /home/964769/Lakshmi/DCGAN/net_with_label.py, line 61, in call
h = F.reshape(F.relu(self.bn0(self.l0(F.concat((z,t),axis=1)))),
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/link.py, line 242, in call
out = forward(args, *kwargs)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/links/connection/linear.py, line 138, in forward
return linear.linear(x, self.W, self.b, n_batch_axes=n_batch_axes)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/functions/connection/linear.py, line 288, in linear
y, = LinearFunction().apply(args)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/function_node.py, line 245, in apply
self.check_data_type_forward(in_data)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/function_node.py, line 330, in check_data_type_forward
self.check_type_forward(in_type)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/functions/connection/linear.py, line 27, in check_type_forward
x_type.shape[1] == w_type.shape[1],
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/utils/typecheck.py, line 546, in expect
expr.expect()
File/home/964769/anaconda3/lib/python3.6/site-packages/chainer/utils/typecheck.py, line 483, in expect
'{0} {1} {2}'.format(left, self.inv, right))
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
Filetrain.py, line 140, in
main()
Filetrain.py, line 135, in main
trainer.run()
File/home/964769/anaconda3/lib/python3.6/site-packages/chainer/training/trainer.py, line 329, in run
six.reraise(sys.exc_info())
File /home/964769/anaconda3/lib/python3.6/site-packages/six.py, line 686, in reraise
raise value
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/training/trainer.py, line 315, in run
update()
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py, line 165, in update
self.update_core()
File /home/964769/Lakshmi/DCGAN/updater_with_label.py, line 50, in update_core
x_fake = gen(z,labels)
File /home/964769/Lakshmi/DCGAN/net_with_label.py, line 61, in call
h = F.reshape(F.relu(self.bn0(self.l0(F.concat((z,t),axis=1)))),
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/link.py, line 242, in call
out = forward(args, **kwargs)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/links/connection/linear.py, line 138, in forward
return linear.linear(x, self.W, self.b, n_batch_axes=n_batch_axes)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/functions/connection/linear.py, line 288, in linear
y, = LinearFunction().apply(args)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/function_node.py, line 245, in apply
self.check_data_type_forward(in_data)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/function_node.py, line 330, in check_data_type_forward
self.check_type_forward(in_type)
File /home/964769/anaconda3/lib/python3.6/site-packages/chainer/functions/connection/linear.py, line 27, in checktype_forward
x_type.shape[1] == w_type.shape[1],
File "/home/964769/anaconda3/lib/python3.6/site-packages/chainer/utils/typecheck.py, line 546, in expect
expr.expect()
File/home/964769/anaconda3/lib/python3.6/site-packages/chainer/utils/type_check.py", line 483, in expect
'{0} {1} {2}'.format(left, self.inv, right))
chainer.utils.type_check.InvalidType:
Invalid operation is performed in: LinearFunction (Forward)
Expect: x.shape[1] == W.shape[1]
Actual: 110 != 100

IndexError: list index out of range right after printing len(list) > 0

I literally have these two lines of code in sequence:
print(len(counter.ondas))
onda = counter.ondas[-1]
I got 13 printed for the first line and it crashed giving me tracebook to the onda = counter.ondas[-1] line saying IndexError: list index out of range right after printing len(list).
It works thousands of times before crashing. I have no clue on how to approach this problem.
Output for print(counter.ondas):
[Onda([<workers.mov.Mov object at 0x244EA050>], [Candle(4, 'GBP_JPY', Timestamp('2017-06-12 16:59:00'), 138.884, 138.897, 138.674, 138.76, 10957.0, True)]), Onda([<workers.mov.Mov object at 0x245073D0>], [Candle(4, 'GBP_JPY', Timestamp('2017-06-12 16:59:00'), 138.884, 138.897, 138.674, 138.76, 10957.0, True)...]
Output for print(type(counter.ondas)):
<class 'list'>
Output for print(isinstance(counter.ondas, list)):
True
Full Error Traceback
Traceback (most recent call last):
File "C:\Users\joaoa\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "C:\Users\joaoa\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 44, in mapstar
return list(map(*args))
File "C:\Users\joaoa\PycharmProjects\aquitania\general_manager.py", line 88, in load_observer_manager
observer_manager.update_load_run_data()
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\management\observer_manager.py", line 78, in update_load_run_data
self.load_run_data()
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\management\observer_manager.py", line 91, in load_run_data
self.feeder.exec_df(df[self.start_date:])
File "C:\Users\joaoa\PycharmProjects\aquitania\data_source\feeder.py", line 429, in exec_df
self.feed(candle)
File "C:\Users\joaoa\PycharmProjects\aquitania\data_source\feeder.py", line 98, in feed
self.make_candle(ts, candle, criteria_table)
File "C:\Users\joaoa\PycharmProjects\aquitania\data_source\feeder.py", line 126, in make_candle
self.set_values(ts, candle)
File "C:\Users\joaoa\PycharmProjects\aquitania\data_source\feeder.py", line 310, in set_values
ts_obs.feed_complete(self._candles[ts])
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\management\observer_loader.py", line 100, in feed_complete
observer.update_last_candle(candle, store_candle)
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\abstract\observer_abc.py", line 93, in update_last_candle
self.set_observe(self.update_method(candle))
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\ondas\ondas_inside.py", line 393, in update_method
self.update_routine(counter_id=2, candle=candle)
File "C:\Users\joaoa\PycharmProjects\aquitania\observer\ondas\ondas_inside.py", line 412, in update_routine
onda = list(counter.ondas)[-1]
IndexError: list index out of range
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/joaoa/PycharmProjects/aquitania/general_manager.py", line 145, in <module>
gm.run()
File "C:/Users/joaoa/PycharmProjects/aquitania/general_manager.py", line 113, in run
list_of_observer_managers = self.load_all_observer_managers()
File "C:/Users/joaoa/PycharmProjects/aquitania/general_manager.py", line 60, in load_all_observer_managers
observer = currency_pool.map(self.load_observer_manager, list_of_currencies)
File "C:\Users\joaoa\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:\Users\joaoa\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 644, in get
raise self._value
IndexError: list index out of range
I was able to idenfity the issue. The error was being thrown in a separate process than the one that was being printed and for some reason the error ocurred only after the process that was printing the results was interrupted by another unrelated error.
Weird behavior, but now I am able to tackle the problem.
Debugging in a single process mode helped a lot.
Thank you for your help.

Astropy get_gcrs_posvel in Python 2.7

In Python 2.7 I am trying to calculate the position and velocity of an observatory by doing
>>> from astropy.time import Time
>>> from astropy.coordinates import SkyCoord, EarthLocation, ICRS
>>> from astropy import units as u
>>> from astropy import coordinates
>>> time=Time(58121.93, format='mjd')
>>> location=EarthLocation(53.2367, 2.3085, 100, ellipsoid = None)
>>> op, ov = location.get_gcrs_posvel(time)
But I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\p\lib\site-packages\astropy\coordinates\earth.py", line 653, in get_gcrs_posvel
gcrs_data = self._get_gcrs(obstime).data
File "C:\p\lib\site-packages\astropy\coordinates\earth.py", line 633, in _get_gcrs
return itrs.transform_to(GCRS(obstime=obstime))
File "C:\p\lib\site-packages\astropy\coordinates\baseframe.py", line 934, in transform_to
return trans(self, new_frame)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 1314, in __call__
curr_coord = t(curr_coord, curr_toframe)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 847, in __call__
reprwithoutdiff = supcall(from_diffless, toframe)
File "C:\p\lib\site-packages\astropy\coordinates\builtin_frames\intermediate_rotation_transforms.py", line 72, in cirs_to_gcrs
return gcrs.transform_to(gcrs_frame)
File "C:\p\lib\site-packages\astropy\coordinates\baseframe.py", line 934, in transform_to
return trans(self, new_frame)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 1314, in __call__
curr_coord = t(curr_coord, curr_toframe)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 914, in __call__
return supcall(fromcoord, toframe)
File "C:\p\lib\site-packages\astropy\coordinates\builtin_frames\icrs_cirs_transforms.py", line 221, in gcrs_to_gcrs
return from_coo.transform_to(ICRS).transform_to(to_frame)
File "C:\p\lib\site-packages\astropy\coordinates\baseframe.py", line 934, in transform_to
return trans(self, new_frame)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 1314, in __call__
curr_coord = t(curr_coord, curr_toframe)
File "C:\p\lib\site-packages\astropy\coordinates\transformations.py", line 914, in __call__
return supcall(fromcoord, toframe)
File "C:\p\lib\site-packages\astropy\coordinates\builtin_frames\icrs_cirs_transforms.py", line 188, in gcrs_to_icrs
i_ra, i_dec = aticq(gcrs_ra, gcrs_dec, astrom)
File "C:\p\lib\site-packages\astropy\coordinates\builtin_frames\utils.py", line 196, in aticq
before = norm(ppr-d)
File "C:\p\lib\site-packages\astropy\coordinates\builtin_frames\utils.py", line 125, in norm
return p/np.sqrt(np.einsum('...i,...i', p, p))[..., np.newaxis]
File "C:\p\lib\site-packages\numpy\core\einsumfunc.py", line 1087, in einsum
einsum_call=True)
File "C:\p\lib\site-packages\numpy\core\einsumfunc.py", line 688, in einsum_path
input_subscripts, output_subscript, operands = _parse_einsum_input(operands)
File "C:\p\lib\site-packages\numpy\core\einsumfunc.py", line 432, in _parse_einsum_input
raise TypeError("For this input type lists must contain "
TypeError: For this input type lists must contain either int or Ellipsis
I searched for this issue on Astropy's issue tracker and came across this:
numpy TypeError on coordinate transform (TypeError: For this input type lists must contain either int or Ellipsis)
It was fixed as of Astropy v2.0.4, so, pretty recent. You should be able to upgrade your Astropy.

MDP FANode issues

I'm trying to perform factorial analysis on a distance matrix (made of distances between about 1700 points, all ranging between 0.0 and 1.0, inclusively). I'm a total FA newbie.
Anyways, this code:
fan=mdp.nodes.FANode()
far=fan.execute(a)
# a is a numpy.array, size 1780x1780
Gives me:
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
far=fan.execute(a)
File "/usr/lib/pymodules/python2.7/mdp/signal_node.py", line 575, in execute
self._pre_execution_checks(x)
File "/usr/lib/pymodules/python2.7/mdp/signal_node.py", line 451, in _pre_execution_checks
self._if_training_stop_training()
File "/usr/lib/pymodules/python2.7/mdp/signal_node.py", line 431, in _if_training_stop_training
self.stop_training()
File "/usr/lib/pymodules/python2.7/mdp/signal_node.py", line 556, in stop_training
self._train_seq[self._train_phase][1](*args, **kwargs)
File "/usr/lib/pymodules/python2.7/mdp/nodes/em_nodes.py", line 93, in _stop_training
A = normal(0., sqrt(scale/k), size=(d, k)).astype(typ)
File "mtrand.pyx", line 1279, in mtrand.RandomState.normal (numpy/random/mtrand/mtrand.c:6943)
ValueError: scale <= 0
I tried replacing 0 values with 0.00001, to no avail. Any idea what this might mean?

Categories

Resources