def recursive_optimal_param(training, testing, params, step=0.1, layer=0): if step == 0.1: a_lo = 0 a_hi = 1 b_lo = 0 b_hi = 1 g_lo = 0 g_hi = 1 else: base_a, base_b, base_g = params a_lo = max(0, base_a - step * count) b_lo = max(0, base_b - step * count) g_lo = max(0, base_g - step * count) a_hi = min(1, base_a + step * count) b_hi = min(1, base_b + step * count) g_hi = min(1, base_g + step * count) len_a = math.floor((a_hi - a_lo) / step) len_b = math.floor((b_hi - b_lo) / step) len_g = math.floor((g_hi - g_lo) / step) best_al = 0 best_be = 0 best_ga = 0 best_yhmape = 0 best_pred = [] for alpha_i in range(len_a): al = a_lo + alpha_i * step for beta_i in range(len_b): be = b_lo + beta_i * step # for gamma_i in range(len_g): # ga = g_lo + gamma_i * step predictions = holtWinters(training, 52, 3, fcst_len, 'additive', alpha=al, beta=be, gamma=ga)["predicted"] yhmape = mape(testing, predictions) if best_yhmape == 0 or best_yhmape > yhmape: best_yhmape = yhmape best_al = al best_be = be best_ga = ga best_pred = predictions print("Item [{3:d}] {4:f}: alpha:{0:f}, beta:{1:f}, gamma {2:f}".format( best_al, best_be, best_ga, item, best_yhmape)) if layer == max_layer: return best_al, best_be, best_ga, best_pred, best_yhmape else: return recursive_optimal_param(training, testing, (best_al, best_be, best_ga), step=step / 10, layer=layer + 1)
except ValueError: continue gw_fcsting = gw_fcst[ind][:] predictions = [] print(testing) predictions = holtWinters(training, 52, 3, fcst_len, 'additive', alpha=al, beta=be, gamma=ga)["predicted"] # predictions = [x if x > 0 else 0 for x in predictions] yhmape = mape(testing, predictions) yhsum = yhsum + yhmape yhssum = yhssum + yhmape * yhmape gwmape = mape(testing, gw_fcsting) gwsum = gwsum + gwmape gwssum = gwssum + gwmape * gwmape print("YH_MAPE[{0:.3f}]".format(yhmape), predictions[:len(testing)]) print("GW_MAPE[{0:.3f}]".format(gwmape), gw_fcsting) print("================================================") draw1(temp, training, testing, predictions, gw_fcsting, "{0:d}[{1:.2f}_{2:.2f}_{3:.2f}].png".format(item, al, be, ga)) # break # except Exception as e: # print(e) # break
Upper_bound = upper * np.ones((1, d)) Q = np.zeros((N_pop, 1)) # Frequency v = np.zeros((N_pop, d)) # Velocities S = np.zeros((N_pop, d)) # bat position S # Initialize the population/soutions Sol = np.zeros((N_pop, d)) Fitness = np.zeros((N_pop, 1)) for i in range(N_pop): Sol[i] = np.random.uniform(Lower_bound, Upper_bound, (1, d)) predictions = \ holtWinters(training, 52, 3, fcst_len, 'additive', alpha=S[i][0], beta=S[i][1], gamma=S[i][2])[ "predicted"] Fitness[i] = mape(testing, predictions) fmax = min(Fitness) Index = list(Fitness).index(fmax) best = Sol[Index] # Start the iterations for t in range(N_gen): # Loop over all solutions for i in range(N_pop): Q[i] = np.random.uniform(Qmin, Qmax) v[i] = v[i] + (Sol[i] - best) * Q[i] S[i] = Sol[i] + v[i] if rand() > plusRate: S[i] = best + 0.001 * np.random.randn(1, d) # Apply simple bounds/limits S[i] = simplebounds(S[i], Lower_bound, Upper_bound)
continue gw_fcsting = gw_fcst[ind][:] predictions = [] print(testing) sales_dollar = ty_sales_money[ind] # prepare data ends # output best predict, parameter, model and result if calc_param: best_al, best_be, best_ga, best_pred, best_yhmape = recursive_optimal_param( training, testing, ()) else: best_al, best_be, best_ga = param_dict[item] best_pred = holtWinters(training, 52, 3, fcst_len, 'additive', best_al, best_be, best_ga)["predicted"] best_yhmape = mape(testing, best_pred) print("Item [{3:d}] {4:f}: alpha:{0:f}, beta:{1:f}, gamma {2:f}". format(best_al, best_be, best_ga, item, best_yhmape)) # compute mape yhsum = yhsum + best_yhmape yhssum = yhssum + best_yhmape * best_yhmape gwmape = mape(testing, gw_fcsting) gwsum = gwsum + gwmape gwssum = gwssum + gwmape * gwmape # output report write_file.write("{2:d},Actual_Sale,{0:.3f},{1:s}\n".format( 0, float_list_2_string(testing), item)) write_file.write("{2:d},YH_forecast,{0:.3f},{1:s}\n".format( best_yhmape, float_list_2_string(best_pred), item))