Exploring Effects of Commenting Writes in FORTRAN SDP

In summary: Good luck with your program!In summary, the conversation discusses a program written in FORTRAN that involves a stochastic dynamic program with a Markov process. The program outputs an array based on different variables and the different outputs were observed when a specific write statement was commented out or not. The expert suggests that the issue may be due to the array not being initialized to a specific value and recommends doing so as a solution. The conversation ends with the expert wishing the other person good luck with their program.
  • #1
kingfisher
2
0
I have a stochastic dynamic program with markov process written in FORTRAN, below. If you comment out the write statement that says "uh, THIS SHOULD MAKE NO DIFFERENCE" and/or some of the other write statements that are just text, no variables being written, you will see a difference in the final array output as compared to when you do not comment any out.

experiment with commenting some of the write statements out/bringing them back in and look at the output array.

can anyone explain this? it happens on more than one computer.


PROGRAM HW4A
IMPLICIT NONE
REAL :: S !current storage
REAL :: LI !previous inflow
REAL :: R !planned current release
REAL :: P !penalty based on planned current release
REAL :: oops !cost of releasing less than planned release
REAL :: f !future costs sum
REAL :: OFP !optimum future costs
REAL :: OCR !optimum current release

INTEGER :: t,st,lin,cin,fst,re !counter indexes for loops and array addresses
!stage, storage, last inflow, future storage, release

!row is current inflow, column is current storage
REAL, DIMENSION(5,0:7) :: Of !best future costs for storage and inflow
REAL, DIMENSION(5,0:7) :: OR !best release for storage and inflow

REAL, DIMENSION(5) :: PIC !probabilities of next inflows




t=20
! WRITE (*,*)"Number of Stages to run?"
! READ(*,*)t
! WRITE (*,*)" Based on",t,"year SDP"
DO !for each t
st=0 !reset current storage


DO !for each S
lin=1 !reset previous inflow


DO !for each I
re=0 !reset current release
LI=lin*10.
OFP=9999999 !reset optimum future cost
CALL INFLOWPROB(LI,PIC)

DO !for each R
R=re*10.
CALL PENALTY (R,P)
f=0 !reset future costs accumulator
cin=1 !reset current inflow

DO !for each current inflow
fst=st-re+cin
IF(fst<0)THEN
oops=5000
fst=0
ELSE
oops=0
END IF
IF(fst>7)fst=7
f=f+PIC(cin)*(Of(cin,fst)+oops)
IF(cin==5)EXIT
cin=cin+1
END DO

IF(OFP>P+f)THEN
OFP=P+f
OCR=R
END IF
IF(re==5)EXIT
re=re+1

END DO
Of(lin,st)=OFP
OR(lin,st)=OCR
! WRITE(*,*)Of(3,3)
IF(lin==5)EXIT
lin=lin+1

END DO

IF(st==7)EXIT
st=st+1

END DO
! WRITE (*,*)" ____________STORAGE____________"
! WRITE (*,*)" 0 10 20 30 40 50 60 70"
! WRITE (*,*)" ________________________________"
! WRITE (*,*)"I 10:",NINT(Of(1,:))
! WRITE (*,*)"N 20:",NINT(Of(2,:))
! WRITE (*,*)"F 30:",NINT(Of(3,:))
! WRITE (*,*)"L 40:",NINT(Of(4,:))
! WRITE (*,*)"O 50:",NINT(Of(5,:))
! WRITE(*,100)Of
!100 FORMAT(1X,5(8F10.0,/),/)
IF(t==1)EXIT
t=t-1
END DO


write(*,*)"uh, THIS SHOULD MAKE NO DIFFERENCE!"
WRITE (*,*)" Advised Releases based on"
WRITE (*,*)" Last Season Inflow and Current Storage"
WRITE (*,*)" ____________STORAGE____________"
WRITE (*,*)" 0 10 20 30 40 50 60 70"
WRITE (*,*)" ________________________________"
WRITE (*,*)"I 10:",OR(1,:)
WRITE (*,*)"N 20:",OR(2,:)
WRITE (*,*)"F 30:",OR(3,:)
WRITE (*,*)"L 40:",OR(4,:)
WRITE (*,*)"O 50:",OR(5,:)


END PROGRAM HW4A

!
SUBROUTINE PENALTY(Q,P)
REAL, INTENT(IN) :: Q
REAL, INTENT(OUT) :: P
REAL :: a = 10
REAL :: b = 2
REAL :: T = 30

! penalty function
IF(Q<T)THEN
P = a*T*(EXP(b*(T-Q)/T)-1)
ELSE
P=0
END IF


END SUBROUTINE PENALTY


!
SUBROUTINE INFLOWPROB(LI,PIC)
REAL, INTENT(IN) :: LI !previous inflow
REAL, DIMENSION(5), INTENT(OUT) :: PIC !current inflow probability

IF(LI == 10)THEN
PIC(1) = 0.8
PIC(2) = 0.1
PIC(3) = 0.1
PIC(4) = 0
PIC(5) = 0
ELSE IF(LI == 20)THEN
PIC(1)= 0.3
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.1
PIC(5) = 0
ELSE IF(LI == 30)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 40)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 50)THEN
PIC(1) = 0.1
PIC(2) = 0.2
PIC(3) = 0.3
PIC(4) = 0.3
PIC(5) = 0.1
END IF

END SUBROUTINE INFLOWPROB
 
Technology news on Phys.org
  • #2


Hi kingfisher,

One line that I noticed was this one:

Code:
f=f+PIC(cin)*(Of(cin,fst)+oops)

I was looking at the array Of(cin,fst) and it does not appear to be initialized to any value before it is used. Were you wanting to use array Of here? I think some compilers automatically set things to zero if they are not explicitly initialized, while others just use whatever (garbage) value happens to be at that memory location. Or am I misreading your program?

I don't know how that would give the behavior you are talking about though. Can you give the different outputs that you are getting when the write statement is commented out and active? Also, what compiler are you using?
 
  • #3


YOU ARE GREAT, AND I THANK YOU

initializing the array was the solution
 
  • #4


Glad to help! That was an interesting bug; I actually got identical outputs with my compiler whether the write statement was commented out or not, so I could not reproduce the behavior.

But I'm guessing that your compiler chose a different memory location for the variable array Of(,) based on whether the write line was there or not, which caused different "random" values to be used.
 

What is FORTRAN SDP?

FORTRAN SDP stands for FORTRAN Self-Describing Program and is a programming language used for scientific and numerical computations. It was introduced in the 1950s and is still widely used in fields such as physics and engineering.

What is the purpose of "Exploring Effects of Commenting Writes" in FORTRAN SDP?

The purpose of this project is to investigate the impact of adding comments to FORTRAN SDP code. Comments are used to provide explanations and descriptions of the code, and this study aims to determine how they affect the performance and readability of the program.

What are the potential benefits of commenting writes in FORTRAN SDP?

Commenting writes in FORTRAN SDP can improve the overall organization and clarity of the code. It can also make the code more accessible to others, as comments can help readers understand the purpose and logic behind the program.

What factors will be considered in the exploration of commenting writes in FORTRAN SDP?

The study will consider factors such as the type and amount of comments, the size and complexity of the program, and the experience level of the programmer. These variables will be analyzed to determine their effect on the code's performance and readability.

Why is it important to understand the effects of commenting writes in FORTRAN SDP?

Understanding the effects of commenting writes in FORTRAN SDP can help programmers make informed decisions about when and how to use comments in their code. This knowledge can lead to more efficient and organized programs, making it easier for others to understand and modify the code in the future.

Similar threads

  • Programming and Computer Science
Replies
4
Views
498
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top