I know that people ask a LOT of segmentation err questions here, but I've put my effort in solving this problem for more than three hour and still wasn't able to solve this. :/ So here is my code:
c sinle event analysis
implicit real(a-h,o-z)
real day(12), nmonth(12), year(12), clas(12),
$ hour(12), nmin(12)
integer mark(12)
real tst(12), D(12), avgP(12,6), avgA(12,6)
integer k, m, n, g
real time(2054904), proa(2054904), prob(2054904), w1(2054904),
$ w2(2054904), w3(2054904), w4(2054904)
D(1) = 31; D(2) = 28; D(3) = 31; D(4) = 30; D(5) = 31;
D(6) = 30; D(7) = 31; D(8) = 31; D(9) = 30; D(10) = 31;
D(11) = 30; D(12) = 31
open(100,file='singleE.dat')
do i=1, 12
tst(i)=0
enddo
900 do i=1, 12
read(100, 1150) day(i), nmonth(i), year(i),
$ hour(i), nmin(i), clas(i)
do j=1, 12
if (int(nmonth(i)).EQ.(13-j)) then
tst(i) = tst(i) + D(12-j)
nmonth(i) = nmonth(i)-1
endif
enddo
tst(i) = tst(i) + day(i) + (year(i) - 2010)*365
$ + (hour(i) + nmin(i)/60)/24
if (year(i) > real(2011)) then
tst(i) = tst(i) + 1
endif
enddo
open(200,file='hole.dat',status='OLD')
k = 0
do i=1, 2054904
read(200,950) time(i), proa(i), prob(i),
$ w1(i), w2(i), w3(i), w4(i)
enddo
mark = 0
do i=1, 12
do j=1, 2054904
k = k + 1
if(abs(tst(i)-time(j))<0.0001) then
mark(i) = k
endif
enddo
enddo
n = 5;
do i= 1, 12
do j= 1,6
avgP(i,j) = 0
avgA(i,j) = 0
enddo
enddo
do i=1, 12
if (mark(i).EQ.0) then
go to 750
endif
do j = (mark(i)-(n+1)*1440), (mark(i)-n*1440)
avgP(i,1) = avgP(i,1) + proa(j)
avgP(i,2) = avgP(i,2) + prob(j)
avgP(i,3) = avgP(i,3) + w1(j)
avgP(i,4) = avgP(i,4) + w2(j)
avgP(i,5) = avgP(i,5) + w3(j)
avgP(i,6) = avgP(i,6) + w4(j)
enddo
do g = (mark(i)+n*1440), (mark(i)+(n+1)*1440)
avgA(i,1) = avgA(i,1) + proa(g)
avgA(i,2) = avgA(i,2) + prob(g)
avgA(i,3) = avgA(i,3) + w1(g)
avgA(i,4) = avgA(i,4) + w2(g)
avgA(i,5) = avgA(i,5) + w3(g)
avgA(i,6) = avgA(i,6) + w4(g)
enddo
750 print *, avgP(i,1), avgP(i,2), avgP(i,3), avgP(i,4),
$ avgP(i,5), avgP(i,6)
enddo
850 close(i)
950 FORMAT(F12.7,2x,E10.3,2x,E10.3,2x,E10.3,2x,E10.3,
$ 2x,E10.3,2x,E10.3)
1150 FORMAT(F2.0,1x,F2.0,1x,F4.0,1x,F2.0,1x,F2.0,4x F3.1)
end
The part that is causing me trouble is the loop here:
do i=1, 12
if (mark(i).EQ.0) then
go to 750
endif
do j = (mark(i)-(n+1)*1440), (mark(i)-n*1440)
avgP(i,1) = avgP(i,1) + proa(j)
avgP(i,2) = avgP(i,2) + prob(j)
avgP(i,3) = avgP(i,3) + w1(j)
avgP(i,4) = avgP(i,4) + w2(j)
avgP(i,5) = avgP(i,5) + w3(j)
avgP(i,6) = avgP(i,6) + w4(j)
enddo
do g = (mark(i)+n*1440), (mark(i)+(n+1)*1440)
avgA(i,1) = avgA(i,1) + proa(g)
avgA(i,2) = avgA(i,2) + prob(g)
avgA(i,3) = avgA(i,3) + w1(g)
avgA(i,4) = avgA(i,4) + w2(g)
avgA(i,5) = avgA(i,5) + w3(g)
avgA(i,6) = avgA(i,6) + w4(g)
enddo
enddo
Using gdb, I found out that the 'j' loop is causing trouble. All of the parameters are fine, but every time I execute the program the 'j' loop goes only once. The peculiar thing is that as 'i' increases the processes start to cripple one by one. for example, at i = 1 the loop executes well. Then, at i = 2, avgP(i,6) = avgP(i,6) + w4(j) causes the seg fault. At i = 3, avgP(i,5) = avgP(i,5) + w3(j) causes the seg fault and in the end at i = 7, entire loop doesn't work. What a strange error! Hope that I can get some help with this.
The loop
do j=1, 12
if (int(nmonth(i)).EQ.(13-j)) then
tst(i) = tst(i) + D(12-j)
nmonth(i) = nmonth(i)-1
endif
enddo
potentially tries to access the value D(0) when j=12 however D is dimensioned 1:12 so who knows what is being written to tst and its consequences.
This should be caught by turning on bounds checking.
Related
I am trying to get all solutions for a Mixed Integer program through ortools. I have two lists x and y of size 4. I want to get all solutions which satisfy sum(x) = 4 * sum(y). I created a function which takes list of past solutions as input and returns next solution. I am able to get only 2 solutions even though there are more. What am I doing wrong here?
I am expecting the following solutions
Solution 1:
xs1 = [0,0,0,0], ys1 = [0,0,0,0]
Solution 2:
xs2 = [4,0,0,0], ys2 = [1,0,0,0]
Solution 3:
xs3 = [0,4,0,0], ys3 = [1,0,0,0]
Solution 4:
xs4 = [0,0,4,0], ys4 = [0,0,1,0]
and soon on
from ortools.linear_solver import pywraplp
def opt(xs, ys):
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()
# x and y are integer non-negative variables.
n = 4
M = 20
x = [0]* n
y = [0]* n
w = [[0]* n]*len(xs)
δ = [[0]* n]*len(xs)
for i in range(0,n):
x[i] = solver.IntVar(0, 20, 'x'+str(i))
y[i] = solver.IntVar(0, 20, 'y'+str(i))
for j in range(len(xs)):
w[j][i] = solver.IntVar(0, 20, 'zp'+str(j)+ '-' + str(i))
δ[j][i] = solver.IntVar(0, 1, 'δ'+str(j)+ '-' + str(i))
for j in (range(len(xs))):
for i in range(0,n):
solver.Add((w[j][i] - x[i] + xs[j][i]) >=0)
solver.Add((w[j][i] - x[i] + xs[j][i]) <= M*(1-δ[j][i]))
solver.Add((w[j][i] + x[i] - xs[j][i]) >=0)
solver.Add((w[j][i] + x[i] - xs[j][i]) <= M*δ[j][i])
for j in range(len(xs)):
solver.Add(solver.Sum([w[j][i] for i in range(0,n)]) >= 1)
solver.Add(solver.Sum([x[i] for i in range(0, n)]) - 4 * solver.Sum([y[i] for i in range(0, n)]) == 0)
solver.Minimize(solver.Sum([x[i] for i in range(0, n)]))
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
solver_x = [0]*n
solver_y = [0]*n
for i in range(0,n):
solver_x[i] = x[i].solution_value()
solver_y[i] = y[i].solution_value()
return ([solver_x, solver_y, solver.Objective().Value()])
else:
print('No Solution')
return ([[0], [0]], -1)
psx = [[0,0,0,0], [0,4,0,0]]
psy = [[0,0,0,0], [1,0,0,0]]
ns = opt(psx, psy)
print(ns)
Output:
No Solution
([[0], [0]], -1)
Reference:
Finding multiple solutions to general integer linear programs
How to write constraints for sum of absolutes
If you have a pure integer programming model, you can use the CP-SAT solver which allows you to print all the solutions [See this].
This is my code, I want to make it parallel with OpenMP. I have one main loop to make parallel and some inner loops.
Are the indices of inner loops, like p, i or Li private or shared?
What happend if I do not declare the variables as private or shared?
Do you suggest to use the allocatable variables for this parallel loop?
!$OMP PARALLEL DO
do l = 1,n_rep
do p = 1,n_l - 1
do q = 1,n_l - 1
do r = 1,n_l - 1
Li = (p - 1)*(n_l - 1)**2 + (q - 1)*(n_l - 1) + r
alpha(Li) = pi*rand()
gamma(Li) = pi*rand()
beta(Li) = pi/2*rand()
R_x(1,1) = 1.d0
R_x(1,2) = 0.d0
R_x(1,3) = 0.d0
R_x(2,1) = 0.d0
R_x(2,2) = cos(alpha(Li))
R_x(2,3) = sin(alpha(Li))
R_x(3,1) = 0.d0
R_x(3,2) = -sin(alpha(Li))
R_x(3,3) = cos(alpha(Li))
R_y(1,1) = cos(beta(Li))
R_y(1,2) = 0.d0
R_y(1,3) = -sin(beta(Li))
R_y(2,1) = 0.d0
R_y(2,2) = 1.d0
R_y(2,3) = 0.d0
R_y(3,1) = sin(beta(Li))
R_y(3,2) = 0.d0
R_y(3,3) = cos(beta(Li))
R_z(1,1) = cos(gamma(Li))
R_z(1,2) = sin(gamma(Li))
R_z(1,3) = 0.d0
R_z(2,1) = -sin(gamma(Li))
R_z(2,2) = cos(gamma(Li))
R_z(2,3) = 0.d0
R_z(3,1) = 0.d0
R_z(3,2) = 0.d0
R_z(3,3) = 1.d0
R_xy = matmul(R_x,R_y)
R_xyz = matmul(R_xy,R_z)
do i = 1,n_f - 1
do j = 1,n_f - 1
do k = 1,n_f - 1
Li = (i - 1)*(n_f - 1)**2 + (j - 1)*(n_f - 1) + k
cf_x(i) = x_f(i) + (p - 1)*d_l - x_c(p)
cf_y(j) = y_f(j) + (q - 1)*d_l - y_c(q)
cf_z(k) = z_f(k) + (r - 1)*d_l - z_c(r)
x_rotated = R_xyz(1,1)*cf_x(i) + R_xyz(1,2)*cf_y(j) &
+ R_xyz(1,3)*cf_z(k)
y_rotated = R_xyz(2,1)*cf_x(i) + R_xyz(2,2)*cf_y(j) &
+ R_xyz(2,3)*cf_z(k)
z_rotated = R_xyz(3,1)*cf_x(i) + R_xyz(3,2)*cf_y(j) &
+ R_xyz(3,3)*cf_z(k)
enddo
enddo
enddo
enddo
enddo
enddo
enddo
!$OMP END PARALLEL DO
Personally I would break this problem up a bit.
Size_of_Array = n_l * n_l * n_l
IF(ALLOCATED(Li)) DEALLOCATE( Li )
ALLOCATE( Li (Size_of_Array))
IF(ALLOCATED(Alpha)) DEALLOCATE( Alpha)
ALLOCATE (Alpha (Size_of_Array))
IF(ALLOCATED(Beta)) DEALLOCATE( Beta )
ALLOCATE( Beta (Size_of_Array))
IF(ALLOCATED(Gamma)) DEALLOCATE( Gamma)
ALLOCATE( Gamma (Size_of_Array))
indexer = 0
do l = 1,n_rep
do p = 1,n_l - 1
do q = 1,n_l - 1
do r = 1,n_l - 1
indexer = indexer + 1
Li(Indexer) = (p - 1)*(n_l - 1)**2 + (q - 1)*(n_l - 1) + r
ENDDO
ENDDO
ENDDO
ENDDO
alpha = pi*rand()
gamma = pi*rand()
beta = pi/2*rand()
!?OMP DO PARALLEL
DO I= 1, SIZE(Li)
CALL Make_Array(Alpha(I), Beta(I), Gamma(I), MyArray(:,:,I) )
ENDDO
!etc
Basically moving the array to be inside of either an ELEMENTAL FUNCTION or a PURE SUBROUTINE. Then see what it does for speed with inlining and a single parallel do of some sort (OMP or other).
PURE SUBROUTINE Make_Array(Alpha, Beta, Gamma, MyArray)
IMPLICIT NONE
DOUBLE, INTENT(IN ) :: Alpha
DOUBLE, INTENT(IN ) :: Beta
DOUBLE, INTENT(IN ) :: Gamma
DOUBLE, DIMENSION(3,3) INTENT(INOUT) :: MyArray ! Maybe just intent(OUT)?
R_x(:,:) = 0.d0
R_x(1,1) = 1.d0
R_x(2,2) = cos(alpha)
R_x(2,3) = sin(alpha)
R_x(3,2) = -sin(alpha)
R_x(3,3) = cos(alpha)
R_y(1,1) = cos(beta)
R_y(1,3) = -sin(beta)
R_y(2,1) = 0.d0
R_y(2,2) = 1.d0
R_y(2,3) = 0.d0
R_y(3,1) = sin(beta(Li))
R_y(3,2) = 0.d0
R_y(3,3) = cos(beta(Li))
R_z(1,1) = cos(gamma(Li))
R_z(1,2) = sin(gamma(Li))
R_z(1,3) = 0.d0
R_z(2,1) = -sin(gamma(Li))
R_z(2,2) = cos(gamma(Li))
END SUBROUTINE Make_Array
Etc... For other elemental functions or pure subroutines
R_xy = matmul(R_x,R_y)
R_xyz = matmul(R_xy,R_ ...
I am having a nightmare of a time trying to run the following code with the input values below. Now when i run it i get the following error , i read some place that it was due to the fact that my 2,txt file looks messy if i dont save it in a UTF -16 bit, is that something that is possible?
At line 11 of file bndry.f (unit = 5, file = '2.txt')
Fortran runtime error: Bad real number in item 1 of list input
Main code:
IMPLICIT NONE
INTEGER MS,NS,JS,N,I
PARAMETER(MS=50000,NS=50000)
REAL*8 S,K,TAU,SIGMA,R,DELTA,SMIN,SMAX,DTAU,ALPHA,BETA,LAM
REAL*8 V(0:MS),BD(0:NS),T(0:NS)
COMMON/OUTPUT/V,BD,T
OPEN(UNIT=5, FILE='2.f')
WRITE(6,*) S,K,TAU,SIGMA,R,DELTA,DTAU
READ(5,*) S,K,TAU,SIGMA,R,DELTA,DTAU
N = TAU/DTAU
ALPHA = 2.0D0*R/SIGMA**2
BETA = 2.0D0*(R-DELTA)/SIGMA**2
LAM = (BETA-1.0D0) + DSQRT((BETA-1.0D0)**2+4.0D0*ALPHA)
LAM = LAM/2.0D0
SMIN = K/(1.0D0+1.0D0/LAM) ! PERPETUAL BOUNDARY
SMAX = 10.0D0*K
CALL EXP_DIFF(S,K,TAU,SIGMA,R,DELTA,SMAX,SMIN,DTAU,JS)
WRITE(6,*) 'PRICE: ', V(JS)
DO I = 0, N
WRITE(2,10) T(I),BD(I)
ENDDO
10 FORMAT(1X,2F14.8)
STOP
END
C=======================================================================
SUBROUTINE EXP_DIFF(S,K,TAU,SIGMA,R,DELTA,SMAX,SMIN,DTAU,JS)
IMPLICIT NONE
INTEGER MS,NS,JS,M,N,I,J,IEARLY
PARAMETER(MS=50000,NS=50000)
REAL*8 S,K,TAU,SIGMA,R,DELTA,XMIN,XMAX,DTAU,DX,ALPHA,SMIN,SMAX,
& P1,P2,P3,VC,A,B
REAL*8 VE(0:MS),V(0:MS),BD(0:NS),T(0:NS)
COMMON/OUTPUT/V,BD,T
IF (S.GT.SMAX) THEN
STOP 'THE OPTION IS WORHTLESS'
ENDIF
IF (S.LT.SMIN) THEN
STOP 'THE OPTION WORTHS K-S FOR CERTAIN'
ENDIF
XMIN = DLOG(SMIN)
XMAX = DLOG(SMAX)
DX = SIGMA*DSQRT(3.0*DTAU)
JS = (DLOG(S)-XMIN)/DX
DX = (DLOG(S)-XMIN)/FLOAT(JS)
ALPHA = R - DELTA - SIGMA**2/2.0
P1 = SIGMA**2*DTAU/(2.0*DX**2) + ALPHA*DTAU/(2.0*DX)
P2 = 1.0 - SIGMA**2*DTAU/DX**2
P3 = 1.0 - P1 -P2
P1 = P1/(1.0+R*DTAU)
P2 = P2/(1.0+R*DTAU)
P3 = P3/(1.0+R*DTAU)
WRITE(6,*) 'P1,P2,P3',P1,P2,P3
IF (P1.LT.0.0.OR.P2.LT.0.0.OR.P3.LT.0.0) STOP 'DECREASE DTAU'
M = (XMAX-XMIN)/DX
N = TAU/DTAU
IF (M.GT.MS.OR.N.GT.NS) STOP 'INCREASE MS AND NS'
DO J = 0, M
VE(J) = MAX(K-DEXP(J*DX+XMIN),0.0)
V(J) = VE(J)
ENDDO
BD(0) = K
T(0) = 0.0
DO I = 1, N
IEARLY = 0
A = V(M)
B = V(M-1)
DO J = M-1, 1, -1
VC = P1*A+P2*B+P3*V(J-1)
IF (VC.LT.VE(J).AND.IEARLY.EQ.0) THEN
BD(I) = DEXP(XMIN+J*DX)
T(I) = DTAU*DFLOAT(I)
IEARLY = 1
ENDIF
V(J) = MAX(VC, VE(J))
A = B
B = V(J-1)
ENDDO
ENDDO
RETURN
END
Data in 2.f file:
S = 100.0DO
K = 100.0D0
TAU = 3.0D0
SIGMA = 0.2D0
R = 0.08D0
DELTA = 0.04D0
DTAU = 0.03D0
JS is coming out to zero. You then divide by it.
Why is JS an INTEGER?
You're trying to read the names of the variables. The input file works fine like this:
100.0D0
100.0D0
3.0D0
0.2D0
0.08D0
0.04D0
0.03D0
And the last 0 on the first line was a capital O.
I've been trying to implement an homomorphic filter in frequency domain on both MATLAB and Python using OpenCV2 and NumPy, the MATLAB code gives the expected answer but the Python does not, the resulting image is very weird. I've tested all variables and came to the conclusion the only point there is a difference is the IFFT. On MATLAB, the results can be applied normally to the exp function and return the filtered original image expected, but the values of Python ifft are very different. I happened to see other posts with similar problems, but no satisfactory answer (perhaps i'm just bad at searching too...).
The MATLAB code
function [ img_r ] = homomorphic( img, D0, n )
[N, M] = size(img);
img_bk = double(img);
img_bk = log(img_bk+1);
img_freq = fftshift(fft2(img_bk));
magA = uint8(10*log(1+abs(img_freq)));
cu = M/2;
cv = N/2;
Hf = zeros(N,M);
for v = 1:N
dv = v - cv;
for u = 1:M
du = u - cu;
D = sqrt(du*du + dv*dv);
num = 1;
if D > 0
den = 1+((D0/D)^(2*n));
else
den = 0; %to replace +inf
end
if den ~= 0
H = num/den;
else
H = 0;
end
img_freq(v,u) = H*img_freq(v,u);
end
end
magB = uint8(10*log(1+abs(img_freq)));
img_r = (ifft2(ifftshift(img_freq)));
img_r = exp(img_r);
img_r = uint8(img_r);
and the Python code (might have some bugs but overall works)
import numpy as np
import cv2
def homomorphic(img, D0, n=2):
[N,M] = img.shape
img_bk = np.log(1 + np.float64(img))
img_freq = np.fft.fftshift(np.fft.fft2(img_bk))
cu = M/2.0
cv = N/2.0
for v in range(N):
dv = v - cv
for u in range(M):
du = u - cu
D = np.sqrt(du*du + dv*dv)
if D != 0:
a = 1.0 + (D0/D)**(2*n)
H = 1/a
else:
print D
H = 0
img_freq[v][u] = H*img_freq[v][u]
img_r = np.abs(np.fft.ifft2(np.fft.ifftshift(img_freq)))
eimg = np.exp(img_r)
eimg = np.uint8(eimg)
return eimg
I really don't get it, why the results are so different? Does anyone have any idea?
This is the code and please help I'm getting tuple index out of range error in line 19
import math
n = 7
x = 0.5,1.2,2.1,2.9,3.6,4.5,5.7
y = 3.2,5.2,9.3,14.6,20.5,30.1,45.2
xx = 3.4
yy = y[0 ]
fact = 1
for i in range(0,n):
fact = fact*(xx - x[i])
s = 0.0
i1 = i+ 2
for ip in range(0,i1):
pro = 1.0
for ir in range(0,i1):
if (ir == ip): continue
pro = pro*(x[ip] - x[ir])
s = s + y[ip]/pro
yy = yy + s*fact
print "x=%5.2f y=%5.2f" %(xx,yy)
You're setting i1 = i + 2 in your loop. Since i runs from 0 to n, i1 is set to values larger than the length of x. If you put a
print i1
before the for ip, you can see exactly where it goes wrong.