|
36 | 36 | "from randon import randint, uniform\n", |
37 | 37 | "\n", |
38 | 38 | "\n", |
39 | | - "def adpt_distr(boundict, Method=True, Size=1, out='df', hardfloat=True):\n", |
| 39 | + "def adpt_distr(boundict, Method: bool=True, Size=1, out='df', hardfloat=True):\n", |
40 | 40 | " \"\"\"\n", |
41 | 41 | " Takes input with bounds, check bounds, if bounds are (float) sample from uniform 0,1\n", |
42 | 42 | " otherwise if bounds are (bool) sample from randint 0,1 and otherwise sample from randint bound to bound\n", |
|
152 | 152 | }, |
153 | 153 | "outputs": [], |
154 | 154 | "source": [ |
| 155 | + "from collections import OrderedDict as OD\n", |
| 156 | + "import numpy as np\n", |
| 157 | + "\n", |
| 158 | + "\n", |
155 | 159 | "def dicwrap(funck,\n", |
156 | | - " dicti,\n", |
157 | | - " lenit=1,\n", |
158 | | - " inpt='',\n", |
159 | | - " i_as_meth_arg=False,\n", |
160 | | - " factory=True,\n", |
161 | | - " Cmeth=\"RUN\",\n", |
162 | | - " staticD:=dict(),\n", |
163 | | - " hardfloat=False):\n", |
| 160 | + " boundings,\n", |
| 161 | + " lenit: int=1,\n", |
| 162 | + " inpt=None,\n", |
| 163 | + " i_as_meth_arg: bool=False,\n", |
| 164 | + " factory: bool=True,\n", |
| 165 | + " Cmeth: str=\"RUN\",\n", |
| 166 | + " staticD=dict(),\n", |
| 167 | + " hardfloat=False,\n", |
| 168 | + " inner_bounding=False):\n", |
164 | 169 | " \"\"\"take in function and dict and return:\n", |
165 | 170 | " if factory is True :\n", |
166 | 171 | " the primed function is returned, this function is the one given to minimize\n", |
|
174 | 179 | " args:\n", |
175 | 180 | " funck:\n", |
176 | 181 | " function to optimize\n", |
177 | | - " dicti:\n", |
178 | | - " dictionnary with kwargs, and bounds as tuple ex: {'lenght':(0,12),'height':(4,6),'condition':(bool),'size':(float)}\n", |
| 182 | + " boundings:\n", |
| 183 | + " list or ordered dictionnary, if a list is passed is should be composed of tuples,\n", |
| 184 | + " the first level of tuples contains the key and another tuple with a type or the bounds\n", |
| 185 | + " i.e.:[('a',(1,100)),('b',(float)),('c',(1,100)),('d',(bool)),('e',(1,100)),('f',(1,100))]\n", |
179 | 186 | " lenit:\n", |
180 | 187 | " lenght(row not cols) of the first innit distribution\n", |
181 | 188 | " inpt:\n", |
|
194 | 201 | " if hardfloat is true, floats will be returned in the initial guess and bounds,\n", |
195 | 202 | " this is not recomended to use with minimize,\n", |
196 | 203 | " if floats are needed in the function it is recommended to do a type check and to convert from int to float and divide\n", |
| 204 | + " inner_bounding:\n", |
| 205 | + " if True, bounds will be enforced inside the generated function and not with scipy,\n", |
| 206 | + " otherwise bounds are assumed to be useless or enforced by the optimizer\n", |
197 | 207 | " \"\"\"\n", |
198 | | - " dicf = {}\n", |
| 208 | + " if isinstance(boundings, list):\n", |
| 209 | + " dicti = OD(boundings)\n", |
| 210 | + " elif isinstance(boundings, OD):\n", |
| 211 | + " print('good type of input')\n", |
| 212 | + " elif isinstance(boundings, dict):\n", |
| 213 | + " print(\n", |
| 214 | + " \"kwargs will be in a random order, use ordered dictionnary instead\"\n", |
| 215 | + " )\n", |
| 216 | + " else:\n", |
| 217 | + " print(\"invalid input for boundings, quitting\")\n", |
| 218 | + " exit(1)\n", |
| 219 | + "\n", |
| 220 | + " dicf = OD()\n", |
199 | 221 | " args = []\n", |
200 | 222 | " bounds = []\n", |
201 | 223 | " initg = []\n", |
202 | | - " if factory and (inpt == '' or inpt == None):\n", |
| 224 | + " if factory and (\n", |
| 225 | + " inpt == None\n", |
| 226 | + " ): # set inpt as '' when creating the function to ignore it\n", |
203 | 227 | " inpt = input(\n", |
204 | 228 | " 'please input the arg that will be executed by the function')\n", |
205 | | - " for ke, va in dicti.items():\n", |
| 229 | + " for ke, va in boundings.items():\n", |
206 | 230 | " if va == bool:\n", |
207 | 231 | " dicf[ke] = (0, 1)\n", |
208 | 232 | " elif va == float:\n", |
|
223 | 247 | " if lenit > 0:\n", |
224 | 248 | " initguess = adpt_distr(\n", |
225 | 249 | " dicf, out='array', Size=lenit, hardfloat=hardfloat)\n", |
226 | | - " for kk, vv in dicti.items():\n", |
227 | | - " args.append(kk)\n", |
| 250 | + " for kk, vv in dicf.items():\n", |
228 | 251 | " bounds.append(vv)\n", |
| 252 | + " args.append(kk)\n", |
229 | 253 | " if factory:\n", |
230 | 254 | "\n", |
231 | | - " def kwargsf(initvs):\n", |
232 | | - " dictos = {}\n", |
| 255 | + " def kwargsf(initvs): # inner funct\n", |
233 | 256 | " if not (len(initvs) == len(args)):\n", |
234 | | - " print(\n", |
235 | | - " \"\"\"initial values provided are not the same lenght as keywords provided,\n", |
236 | | - " something went wrong, aborting\"\"\")\n", |
237 | | - " exit(1)\n", |
238 | | - " else:\n", |
239 | | - " dictos = dicti(zip(args, initvs))\n", |
240 | | - " if i_as_meth_arg:\n", |
| 257 | + " if isinstance(initvs,\n", |
| 258 | + " (np.ndarray,\n", |
| 259 | + " np.array)) and len(initvs[0]) == len(args):\n", |
| 260 | + " initvs = initvs[0]\n", |
| 261 | + " else:\n", |
| 262 | + " print(initvs)\n", |
| 263 | + " print(len(initvs), len(args))\n", |
| 264 | + " print(initvs.type)\n", |
| 265 | + " print(\n", |
| 266 | + " \"\"\"initial values provided are not the same lenght as keywords provided,\n", |
| 267 | + " something went wrong, aborting\"\"\")\n", |
| 268 | + " exit(1)\n", |
| 269 | + " if inner_bounding:\n", |
| 270 | + " for i in range(len(bounds)):\n", |
| 271 | + " maxx = max(bounds[i])\n", |
| 272 | + " minn = min(bounds[i])\n", |
| 273 | + " if initvs[i] > maxx:\n", |
| 274 | + " initvs[i] = maxx\n", |
| 275 | + " elif initvs[i] < minn:\n", |
| 276 | + " initvs[i] = minn\n", |
| 277 | + " dictos = dict(zip(args, initvs))\n", |
| 278 | + " if len(inpt) == 0 or len(\n", |
| 279 | + " inpt) == 1: # no static input, only values to optimize\n", |
| 280 | + " instt = funck(**staticD, **dictos)\n", |
| 281 | + " elif i_as_meth_arg:\n", |
241 | 282 | " # an alternative may be instt=funck(inpt,**staticD,**dictos)\n", |
242 | 283 | " instt = funck(**staticD, **dictos)\n", |
243 | 284 | " else:\n", |
244 | 285 | " instt = funck(inpt, **staticD, **dictos)\n", |
245 | 286 | " # if an element is present in both dictos and staticD, dictos will overwrite it\n", |
246 | 287 | " # if you want the element in staticD to never change, place it after dictos\n", |
247 | 288 | " # check if executing the function return an output\n", |
248 | | - " if not (isinstance(instt,\n", |
249 | | - " (tuple, array, ndarray, int, float, list))):\n", |
| 289 | + " if not (isinstance(instt, (tuple, int, float, list))\n", |
| 290 | + " or isinstance(instt,\n", |
| 291 | + " (np.array, np.ndarray, pd.DataFrame))):\n", |
250 | 292 | " # if no value output is returned then it is assumed that the function is a class instance\n", |
251 | 293 | " if i_as_meth_arg:\n", |
252 | 294 | " outa = getattr(instt, Cmeth)(inpt)\n", |
|
257 | 299 | " return (instt)\n", |
258 | 300 | "\n", |
259 | 301 | " if lenit > 0:\n", |
| 302 | + " if inner_bounding:\n", |
| 303 | + " return (kwargsf, initguess, args)\n", |
260 | 304 | " return (kwargsf, initguess, bounds, args)\n", |
261 | 305 | " else:\n", |
| 306 | + " if inner_bounding:\n", |
| 307 | + " return (kwargsf, args)\n", |
262 | 308 | " return (kwargsf, bounds, args)\n", |
263 | 309 | " else:\n", |
| 310 | + " if inner_bounding:\n", |
| 311 | + " return (initguess, args)\n", |
264 | 312 | " return (initguess, bounds, args)" |
265 | 313 | ] |
266 | 314 | }, |
|
293 | 341 | "\n", |
294 | 342 | "\n", |
295 | 343 | "# our dinctionnary with our bounds and variable to optimize\n", |
296 | | - "kwarg = {'first_V': (0, 23), 'second_V': (bool), 'third_V': (float)}\n", |
| 344 | + "kwarg = [('first_V', (0, 23)), ('second_V', (bool)), ('third_V', (float))]\n", |
297 | 345 | "\n", |
298 | 346 | "Function, Vector_init, Bounds, Args = dicwrap(\n", |
299 | | - " foo, dicti=kwarg, lenit=1, inpt=data)\n", |
| 347 | + " foo, dicti=kwarg, lenit=1, inpt='')\n", |
300 | 348 | "optimized = minimize(fun=Function, x0=Vector_init, bounds=Bounds)\n", |
301 | 349 | "optimize_kwargs = zip(Args, optimized)" |
302 | 350 | ] |
|
337 | 385 | " self.fourth = fourth_V\n", |
338 | 386 | "\n", |
339 | 387 | " def EXEC(self, data):\n", |
340 | | - " # do something using the instance variables set by init\n", |
| 388 | + " # do something using the instance variables set by init and some data\n", |
341 | 389 | " pass\n", |
342 | 390 | "\n", |
343 | 391 | "\n", |
344 | 392 | "# our dinctionnary with our bounds and variable to optimize\n", |
345 | | - "kwarg1 = {'first_V': (0, 23), 'second_V': (float)}\n", |
346 | | - "kwarg2 = {'third_V': (13, 38), 'fourth_V': (bool)}\n", |
347 | | - "optimized_kwargs = dict() # create empty dict to ensure everything goes well\n", |
| 393 | + "kwarg1 = [('first_V', (0, 23)), ('second_V', (float))]\n", |
| 394 | + "kwarg2 = [('third_V', (13, 38)), ('fourth_V', (bool))]\n", |
| 395 | + "optimized_kwargs = OD() # create empty dict to ensure everything goes well\n", |
348 | 396 | "\n", |
349 | 397 | "for dicto in [kwarg1, kwarg2]:\n", |
350 | 398 | " Function, Vector_init, Bounds, Args = dicwrap(\n", |
|
0 commit comments