<jupyter_start><jupyter_text>Initialize script with same initial conditions as in the paper<jupyter_code># initial parameters
x = np.zeros(4)
u = np.zeros(3)
p = np.zeros(3)

# this is given by the problem data, but might be "controlled" via OPF
u[0] = 1.0 #VM1
u[1] = 1.7 #P2
u[2] = 1.0 #VM2

# these parameters are fixed through the computation
p[0] = 0.0 #VA1, slack angle
p[1] = 2.0 #P3
p[2] = 1.0 #Q3


# initial guess
x[0] = 1.0 #VM3
x[1] = 0.0 #VA3
x[2] = 0.0 #VA2
x[3] = 0.5


# print initial guesses
print(x)
print(u)
# POWER FLOW ALGO

def powerflow(x, u, p):
    
    sol = fsolve(gfun, x, args=(u,p,))
    return sol

print(powerflow(x, u, p))

print(np.linalg.cond(gfun_x(x, u, p)))

# Reduced gradient iteration

max_iter = 100
xk = np.copy(x)
uk = np.copy(u)

for i in range(max_iter):
    
    # power flow
    xk = powerflow(xk, uk, p)

    # lambda calculation
    J_x = gfun_x(xk, uk, p)
    G_x = cfun_x(xk, uk, p)
    print("condition jacobian:", np.linalg.cond(J_x))
    lam = -np.dot(inv(np.transpose(J_x)), G_x)
    
    # gradient cost function
    J_u = gfun_u(xk, uk, p)
    G_u = cfun_u(xk, uk, p)
    
    grad_c = G_u + np.dot(np.transpose(J_u), lam)
    print("Norm of gradient: ", np.linalg.norm(grad_c))
    
    # evaluate cost function
    print("Cost function: ", cfun(xk, uk, p))    
    
    # compute step
    alpha = 0.12
    uk = uk - alpha*grad_c<jupyter_output>condition jacobian: 39.59958561633352
Norm of gradient:  0.7038831242740287
Cost function:  2.1767109512499543
condition jacobian: 46.68308341866992
Norm of gradient:  0.5531510420686726
Cost function:  2.1598296133612314
condition jacobian: 44.71393962254119
Norm of gradient:  0.4450119712664789
Cost function:  2.149087109860759
condition jacobian: 49.48699914092949
Norm of gradient:  0.3506100402295171
Cost function:  2.1398376900170497
condition jacobian: 48.99354430244175
Norm of gradient:  0.2865642394328765
Cost function:  2.1329741612923963
condition jacobian: 52.204220536784455
Norm of gradient:  0.23911203526530678
Cost function:  2.127220777411127
condition jacobian: 52.614602175128354
Norm of gradient:  0.2081087508231566
Cost function:  2.122449802647705
condition jacobian: 54.89049527130433
Norm of gradient:  0.186821075858742
Cost function:  2.1183014665515123
condition jacobian: 55.76733748285113
Norm of gradient:  0.17233726046432105
Cost function:  2.1146440807183913
c[...]
StarcoderdataJupyterStructuredCleanDedup