Minimax: Should the evaluation function be different in different turns? - algorithm

I am doing an AI using Minimax for dots and boxes. After the most of the work, I try some different evaluation function to find the most suitable one. But I get confused with the evaluation function one(below), for it make some stupid moves(like the third edge of a box when there are boxes who have all four edges unmade), and use the same evaluation like function two don't make that errors. And that is contradictory with this answer .
And this is function one for dots and boxes:
//System.out.println("in evaluate" );
if (node.ai.isTurn) {
value = 4*node.ai.score - node.man.score * 2 + 0.5 * s2 - 0.75 * s3 ;
}else {
value = 4*node.ai.score - node.man.score * 2 - 0.5 * s2 + 0.75 * s3 ;
}
//System.out.println(value);
return value;
And function two:
value = 4*node.ai.score - node.man.score * 2 + 0.5 * s2 - 0.75 * s3 ;
So could anyone tell me should the evaluation function be different in max turn and min turns. And any suggestions are appreciated.

Related

How the property of modulous (A*B)%m = (A%m * B%m) %m is used to find the mod of very large numbers

I saw the property of mod where
(A*B)%m = (A%m * B%m) %m
And this property is used in the below algorithm to find the mod of very large numbers.
Get one variable to store the answer initialized to zero.
Scan the string from left to right,
every time multiply the answer by 10 and add the next number and take the modulo and store this as the new answer.
But I'm unable to understand this algorithm . How the property is connected to the algorithm here?
It will be helpful if used an example to understand the underneath math behind the algorithm , for example 12345%100
Using this algorithm, 23 % k is computed as
(2%k * 10 + 3)%k
((2%k * 10)%k + 3)%k // because (a+b)%k = (a%k + b)%k (1)
(((2%k)%k * 10%k)%k + 3)%k // because (a*b)%k = (a%k * b%k)%k (2)
((2%k * 10%k)%k + 3)%k // because (a%k)%k = a%k (trivial)
((2 * 10)%k + 3)%k // because (a%k * b%k)%k = (a*b)%k (2)
(2 * 10 + 3)%k // because (a%k + b)%k = (a+b)%k (1)
23%k
In other words, (a%k * p + b)%k = (a * p + b)%k thanks to that property (2). b is the last digit of a number in base p (p = 10 in your example), and a is the rest of the number (all the digits but the last).
In my example, a is just 2, but if you apply this recursively, you have your algorithm. The point is that a * p + b might be too big to handle, but a%k * p + b probably isn't.

Why does finding the eigenvalues of a 4*4 matrix by z3py take so much time and do not give any solutions?

I'm trying to calculate the eigenvalues of a 4*4 matrix called A in my code (I know that the eigenvalues are real values). All the elements of A are z3 expressions and need to be calculated from the previous constraints. The code below is the last part of a long code that tries to calculate matrix A, then its eigenvalues. The code is written as an entire but I've split it into two separate parts in order to debug it: part 1 in which the code tries to find the matrix A and part 2 which is eigenvalues' calculation. In part 1, the code works very fast and calculates A in less than a sec, but when I add part 2 to the code, it doesn't give me any solutions after.
I was wondering what could be the reason? Is it because of the order of the polynomial (which is 4) or what? I would appreciate it if anyone can help me find an alternative way to calculate the eigenvalues or give me some hints on how to rewrite the code so it can solve the problem.
(Note that A2 in the actusl code is a matrix with all of its elements as z3 expressions defined by previous constraints in the code. But, here I've defined the elements as real values just to make the code executable. In this way, the code gives a solution so fast but in the real situation it takes so long, like days.
for example, one of the elements of A is almost like this:
0 +
1*Vq0__1 +
2 * -Vd0__1 +
0 +
((5.5 * Iq0__1 - 0)/64/5) *
(0 +
0 * (Vq0__1 - 0) +
-521702838063439/62500000000000 * (-Vd0__1 - 0)) +
((.10 * Id0__1 - Etr_q0__1)/64/5) *
(0 +
521702838063439/62500000000000 * (Vq0__1 - 0) +
0.001 * (-Vd0__1 - 0)) +
0 +
0 + 0 +
0 +
((100 * Iq0__1 - 0)/64/5) * 0 +
((20 * Id0__1 - Etr_q0__1)/64/5) * 0 +
0 +
-5/64
All the variables in this example are z3 variables.)
from z3 import *
import numpy as np
def sub(*arg):
counter = 0
for matrix in arg:
if counter == 0:
counter += 1
Sub = []
for i in range(len(matrix)):
Sub1 = []
for j in range(len(matrix[0])):
Sub1 += [matrix[i][j]]
Sub += [Sub1]
else:
row = len(matrix)
colmn = len(matrix[0])
for i in range(row):
for j in range(colmn):
Sub[i][j] = Sub[i][j] - matrix[i][j]
return Sub
Landa = RealVector('Landa', 2) # Eigenvalues considered as real values
LandaI0 = np.diag( [ Landa[0] for i in range(4)] ).tolist()
ALandaz3 = RealVector('ALandaz3', 4 * 4 )
############# Building ( A - \lambda * I ) to find the eigenvalues ############
A2 = [[1,2,3,4],
[5,6,7,8],
[3,7,4,1],
[4,9,7,1]]
s = Solver()
for i in range(4):
for j in range(4):
s.add( ALandaz3[ 4 * i + j ] == sub(A2, LandaI0)[i][j] )
ALanda = [[ALandaz3[0], ALandaz3[1], ALandaz3[2], ALandaz3[3] ],
[ALandaz3[4], ALandaz3[5], ALandaz3[6], ALandaz3[7] ],
[ALandaz3[8], ALandaz3[9], ALandaz3[10], ALandaz3[11]],
[ALandaz3[12], ALandaz3[13], ALandaz3[14], ALandaz3[15] ]]
Determinant = (
ALandaz3[0] * ALandaz3[5] * (ALandaz3[10] * ALandaz3[15] - ALandaz3[14] * ALandaz3[11]) -
ALandaz3[1] * ALandaz3[4] * (ALandaz3[10] * ALandaz3[15] - ALandaz3[14] * ALandaz3[11]) +
ALandaz3[2] * ALandaz3[4] * (ALandaz3[9] * ALandaz3[15] - ALandaz3[13] * ALandaz3[11]) -
ALandaz3[3] * ALandaz3[4] * (ALandaz3[9] * ALandaz3[14] - ALandaz3[13] * ALandaz3[10]) )
tol = 0.001
s.add( And( Determinant >= -tol, Determinant <= tol ) ) # giving some flexibility instead of equalling to zero
print(s.check())
print(s.model())
Note that you seem to be using Z3 for a type of equations it absolutely isn't meant for. Z is a sat/smt solver. Such a solver works internally with a huge number of boolean equations. Integers and fractions can be converted to boolean expressions, but with general floats Z3 quickly reaches its limits. See here and here for a lot of typical examples, and note how floats are avoided.
Z3 can work in a limited way with floats, converting them to fractions, but doesn't work with approximations and accuracies as in needed in numerical algorithms. Therefore, the results are usually not what you are hoping for.
Finding eigenvalues is a typical numerical problem, where accuracy issues are very tricky. Python has libraries such as numpy and scipy to efficiently deal with those. See e.g. numpy.linalg.eig.
If, however your A2 matrix contains some symbolic expressions (and uses fractions instead of floats), sympy's matrix functions could be an interesting alternative.

How can I create (approximately) the double sigmoid in the shown figure?

I want to create (approximately) the double sigmoid in the shown figure as
a function in terms of the parameters X,Y,Z, a,b,c and d.
Any idea? Thanks.
This question seems to have gone ignored, so try something like this:
k = 1 # adjust this for "sharpness"
s(x) = (tanh(k * x) + 1) / 2
f(x) = X + (Y-X) * s(x-b) + (Z-Y) * s(x-c)
Here's an example plot.

Derivative of sigmoid

I'm creating a neural network using the backpropagation technique for learning.
I understand we need to find the derivative of the activation function used. I'm using the standard sigmoid function
f(x) = 1 / (1 + e^(-x))
and I've seen that its derivative is
dy/dx = f(x)' = f(x) * (1 - f(x))
This may be a daft question, but does this mean that we have to pass x through the sigmoid function twice during the equation, so it would expand to
dy/dx = f(x)' = 1 / (1 + e^(-x)) * (1 - (1 / (1 + e^(-x))))
or is it simply a matter of taking the already calculated output of f(x), which is the output of the neuron, and replace that value for f(x)?
Dougal is correct. Just do
f = 1/(1+exp(-x))
df = f * (1 - f)
The two ways of doing it are equivalent (since mathematical functions don't have side-effects and always return the same input for a given output), so you might as well do it the (faster) second way.
A little algebra can simplify this so that you don't have to have df call f.
df = exp(-x)/(1+exp(-x))^2
derivation:
df = 1/(1+e^-x) * (1 - (1/(1+e^-x)))
df = 1/(1+e^-x) * (1+e^-x - 1)/(1+e^-x)
df = 1/(1+e^-x) * (e^-x)/(1+e^-x)
df = (e^-x)/(1+e^-x)^2
You can use the output of your sigmoid function and pass it to your SigmoidDerivative function to be used as the f(x) in the following:
dy/dx = f(x)' = f(x) * (1 - f(x))

Converting an algerberic expression into a function?

I have been trying to create an expression to calculate brick and glass prices.
I'm working with a, b and c
so the price is $30m^2 for bricks and $20m^2 for glass
A and B are walls C is a round window radius
A = 3m (not m^2)
B = 2m (not m^2)
C = 1m (not m^2)
I believe that my expression (a*b)*30 – (c^2*20) works but how can I turn this into a java script function that in future I could use in a calculator to calculate prices etc...
Bit of a newbie with java script.
thanks all!
I believe the following function cost(a,b,c) should do what you want, assuming I've understood the question:
function cost(a,b,c) {
return (a * b * 30) - (Math.PI * Math.pow(c,2) * 20)
}
This returns 117.16814692820414 given the parameters a = 3, b = 2 and a = 1.
An example of the cost function in use would be:
var price = cost(3,2,1);

Resources