Other multi value optimisation using brute force search? - search

I've 6 list of company possible orders (a,b,c,d,e,f) in dollars.The price changes every week (13 weeks) as below.
a1_list = [1075000, 1075000, 1072500, 1072500, 1070000, 1070000, 1050000, 1535000, 1535000, 1015000, 1015000, 1000000, 1000000]
b1_list = [1275000, 1275000, 1278000, 1278000, 1242000, 1242000, 1266000, 1806000, 1806000, 1191000, 1191000, 1191000, 1191000]
c1_list = [1530000, 1530000, 1822500, 1822500, 1813500, 1813500, 1804500, 1773000, 1773000, 1746000, 1746000, 1710000, 1710000]
d1_list = [1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 960000, 960000, 960000, 960000,
960000, 960000]
e1_list = [2436000, 2436000, 2905000, 2905000, 2877000, 2877000, 2849000, 2814000, 2814000, 2779000, 2779000, 2730000, 2730000]
f1_list = [1881000, 1881000, 1890000, 1890000, 1863000, 1863000, 1854000, 1822500, 1822500, 1804500, 1804500, 1786500, 1786500]
I can only choose 1 order from a company every time. That is, if I choose the the order for 1st week to be comp A, then company B, C, D, E and F must not be on week1. Every number in the list correspond to a particular week.
For example, for company A order, the price in week 1 and 2 will be 1075000. If the order is not made in week 1 or 2, but made in week 3 and 4, the price will be 1072500.
This is sort of an optimisation problem.
I wrote a manual calculation to calculate the best combination that give me the best profit.
Below are my codes:
import numpy as np
from scipy import optimize
price = np.array([
[430, 425, 340, 402, 348, 418],
[429, 426, 405, 402, 415, 420],
[428, 414, 403, 402, 411, 414],
[420, 422, 401, 402, 407, 412],
[614, 602, 394, 384, 402, 405],
[406, 397, 388, 384, 397, 401],
[400, 397, 380, 384, 390, 397],
])
a1Q = 2500
a2Q = 4000
b1Q = 3000
b2Q = 3500
c1Q = 4500
c2Q = 5500
c3Q = 4000
d1Q = 2500
d2Q = 4000
e1Q = 7000
e2Q = 2000
f1Q = 4500
f2Q = 1000
a1_price = price[:,0]*a1Q
a2_price = price[:,0]*a2Q
b1_price = price[:,1]*b1Q
b2_price = price[:,1]*b2Q
c1_price = price[:,2]*c1Q
c2_price = price[:,2]*c2Q
c3_price = price[:,2]*c3Q
d1_price = price[:,3]*d1Q
d2_price = price[:,3]*d2Q
e1_price = price[:,4]*e1Q
e2_price = price[:,4]*e2Q
f1_price = price[:,5]*f1Q
f2_price = price[:,5]*f2Q
a1_list = [a1_price[0]]*2 + [a1_price[1]]*2 + [a1_price[2]]*2 + [a1_price[3]] + [a1_price[4]]*2 + [a1_price[5]]*2 + [a1_price[6]]*2
a2_list = [a2_price[0]]*2 + [a2_price[1]]*2 + [a2_price[2]]*2 + [a2_price[3]] + [a2_price[4]]*2 + [a2_price[5]]*2 + [a2_price[6]]*2
b1_list = [b1_price[0]]*2 + [b1_price[1]]*2 + [b1_price[2]]*2 + [b1_price[3]] + [b1_price[4]]*2 + [b1_price[5]]*2 + [b1_price[6]]*2
b2_list = [b2_price[0]]*2 + [b2_price[1]]*2 + [b2_price[2]]*2 + [b2_price[3]] + [b2_price[4]]*2 + [b2_price[5]]*2 + [b2_price[6]]*2
c1_list = [c1_price[0]]*2 + [c1_price[1]]*2 + [c1_price[2]]*2 + [c1_price[3]] + [c1_price[4]]*2 + [c1_price[5]]*2 + [c1_price[6]]*2
c2_list = [c2_price[0]]*2 + [c2_price[1]]*2 + [c2_price[2]]*2 + [c2_price[3]] + [c2_price[4]]*2 + [c2_price[5]]*2 + [c2_price[6]]*2
c3_list = [c3_price[0]]*2 + [c3_price[1]]*2 + [c3_price[2]]*2 + [c3_price[3]] + [c3_price[4]]*2 + [c3_price[5]]*2 + [c3_price[6]]*2
d1_list = [d1_price[0]]*2 + [d1_price[1]]*2 + [d1_price[2]]*2 + [d1_price[3]] + [d1_price[4]]*2 + [d1_price[5]]*2 + [d1_price[6]]*2
d2_list = [d2_price[0]]*2 + [d2_price[1]]*2 + [d2_price[2]]*2 + [d2_price[3]] + [d2_price[4]]*2 + [d2_price[5]]*2 + [d2_price[6]]*2
e1_list = [e1_price[0]]*2 + [e1_price[1]]*2 + [e1_price[2]]*2 + [e1_price[3]] + [e1_price[4]]*2 + [e1_price[5]]*2 + [e1_price[6]]*2
e2_list = [e2_price[0]]*2 + [e2_price[1]]*2 + [e2_price[2]]*2 + [e2_price[3]] + [e2_price[4]]*2 + [e2_price[5]]*2 + [e2_price[6]]*2
f1_list = [f1_price[0]]*2 + [f1_price[1]]*2 + [f1_price[2]]*2 + [f1_price[3]] + [f1_price[4]]*2 + [f1_price[5]]*2 + [f1_price[6]]*2
f2_list = [f2_price[0]]*2 + [f2_price[1]]*2 + [f2_price[2]]*2 + [f2_price[3]] + [f2_price[4]]*2 + [f2_price[5]]*2 + [f2_price[6]]*2
best = 0
ind_a1,ind_b1,ind_c1,ind_d1,ind_e1,ind_f1 = 0,0,0,0,0,0
for a1 in a1_list:
# print(f'A: {ind_a}')
# ind_a+=1
ind_b1 = 0
# ind_c=0
for b1 in b1_list:
if ind_b1 != ind_a1:
# print(ind_b)
# total = a + b
ind_c1 = 0
for c1 in c1_list:
if ind_c1 != ind_a1 and ind_c1 != ind_b1:
ind_d1 = 0
for d1 in d1_list:
if ind_d1 != ind_a1 \
and ind_d1 != ind_b1 \
and ind_d1!= ind_c1:
ind_e1 = 0
for e1 in e1_list:
if ind_e1 != ind_a1 \
and ind_e1 != ind_b1 \
and ind_e1!= ind_c1 \
and ind_e1!= ind_d1:
ind_f1 = 0
for f1 in f1_list:
if ind_f1 != ind_a1 \
and ind_f1 != ind_b1 \
and ind_f1!= ind_c1 \
and ind_f1!= ind_d1 \
and ind_f1!= ind_e1:
total = a1 + b1 + c1 + d1 + e1 + f1
if total >best:
best = total
print(a1,b1,c1,d1,e1,f1, ind_a1,ind_b1,ind_c1,ind_d1,ind_e1,ind_f1)
print(best)
ind_f1 +=1
ind_e1 +=1
ind_d1 += 1
ind_c1+=1
ind_b1+=1
ind_a1+=1
I'm wondering if there's a simpler and cleaner way to solve this problem, or to use built in scipy optimization functions like scipy.optimize.minimize or scipy.optimize.brute?
This is actually a portion of a huge problem optimisation function that I'm solving.
Just to give you an idea (but may be irrelevant to this question), below are some plot. I'm trying to maximize the revenue.
Any help would be greatly appreciated.

This problem is best solved using munkres algorithm (or otherwise known as the Hungarian algorithm).
To get an idea how this algorithm works,
you can watch the video below.
https://www.youtube.com/watch?v=cQ5MsiGaDY8
To do it in Python, first install munkres library.
pip install munkres
You can refer here for more information.
http://software.clapper.org/munkres/
Below is my sample code to solve this problem without looping 6 billion+ times (13 variables) --> O(n!). With munkres algorithm, it's O(n^3). Apparently, there're tricks to optimized the values through some mathematical matrix reduction technique. The video above will give you some idea.
from munkres import Munkres, print_matrix
import sys
matrix = [
a1_list,
b1_list,
c1_list,
d1_list,
e1_list,
f1_list,
a2_list,
b2_list,
c2_list,
d2_list,
e2_list,
f2_list,
c3_list,
]
cost_matrix = []
for row in matrix:
cost_row = []
for col in row:
cost_row += [sys.maxsize - col]
cost_matrix += [cost_row]
m = Munkres()
indexes = m.compute(cost_matrix)
print_matrix(matrix, msg='Highest profit through this matrix:')
total = 0
for row, column in indexes:
value = matrix[row][column]
total += value
print(f'({row}, {column}) -> {value}')
print(f'total profit={total}')
You'll get result as below.
Highest profit through this matrix:
[1575000, 1575000, 1072500, 1072500, 1070000, 1070000, 1050000, 1035000, 1035000, 1015000, 1015000, 1000000, 1000000]
[1275000, 1275000, 1278000, 1278000, 1242000, 1242000, 1266000, 1806000, 1806000, 1191000, 1191000, 1191000, 1191000]
[1530000, 1530000, 1822500, 1822500, 1813500, 1813500, 1804500, 1773000, 1773000, 1746000, 1746000, 1710000, 1710000]
[1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 960000, 960000, 960000, 960000, 960000, 960000]
[2436000, 2436000, 2905000, 2905000, 2877000, 2877000, 2849000, 2814000, 2814000, 2779000, 2779000, 2730000, 2730000]
[1881000, 1881000, 1890000, 1890000, 1863000, 1863000, 1854000, 1822500, 1822500, 1804500, 1804500, 1786500, 1786500]
[2520000, 2520000, 1716000, 1716000, 1712000, 1712000, 1680000, 1656000, 1656000, 1624000, 1624000, 1600000, 1600000]
[1487500, 1487500, 1491000, 1491000, 1449000, 1449000, 1477000, 2107000, 2107000, 1389500, 1389500, 1389500, 1389500]
[1870000, 1870000, 2227500, 2227500, 2216500, 2216500, 2205500, 2167000, 2167000, 2134000, 2134000, 2090000, 2090000]
[1608000, 1608000, 1608000, 1608000, 1608000, 1608000, 1608000, 1536000, 1536000, 1536000, 1536000, 1536000, 1536000]
(0, 0) -> 1575000
(1, 7) -> 1806000
(2, 4) -> 1813500
(3, 12) -> 960000
(4, 3) -> 2905000
(5, 2) -> 1890000
(6, 1) -> 2520000
(7, 8) -> 2107000
(8, 5) -> 2216500
(9, 6) -> 1608000
total profit=19401000

Related

ValueError: Length of values (1) does not match length of index (50)

Hey there awesome peeps,
I am trying to retrieve some trend information based on some keywords that I have in a list (1000 keywords). In order to minimize the chance of getting blocked by Google I have a cutoff period of 50 and a 10 second pause. At the moment I get an error saying that my Length of value does not match the length of the index. This fails on the
df3['Trend'] = trends
If anyone can help I will really appreciate it.
Thanks!
!pip install pytrends
import pandas as pd
import json
import time
from pytrends.request import TrendReq
get_gsc_file = "/content/Queries.csv"
sortby = "Clicks"
cutoff = 50
pause = 10
timeframe = "today 3-m"
geo = "US"
df = pd.read_csv(get_gsc_file, encoding='utf-8')
df.sort_values(by=[sortby], ascending=False, inplace=True)
df = df[:cutoff]
d = {'Keyword': [], sortby:[], 'Trend': []}
df3 = pd.DataFrame(data=d)
keywords = []
trends = []
metric = df[sortby].tolist()
up = 0
down = 0
flat = 0
na = 0
for index, row in df.iterrows():
keyword = row['Top queries']
pytrends = TrendReq(hl='en-US', tz=360, retries=2, backoff_factor=0.1)
kw_list = [keyword]
pytrends.build_payload(kw_list, cat=0, timeframe=timeframe, geo=geo, gprop='')
df2 = pytrends.interest_over_time()
keywords.append(keyword)
try:
trend1 = int((df2[keyword][-5] + df2[keyword][-4] + df2[keyword][-3])/3)
trend2 = int((df2[keyword][-4] + df2[keyword][-3] + df2[keyword][-2])/3)
trend3 = int((df2[keyword][-3] + df2[keyword][-2] + df2[keyword][-1])/3)
if trend3 > trend2 and trend2 > trend1:
trends.append('UP')
up+=1
elif trend3 < trend2 and trend2 < trend1:
trends.append('DOWN')
down+=1
else:
trends.append('FLAT')
flat+=1
except:
trends.append('N/A')
na+=1
time.sleep(pause)
df3['Keyword'] = keywords
df3['Trend'] = trends
df3[sortby] = metric
def colortable(val):
if val == 'DOWN':
color="lightcoral"
elif val == 'UP':
color = "lightgreen"
elif val == 'FLAT':
color = "lightblue"
else:
color = 'white'
return 'background-color: %s' % color
df3 = df3.style.applymap(colortable)
total = len(trends)
print("Up: " + str(up) + " | " + str(round((up/total)*100,0)) + "%")
print("Down: " + str(down) + " | " + str(round((down/total)*100,0)) + "%")
print("Flat: " + str(flat) + " | " + str(round((flat/total)*100,0)) + "%")
print("N/A: " + str(na) + " | " + str(round((na/total)*100,0)) + "%")
df3

Tkinter convert entry.get() to integer problem

I am coding a program with Tkinter to calculate wind values. It takes Rwy Heading, Wind Direction and Wind Speed to calculate the wind whether headwind, tailwind or crosswind with the calculated relevant wind speed.
I get this error:
Traceback (most recent call last):
File "C:\Users\altug\Desktop\PROJECTS\ÇALIŞMA.py", line 40, in <module>
rwy_var= int(rwy)
ValueError: invalid literal for int() with base 10: ''
I both tried IntVar() and int(str) method to convert string into integer but it didn't sort out the problem.
Below you can find my code:
ent1 = Entry(root)
ent1.grid(row = 0, column = 1)
ent2 = Entry(root)
ent2.grid(row = 1, column = 1)
ent3 = Entry(root)
ent3.grid(row = 2, column = 1)
rwy = ent1.get()
wind_direction = ent2.get()
wind_speed = ent3.get()
rwy_var= IntVar()
wind_direction_var= IntVar()
wind_speed_var = IntVar()
rwy_var= int(rwy)
wind_direction_var= int(wind_direction)
wind_speed_var = int(wind_speed)
x = rwy_var - wind_direction_var
radx = math.radians(x)
sinx = math.sin(radx)
cosx = math.cos(radx)
def htwind():
txt.delete(0.0, "end")
b = rwy_var + 90
a = rwy_var - 90
if b > 360:
b -= 360
elif a <0:
a+= 360
if x == abs(90):
result = 0
txt.insert(0.0, result +'\n')
elif a <= wind_direction_var or wind_direction_var<= b :
sh = wind_speed_var * cosx
result = "Headwind"+ " " + round(abs(sh))+ " " + "kt"
txt.insert(0.0, result +'\n')
elif a >= wind_direction_var or wind_direction_var >= b :
st = wind_speed_var * cosx
result = "Tailwind"+ " " + round(abs(st))+ " " + "kt"
txt.insert(0.0, result +'\n')
return ""
def xwind():
txt.delete(0.0, "end")
c=rwy_var-180
d= rwy_var + 180
if d > 360:
d -= 360
elif c<0:
c+= 360
if x == 0 or x == abs(180) or y == 0 or y == abs(180):
print("0")
elif c< wind_direction_var:
sxwl = wind_speed_var * sinx
rslt = "L"+""+round(abs(sxwl))+""+"kt"
txt.insert(0.0, rslt +'\n')
elif wind_direction_var<d:
sxwr = wind_speed_var * sinx
rslt = "R"+""+round(abs(sxwr))+""+"kt"
txt.insert(0.0, rslt +'\n')
return ""
txt = Text(root, width=6, height=2, wrap =WORD)
txt.grid(row =1, column = 1)
button_1 =Button(root, text = "search", command=lambda:[htwind(),xwind()])
button_1.grid(row =0, column = 5)
What is my mistake ?
Thanks in advance.
The IntVar is special in that it can't be directly converted to an int. To get its int value, do
rwy_var = rwy.get()

Neukum production function

I am trying to plot a function which looks like that: .
Where, D range from 0.01 to 300km,
N is number of craters with dia > D per sq.km per giga year,
coefficients values are in code.
I get empty plot and obviously I am doing something very wrong which I am not able to understand. I am sharing my code.
'''
a_0 = -3.0876
a_1 = -3.557528
a_2 = 0.781027
a_3 = 1.021521
a_4 = -0.156012
a_5 = -0.444058
a_6 = 0.019977
a_7 = 0.086850
a_8 = -0.005874
a_9 = -0.006809
a_10 = 8.25*10**-4
a_11 = 5.54*10**-5
a_n = a_0 + a_1 + a_2 + a_3 + a_4 + a_5 + a_6 + a_7 + a_8 + a_9 + a_10 + a_11
for d in range(1, 200000):
n = a_0 + np.multiply(a_1, np.log(d))
+ np.multiply(a_2, np.log(d)**2)
+ np.multiply(a_3, np.log(d)**3)
+ np.multiply(a_4, np.log(d)**4)
+ np.multiply(a_5, np.log(d)**5)
+ np.multiply(a_6, np.log(d)**6)
+ np.multiply(a_7, np.log(d)**7)
+ np.multiply(a_8, np.log(d)**8)
+ np.multiply(a_9, np.log(d)**9)
+ np.multiply(a_10, np.log(d)**10)
+ np.multiply(a_11, np.log(d)**11)
print(10**n)
plt.plot(10**n, color='green')
plt.show()
'''
The graphical curve should look something like that
I can not understand your code, but if you want to plot the function you wrote you can try this:
import numpy as np
import matplotlib.pyplot as plt
a_0 = -3.0876
a_1 = -3.557528
a_2 = 0.781027
a_3 = 1.021521
a_4 = -0.156012
a_5 = -0.444058
a_6 = 0.019977
a_7 = 0.086850
a_8 = -0.005874
a_9 = -0.006809
a_10 = 8.25*10**(-4)
a_11 = 5.54*10**(-5)
N_POINTS = 100
a_coeff = np.array([a_0, a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8, a_9, a_10, a_11])
distance = np.logspace(np.log10(0.01), np.log10(200), N_POINTS)
exponents = np.arange(12)
distance_matrix = distance[:, np.newaxis]*np.ones([N_POINTS, 12])
N = 10**np.sum(a_coeff * (np.log10(distance_matrix)**exponents), axis=1)
fig, ax = plt.subplots(1, figsize=(3, 8))
ax.scatter(distance, N)
ax.set(xscale='log', yscale='log', ylim=(1e-7, 1e4), xlim=(0.001, 300))

in fit_generator, training_generator was influenced by validation_generator

I met a strange problem in fit_generator
model.fit_generator(generate_arrays_from_file(trainSizeListImgDic[s], s, batchSize), steps_per_epoch=math.floor(size / batchSize), epochs=20,
verbose=2, validation_data=generate_arrays_from_file(testSizeListImgDic[s], s, batchSize),
validation_steps=vs,callbacks=[EarlyStoppingByAccVal(monitor='val_acc', value=0.90, verbose=1),checkpointer])
and my generate_arrays_from_file reads:
def generate_arrays_from_file( SizeListImg ,img_size,batch):
size = len(SizeListImg.images)
dim = re.split('[,()]', img_size)
dataX = np.zeros((batch, int(dim[1]), int(dim[2]), 1), dtype=np.float32)
dataY = np.zeros((batch, num_classes), dtype=np.float32)
loopcount = math.floor( size/batch )-1
if loopcount==0:
loopcount = 1
counter = 0
while (True):
i = random.randint(0,loopcount)
for ind in range( (i*batch) , (i + 1)*batch ):
try:
dataX[counter, :, :, 0] = SizeListImg.images[ind]
except :
print('dim='+ str(dim) )
print('error counter=' + str(counter) + " i="+str(i) + " ind=" + str(ind) + " batch="+str(batch) + "\n" )
print("SizeListImg.images="+str( len(SizeListImg.images) ) )
print( "img0 = "+SizeListImg.images_names[0])
print("img1 = " + SizeListImg.images_names[1])
print("img2 = " + SizeListImg.images_names[2])
print("img3 = " + SizeListImg.images_names[3])
for j, imgClass in enumerate(imgClasses):
dataY[counter, j] = (SizeListImg.labels[ind] == imgClass)
counter += 1
if counter>=batch:
yield (dataX,dataY)
counter = 0
dataX = np.zeros((batch, int(dim[1]), int(dim[2]), 1), dtype=np.float32) #not tf.zeros((25, 200, 200, 1)) please noted different: np.zeros(
dataY = np.zeros((batch, num_classes), dtype=np.float32)
During training I found size of training images 139 be reduced to the size of validation images 22, and that lead to the index wrong but the images indeed came from training images set. However if I reduce batch from 20 to 10, no any error.
Any conspiracy of fit_generator against me?

How can I speed up this code, it has a very big input file

Could you please help me speed up this code?
It takes a very long time to run because of how big the input file is, thank you to anyone who helps out.
racers = [int(file[0].split()[0]) / float(x) for x in file[0].split()[1::]]
print("hi")
def av(race):
race = race.split()
j = 0
while j != len([float(race[0]) / float(x) for x in race[1::]]):
racers[j] = [float(race[0]) / float(x) for x in race[1::]][j] + racers[j]
j += 1
for i in range(1, len(file)):
av(file[i])
a = min(racers)
del(racers[racers.index(min(racers))])
b = min(racers)
c = b-a
h = int(c)
c-=h
m = int(c * 60)
c-=m/60
s = round(c * 60 * 60)
print(str(h) + "h" + str(m) + "m" + str(s) + "s")

Resources