Skip to content

Instantly share code, notes, and snippets.

@jgs03177
Last active July 4, 2025 06:33
Show Gist options
  • Select an option

  • Save jgs03177/4a5185de1e67743743ba64c46f255f29 to your computer and use it in GitHub Desktop.

Select an option

Save jgs03177/4a5185de1e67743743ba64c46f255f29 to your computer and use it in GitHub Desktop.
# https://www.wolframalpha.com/input?i=minimize+%28y%5E3%2B17y%5E2+-%2818%2B2c%29y%29+where+y+%3E%3D+1%2C+2c+%3E%3D+y%5E2%2B17y-18
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10,8)
# c -> y -> x -> f -> g
def y(c):
v = ((6*c+343)**0.5-17) / 3
return np.where(v>=1, v, 1)
def x(c,y):
return c - 0.5*(y*y+17*y-18)
def f(x,y):
return x*y
def g(x,y):
return x + (y*y+17*y)/2 - 9
def wait_for_upgrade(target_array):
e = target_array[-1]
tmp = [e:=min(e,target_array[i]) for i in range(len(target_array)-1,-1,-1)]
tmp = np.array(tmp[::-1])
return tmp
# 0~600 minerals
c_limit = 601
cs = list(range(c_limit))
cs = np.array(cs)
# real number
ys = y(cs)
xs = x(cs, ys)
fs = f(xs, ys)
gs = g(xs, ys)
# integer (round up / round down)
ys_floor = np.floor(ys)
ys_ceil = np.ceil(ys)
xs_floor = np.floor(x(cs, ys_floor))
xs_ceil = np.floor(x(cs, ys_ceil))
fs_floor = f(xs_floor,ys_floor)
fs_ceil = f(xs_ceil,ys_ceil)
gs_floor = g(xs_floor,ys_floor)
gs_ceil = g(xs_ceil,ys_ceil)
# choose bigger one
fs_idx = np.where(fs_floor > fs_ceil, 1, 0)
xs_int = np.where(fs_idx, xs_floor, xs_ceil)
ys_int = np.where(fs_idx, ys_floor, ys_ceil)
fs_int = np.where(fs_idx, fs_floor, fs_ceil)
gs_int = np.where(fs_idx, gs_floor, gs_ceil)
fig, axs = plt.subplots(3)
plt.sca(axs[0])
#plt.xlabel('c')
plt.ylabel('x')
plt.plot(cs, xs, label='continuous')
plt.plot(cs, xs_floor, label='floor')
plt.plot(cs, xs_ceil, label='ceil')
plt.plot(cs, xs_int, '--', label='optimal')
#plt.legend()
plt.sca(axs[1])
#plt.xlabel('c')
plt.ylabel('y')
plt.plot(cs, ys, label='continuous')
plt.plot(cs, ys_floor, label='floor')
plt.plot(cs, ys_ceil, label='ceil')
plt.plot(cs, ys_int, '--', label='optimal')
#plt.legend()
plt.sca(axs[2])
plt.xlabel('c')
plt.ylabel('f')
plt.plot(cs, fs, label='continuous')
plt.plot(cs, fs_floor, label='floor')
plt.plot(cs, fs_ceil, label='ceil')
plt.plot(cs, fs_int, '--', label='optimal')
plt.legend()
plt.savefig('fig1', bbox_inches='tight')
plt.show()
# cannot decrease x or y.
# save mineral for upgrade
# compare rounding too
ys_round = np.round(ys)
xs_round = np.floor(x(cs, ys_round))
xs_round = wait_for_upgrade(xs_round)
xs_floor = wait_for_upgrade(xs_floor)
xs_ceil = wait_for_upgrade(xs_ceil)
fs_round = f(xs_round,ys_round)
fs_floor = f(xs_floor,ys_floor)
fs_ceil = f(xs_ceil,ys_ceil)
gs_round = g(xs_round,ys_round)
gs_floor = g(xs_floor,ys_floor)
gs_ceil = g(xs_ceil,ys_ceil)
fig, axs = plt.subplots(3)
plt.sca(axs[0])
#plt.xlabel('c')
plt.ylabel('x')
plt.plot(cs, xs, label='continuous')
plt.plot(cs, xs_floor, label='floor')
plt.plot(cs, xs_ceil, label='ceil')
plt.plot(cs, xs_round, label='round')
#plt.legend()
plt.sca(axs[1])
#plt.xlabel('c')
plt.ylabel('y')
plt.plot(cs, ys, label='continuous')
plt.plot(cs, ys_floor, label='floor')
plt.plot(cs, ys_ceil, label='ceil')
plt.plot(cs, ys_round, label='round')
#plt.legend()
plt.sca(axs[2])
plt.xlabel('c')
plt.ylabel('f')
plt.plot(cs, fs, label='continuous')
plt.plot(cs, fs_floor, label='floor')
plt.plot(cs, fs_ceil, label='ceil')
plt.plot(cs, fs_round, label='round')
plt.legend()
plt.savefig('fig2', bbox_inches='tight')
plt.show()
for ys_arr in [ys_floor, ys_round, ys_ceil]:
for i in range(600):
if ys_arr[i]!=ys_arr[i+1]:
print(int(xs_floor[i]), end=" ")
print()
# then just pick any strategy.
ys_int2 = ys_round
xs_int2 = xs_round
fs_int2 = fs_round
gs_int2 = gs_round
# apply x limit
x_limit=238
# yy + 17y - 18 - 2c + 2x =0
def y_238(c,x):
# assert c >= x
y = ((17*17-4*(-18-2*c+2*x))**0.5 - 17) / 2
return np.where(y>=1, y, 1)
xs_x238 = np.array(list(range(x_limit))+[x_limit]*(len(cs)-x_limit))
ys_x238 = np.floor(y_238(cs, xs_x238))
fs_x238 = f(xs_x238, ys_x238)
gs_x238 = g(xs_x238, ys_x238)
fs_idx = np.where(xs_int2 <= x_limit, 1, 0)
xs_actual = np.where(fs_idx, xs_int2, xs_x238)
ys_actual = np.where(fs_idx, ys_int2, ys_x238)
fs_actual = np.where(fs_idx, fs_int2, fs_x238)
gs_actual = np.where(fs_idx, gs_int2, gs_x238)
fig, axs = plt.subplots(3)
plt.sca(axs[0])
#plt.xlabel('c')
plt.ylabel('x')
plt.plot(cs, xs, label='continuous')
plt.plot(cs, xs_int2, label='discrete')
plt.plot(cs, xs_x238, label='tower first')
plt.plot(cs, xs_actual, '--', label='actual')
#plt.legend()
plt.sca(axs[1])
#plt.xlabel('c')
plt.ylabel('y')
plt.plot(cs, ys, label='continuous')
plt.plot(cs, ys_int2, label='discrete')
plt.plot(cs, ys_x238, label='tower first')
plt.plot(cs, ys_actual, '--', label='actual')
#plt.legend()
plt.sca(axs[2])
plt.xlabel('c')
plt.ylabel('f')
plt.plot(cs, fs, label='continuous')
plt.plot(cs, fs_int2, label='discrete')
plt.plot(cs, fs_x238, label='tower first')
plt.plot(cs, fs_actual, '--', label='actual')
plt.legend()
plt.savefig('fig3', bbox_inches='tight')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment