Package: landscapes
InstallationThere are a couple ways in which you can use this library. The first and probably the easiest is by using pip and PyPi:pipinstalllandscapesYou can also install directly from this git repo:pipinstallgit+https://github.com/nathanrooy/landscapesLastly, you can always clone/download this repo and use as is.wgethttps://github.com/nathanrooy/landscapes/archive/master.zip
unzipmaster.zipcdlandscapes-masterAvailable functions from:single_objectivefunction namemethoddimensionsAckleyackley()2Ackley N.2ackley_n2()2Adjimanadjiman()2AMGMamgm()nBartels Connbartels_conn()2Birdbird()2Bealebeale()2Bent Cigarbent_cigar()nBohachevsky N.1bohachevsky_n1()2Bohachevsky N.2bohachevsky_n2()2Bohachevsky N.3bohachevsky_n3()2Boothbooth()2Braninbranin()2Brentbrent()2Brownbrown()nBukin n6bukin_n6()23-Hump Camelcamel_hump_3()26-Hump Camelcamel_hump_6()2Carrom Tablecarrom_table()2Chichinadzechichinadze()2Chung Reynoldschung_reynolds()nColvillecolville()4Cosine Mixturecosine_mixture()nCross-in-Traycross_in_tray()2Csendescsendes()nCubecube()2Damavandidamavandi()2Deckkers-Aartsdeckkers_aarts()2Dixon & Pricedixon_price()nDrop Wavedrop_wave()2Easomeasom()2Eggholdereggholder()2Exponentialexponential()nFreudenstein Rothfreudenstein_roth()2Goldstein–Pricegoldstein_price()2Griewankgriewank()nHimmelblauhimmelblau()2Hölder tableholder_table()2Hosakihosaki()2Keanekeane()2Leonleon()2Lévi function N.13levi_n13()2Matyasmatyas()2MichalewiczmichalewicznMcCormickmccormick()2Parsopoulosparsopoulos()2Pen Holderpen_holder()2Plateauplateau()nQingqing()nQuarticquartic()nRastriginrastrigin()nRotated Hyper-Ellipsoidrotated_hyper_ellipsoid()nRosenbrockrosenbrock()nSalomonsalomon()nSchaffer N.2schaffer_n2()2Schaffer N.4schaffer_n4()2Schwefelschwefel()nSpheresphere()nStepstep()nStyblinski–Tangstyblinski_tang()nSum of Different Powerssum_of_different_powers()nSum of Squaressum_of_squares()nTridtrid()nTripodtripod()2Wolfewolfe()3Zakharovzakharov()nUsageAs a simple example, let's use theNelder-Mead methodviaSciPyto minimize the sphere function. We'll start off by importing thespherefunction from Landscapes and theminimizemethod from SciPy.>>>fromlandscapes.single_objectiveimportsphere>>>fromscipy.optimizeimportminimizeNext, we'll call theminimizemethod using a starting location of [5,5].>>>minimize(sphere,x0=[5,5],method='Nelder-Mead')The output of which should look close to this:final_simplex:(array([[-3.33051318e-05,-1.93825710e-05],[4.24925225e-05,1.37129516e-05],[3.09383247e-05,-4.04797876e-05]]),array([1.48491586e-09,1.99365951e-09,2.59579314e-09]))fun:1.4849158640215086e-09message:'Optimization terminated successfully.'nfev:80nit:44status:0success:Truex:array([-3.33051318e-05,-1.93825710e-05])Function Reference - Single ObjectiveAckley functionfromlandscapes.single_objectiveimportackleyglobal minimumboundsusagef(x=0,y=0)=0-5.12 <= x, y <= 5.12ackley([x,y])Beale functionfromlandscapes.single_objectiveimportbealeglobal minimumboundsusagef(x=3, y=0.5) = 0-4.5 <= x, y <= 4.5beale([x,y])Booth functionfromlandscapes.single_objectiveimportboothglobal minimumboundsusagef(x=1, y=3) = 0-10 <= x, y <= 10booth([x,y])Bukin N.6 functionfromlandscapes.single_objectiveimportbukin_n6global minimumboundsusagef(x=-10, y=1) = 0-15 <= x <= -5-3 <= y <= 3bukin_n6([x,y])Cross-in-tray functionfromlandscapes.single_objectiveimportcross_in_trayglobal minimum(s)boundsusagef(x=1.34941, y=-1.34941) = -2.06261f(x=1.34941, y=1.34941) = -2.06261f(x=-1.34941, y=1.34941) = -2.06261f(x=-1.34941, y=-1.34941) = -2.06261-10 <= x, y <= 10cross_in_tray([x,y])Easom functionfromlandscapes.single_objectiveimporteasomglobal minimumboundsusagef(x=pi, y=pi) = -1-100 <= x, y <= 100easom([x,y])Eggholder functionfromlandscapes.single_objectiveimporteggholderglobal minimumboundsusagef(x=512, y=404.2319) = -959.6407-512 <= x, y <= 512eggholder([x,y])Goldstein–Price functionfromlandscapes.single_objectiveimportgoldstein_priceglobal minimumboundsusagef(x=0, y=-1) = 3-2 <= x, y <= 2goldstein_price([x,y])Himmelblau's functionfromlandscapes.single_objectiveimporthimmelblauglobal minimum(s)boundsusagef(x=3.0, y=2.0) = 0.0f(x=-2.805118, y=3.131312) = 0.0f(x=-3.779310, y=-3.283186) = 0.0f(x=3.584428, y=-1.848126) = 0.0-5 <= x, y <= 5himmelblau([x,y])Hölder table functionfromlandscapes.single_objectiveimportholder_tableglobal minimum(s)boundsusagef(x=8.05502, y=9.66459) = -19.2085f(x=-8.05502, y=9.66459) = -19.2085f(x=8.05502, y=-9.66459) = -19.2085f(x=-8.05502, y=-9.66459) = -19.2085-10 <= x, y <= 10holder_table([x,y])Lévi function N.13fromlandscapes.single_objectiveimportlevi_n13global minimumboundsusagef(x=1, y=1) = 0-10 <= x, y <= 10levi_n13([x,y])Matyas functionfromlandscapes.single_objectiveimportmatyasglobal minimumboundsusagef(x=0, y=0) = 0-10 <= x, y <= 10matyas([x,y])McCormick functionfromlandscapes.single_objectiveimportmccormickglobal minimumboundsusagef(x=-0.54719, y=-1.54719) = -1.9133-1.5 <= x <= 4-3 <= y <= 4mccormick([x,y])Rastrigin functionfromlandscapes.single_objectiveimportrastriginglobal minimumboundsusagef([0,...,0]) = 0-5.12 <= x_i <= 5.12rastrigin([x_1,...,x_n])Rosenbrock functionfromlandscapes.single_objectiveimportrosenbrockglobal minimumboundsusagef([1,...,1]) = 0-inf <= x_i <= infrosenbrock([x_1,...,x_n])Schaffer function N.2fromlandscapes.single_objectiveimportschaffer_n2global minimumboundsusagef(x=0, y=0) = 0-100 <= x, y <= 100schaffer_n2([x,y])Schaffer function N.4fromlandscapes.single_objectiveimportschaffer_n4global minimumboundsusagef(x=0, y=1.25313) = 0.292579f(x=0, y=-1.25313) = 0.292579-100 <= x, y <= 100schaffer_n4([x,y])Sphere functionfromlandscapes.single_objectiveimportsphereglobal minimumboundsusagef([0,...,0]) = 0-inf <= x_i <= infsphere([x_1,...x_n])Styblinski–Tang functionfromlandscapes.single_objectiveimportstyblinski_tangglobal minimumboundsusage-39.16617n < f([-2.903534,...,-2.903534]) < -39.16616n-5 <= x_i <= 5styblinski_tang([x_1,...x_n])Three-hump camel functionfromlandscapes.single_objectiveimportcamel_hump_3global minimumboundsusage-f(x=0, y=0) = 0-5 <= x_i <= 5three_hump_camel([x,y])Travelling salesman problem (TSP)fromlandscapes.single_objectiveimporttspThere are several ways to use the TSP function within Landscapes, all of which involve specifying a list of tsp stops, and a distance function.Example 1: Multi-dimensional list of points using Euclidean distance functionfromlandscapes.single_objectiveimporttspfromscipy.spatialimportdistancenp.random.seed(10)pts=np.random.rand(5,3)which will yield a list of three-dimensional points:array([[0.77132064,0.02075195,0.63364823],[0.74880388,0.49850701,0.22479665],[0.19806286,0.76053071,0.16911084],[0.08833981,0.68535982,0.95339335],[0.00394827,0.51219226,0.81262096]])Then, initialize the tsp function:tsp_cost=tsp(distance.euclidean,close_loop=True).distTo calculate the total travel distance, simply call the function with the list of points:tsp_cost(pts)>>>3.2043803044101096The flagclose_loopsimply specifies whether the distance between the first and last points should be calculated.Example 2: Specifying points using Latitude and LongitudeInsead of multi-dimensional points in space, let's specify a list of locations based on longitude and latitude then calculate the distances using the inverseVincenty's formulaewhich is available in thespatialpackage [here].First let's import our Vincenty based distance function and wrap it for easier use.fromspatialimportvincenty_inverseasvidefvi_tsp(p1,p2):returnvi(p1,p2).mi()# output distance in milesNext, let's specify some locations. Here are some breweries in Cincinnati. Each row represents a[longitude, latitude].pts=[[-84.508661,39.110187],[-84.520021,39.117219],[-84.514938,39.113937],[-84.517401,39.111322],[-84.476906,39.128957]]Again, initialize the tsp function:tsp_cost=tsp(vi_tsp,close_loop=True).distAnd finally, calculate the travel distance:tsp_cost(pts)>>>5.993331331465468Example #3: Geospatial distances on a graphInExample #2we used Vincenty's inverse formulae which calculates the distance between two longitude and latitude pairs "as the crow flies". That's great for some situations, but in a city where we're limited by streets and sidewalks, it's a little less useful. Instead, what we want is the actual distance if we were going to walk or bike. This is the network distance and is only slighly more complex, but involves some additinal libraries.First, import the dependencies:importosmnxasoximportnetworkxasnximportpandasaspdNext, load the brewery locations (availablehere) and prepare theOpen Street Map(OSM) network graph.pts_df=pd.read_csv('brewery_locations.csv')# determine bounds for osm networklats=locs_df['lat'].valueslngs=locs_df['lng'].valuesbbox=[max(lats)+0.1,min(lats)-0.1,max(lngs)+0.1,min(lngs)-0.1]# download osm street networkG=ox.graph_from_bbox(bbox[0],bbox[1],bbox[2],bbox[3],network_type='drive')Downloading the osm graph might take a bit depending on internet speed. Next, let's create a new cost function that takes in two brewery names and returns the network distance in meters.defosm_dist(n0,n1):p0=pts_df[pts_df['name']==n0][['lat','lng']].values[0]p1=pts_df[pts_df['name']==n1][['lat','lng']].values[0]p0_node=ox.get_nearest_node(G,p0)p1_node=ox.get_nearest_node(G,p1)dist_m=nx.shortest_path_length(G,p0_node,p1_node,weight='length')returndist_mAgain, specify the tsp cost function:tsp_cost=tsp(osm_dist,close_loop=True).distAnd to get the network distance:tsp_cost(locs_df['name'].values)>>>75950.73399999998This translates to roughly 47 miles.