o.minimize() does not give me the minimal solution - z3

In my last post i got this min(max()) function:
def maxi(a,b):
return If(a>=b,a,b)
a = 0
for y in zip(*time): #time is a list with many rows and columns
z = sum(y) #z receives the sum of the columns
a = maxi(a,z)
o.minimize(a)
Info: Each variable in my matrix can have the value 7.08 or 0
My problem is the following:
-> If I put a=0, I will never get a solution
-> If I put a=50, I will get a solution but I will never get the best solution.
What I'm doing wrong? Thx!

Related

Love2d adding value to table inside for loop.. Using a variable

This is something that's really been nagging at me for some time:
for i = 1, 4 do
x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape):setCategory(10)
x = x:setUserData('Border') -- error here
table.insert(data, x)
end
Let's say I want to insert a variable into the table (basically creating the variable, and then modifying it) and then inserting it:
When I do the x = x:setUserData(...) an error comes up.. saying attempt to index global variable x (nil)
So my question is, how would I create a variable inside a for loop, specifically
I need to do it this way because I'm using love.physics, and creating a fixture with a category. I also need to setUserData at that time but it's not possible.
And I'm sure there has to be a way of doing this...
Thanks in advance!!
The function Fixture:setCategory does not return a value.
so when you do this
x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape):setCategory(10)
you are setting x = nil.
Fixture:setUserData also does not return a value.
If you change it to this you will no longer get that error.
for i = 1, 4 do
x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape)
x:setCategory(10)
x:setUserData('Border') -- error here
table.insert(data, x)
end

How does the for loops function here?

What does the following code do?
I am confused on how the for loops function here, and would appreciate any help to understand.
average_mae_history = [np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]
Suppose avg_mae_history has say 4 lists each with 500 elements, where the 4 lists correspond to the 4 folds and 500 elements correspond to the 500 epochs performed for each fold.
Asking questions without trying out and telling what you have done in order to clarify your doubt is wrong.
Anyway I will explain the code snippet:
Its a simple list comprehension with two for loops.
What it basically does is:
for i in range(num_epochs):
for j in all_mae_history:
temp = []
temp.append(j[i])
z.append(max(temp))
Next time tell us what you have done before posting code snippets.
The correct answer would be
mean = [np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]
print('mean - ', mean)
z = []
for i in range(num_epochs):
temp = []
for j in all_mae_histories:
temp.append(j[i])
z.append(np.mean(temp))
print('mean z ', z)

Dask: what function variable is best to choose for visualize()

I am trying to understand Dask delayed more deeply so I decided to work through the examples here. I modified some of the code to reflect how I want to use Dask (see below). But the results are different than what I expected ie. a tuple vs list. When I try to apply '.visualize()' to see what the execution graph looks like I get nothing.
I worked through all the examples in 'delayed.ipynb' and they all work properly including all the visualizations. I then modified the 'for' loop for one example:
for i in range(256):
x = inc(i)
y = dec(x)
z = add(x, y)
zs.append(z)
to a function call the uses a list comprehension. The result is a variation on the original working example.
%%time
import time
import random
from dask import delayed, compute, visualize
zs = []
#delayed
def inc(x):
time.sleep(random.random())
return x + 1
#delayed
def dec(x):
time.sleep(random.random())
return x - 1
#delayed
def add(x, y):
time.sleep(random.random())
return x + y
def myloop(x):
x.append([add(inc(i), dec(inc(i))) for i in range(8)])
return x
result = myloop(zs)
final = compute(*result)
print(final)
I have tried printing out 'result' (function call) which provides the expected list of delay calls but when I print the results of 'compute' I unexpectedly get the desired list as part of a tuple. Why don't I get a just a list?
When I try to 'visualize' the execution graph I get nothing at all. I was expecting to see as many nodes as are in the generated list.
I did not think I made any significant modifications to the example so what am I not understanding?
The visualize function has the same call signature as compute. So if your compute(*result) call works then try visualize(*result)

How to solve project Euler #12 in Lua?

Ok, here it goes another Euler problem question.
I've started to learn Lua by solving Euler project problems and got stuck on Euler problem 12.
It looks to me very straightforward and I don't understand why is my result incorrect?
Here is my solution so far:
-- return triangular number of the specified number
function get_tri_num(num)
local n = 0
for i=1, num do
n = n + i
end
return n
end
-- return all factors of the specifeid number
function factors(num)
local factors = {}
for i=1, num/2 do
if num%i == 0 then
factors[#factors+1] = i
end
end
factors[#factors+1] = num
return factors
end
-- get the first triangle number with >500 divisors
function euler12()
local n = 0
local trinum = 1
while true do
n = n + 7
trinum = get_tri_num(n)
if #factors(trinum) > 500 then break end
end
print(trinum, n)
end
euler12()
This problem is computation intensive, well, at least the way I am solving it, so I use luajit.
time luajit euler12.lua
103672800 14399
real 3m14.971s
user 3m15.033s
sys 0m0.000s
First, I try this solution on the toy example provided in the problem description. Changing the line of euler12() to if #factors(trinum) > 5 then break end, I get:
28 7
Which corresponds to the results shown in the problem example.
Second, after I see that the toy example is working I run euler12() with >500 condition. According to my solution the answer is 103672800 and yes, if I separately check the number of divisors for this result is >500:
print(#factors(103672800))
648
But...
The problem is here:
while true do
n = n + 7
Why does n increaments 7 each time? That doesn't make sense, change it to 1, and you could get the correct answer.
However, the performance is still poor. Several places that could be improved:
Every time the function get_tri_num is called, it's calculating
from scratch, that's not necessary.
You don't need the factors of a number, you only need the number of
factors of a number, so why return a table in factors?
for i=1, num/2 do is not necessary. Iterating to the square root of
num is enough to get the number of factors.
Refer to my code for the same problem.

Return multiple values to a function, and access them separately in Lua?

If I have a function that returns multiple values, how can I access those values separately? Something like table[i].
angles = function()
x = function()
local value = 0
return value
end
y = function()
local value = 90
return value
end
z = function()
local value = 180
return value
end
return x(), y(), z()
end
A problem arises here when wanting to use, for example, the x value separately, while keeping it in the function angles.
print(????)
Sort of wish functions worked like tables in this respect, so I could type something like print(angles.x)
Also, I know that code seems really redundant, but it's actually a much more simplified version of what I'm actually using. Sorry if it makes less sense that way.
x, y, z= angles()
print (x,y,z)
There's a couple of ways to do this.
Most obvious would be
local x, y, z = angles()
print(x)
If you want the first value specifically
local x = ( angles() )
-- `local x = angles()` would work too. Lua discards excess return values.
print(x)
or, somewhat less readably
print((angles()))
You could also return a table from the function, or use the standard module table to pack the return values into one.
local vals = table.pack(angles())
print(vals[1])
Another way of accessing them individually (as the question wording implies) rather than all at once is this:
print((select(1,angles())))
print((select(2,angles())))
print((select(3,angles())))
Output:
0
90
180
The select() call needs to be in parentheses in order to return individual entries rather than all after the given offset.

Resources