def make_plot(surf, random=False, append=False): from util.plotly import Plot mult = 5 fun = lambda x: np.cos(x[0] * mult) + np.sin(x[1] * mult) np.random.seed(0) p = Plot() low = -0.1 upp = 1.1 dim = 2 plot_points = 2000 N = 30 if random: x = np.random.random(size=(N, dim)) else: N = int(round(N**(1 / dim))) x = np.array([ r.flatten() for r in np.meshgrid(np.linspace(low, upp, N), np.linspace(low, upp, N)) ]).T y = np.array([fun(v) for v in x]) # x = np.array([[0.5,0.5], [0.2,0.2], [0.2,0.8], [0.8,0.8], [0.8,0.2]]) # y = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) p.add("Training Points", *x.T, y) surf.fit(x, y) p.add_func("VMesh", surf, *([(low, upp)] * dim), plot_points=plot_points) p.plot(file_name="vmesh.html", append=append)
def minimize(objective, solution, bounds=None, args=tuple(), max_time=DEFAULT_MAX_TIME_SEC, min_steps=DEFAULT_MIN_STEPS, max_steps=DEFAULT_MAX_STEPS, min_improvement=DEFAULT_MIN_IMPROVEMENT, display=False, method=DiRect, checkpoint=False, checkpoint_file=CHECKPOINT_FILE): # Convert the solution into a float array (if it's not already) solution = np.asarray(solution, dtype=float) # Generate some arbitrary bounds if type(bounds) == type(None): upper = solution + np.ones((len(solution), )) * DEFAULT_SEARCH_SIZE lower = solution - np.ones((len(solution), )) * DEFAULT_SEARCH_SIZE bounds = list(zip(lower, upper)) # Initialize a tracker for halting the optimization t = Tracker(objective, max_time, min_steps, min_improvement, display, checkpoint, checkpoint_file) # Get the initial objective value of the provided solution. t.check(solution, *args) # Call the optimization function and get the best solution method(t.check, t.done, bounds, solution, args=args) if display: print() try: from util.plotly import Plot name = objective.__name__.title() p = Plot(f"Minimization Performance on Objective '{name}'", "Trial Number", "Objective Value") trial_numbers = [n for (o, n, s) in t.record] obj_values = [o for (o, n, s) in t.record] p.add("", trial_numbers, obj_values, color=p.color(1), mode="lines+markers") p.show(show_legend=False) except: pass # Remove the checkpoint file if os.path.exists(checkpoint_file): os.remove(checkpoint_file) return t.best_sol
# print("weights: ",weights) # print("predictions: ",predictions) return predictions if __name__ == "__main__": from util.plotly import Plot mult = 5 fun = lambda x: np.cos(x[0]*mult) + np.sin(x[1]*mult) np.random.seed(0) p = Plot() low = 0 upp = 1 dim = 2 plot_points = 2000 N = 4 random = True if random: x = np.random.random(size=(N,dim)) else: N = int(round(N ** (1/dim))) x = np.array([r.flatten() for r in np.meshgrid(np.linspace(low,upp,N), np.linspace(low,upp,N))]).T y = np.array([fun(v) for v in x]) p.add("Training Points", *x.T, y) surf = Linn() surf.fit(x,y) p.add_func("VMesh", surf, *([(low,upp)]*dim), plot_points=plot_points) p.plot(file_name="vmesh.html")
# Solve the actual least-squares problem A = np.concatenate(([pts[:,1]*pts[:,0]**2], [pts[:,1]*pts[:,0]], [pts[:,1]])).T B = pts[:,-1] a,b,c = np.linalg.lstsq(A,B)[0] print("Actual: d (%.3e x^2 + %.3e x + %.3e)"%(a,b,c)) # Solve the least squares as an optimizaiton problem fun = lambda c: sum( (pts[:,1]*(c[0]*pts[:,0]**2 + c[1]*pts[:,0]**1 + c[2]*pts[:,0]**0) -pts[:,-1])**2) a,b,c = minimize(fun, [0,0,0]) print("Approx: d (%.3e x^2 + %.3e x + %.3e)"%(a,b,c)) # Add the fit function to the plot fit = lambda x: x[1]*(a*x[0]**2 + b*x[0] + c) p.add("Points", *(pts.T)) p.add_func("Fit", fit,[0,10000],[2,12]) p.plot() def ydhm(total_seconds): minutes = total_seconds / 60 minutes, hours = minutes%60, (minutes-minutes%60) / 60 hours, days = hours%24, (hours-hours%24) / 24 days, years = days%365, (days-days%365) / 365 return years, days, hours, minutes n = 50 d = 1000000000 print("%i dimensions:"%d) for n in range(10,101,10): print(("%i points: %i years, %i days, %i hours, %i "+