Welcome to WEAP's Website WEAP
WEAP is an initiative of the Stockholm Environment Institute.

About WEAP

What's New?
Sample Screens
History and Credits

Using WEAP
User Guide
Videos (YouTube)

User Forum
Members List
Edit Profile

Additional Support
University Courses

About Us
SEI-US Water Resources Program
Please Contact Us

Environmental News
Not acting like themselves: Antidepressants in environment alter crayf...
Expose crayfish to antidepressants, and they become more outgoing -- but that might not be such a positive thing for the...

Use of PFAS in cosmetics 'widespread,' new study finds
Many cosmetics sold in the United States and Canada likely contain high levels of per- and polyfluoroalkyl substances (P...

Baltic herring larvae appear earlier and grow faster due to climate ch...
Conditions during Baltic herring spawning may have cascading effects on the whole Baltic ecosystem.
Source: Science Daily
Interested in Energy?
Read about LEAP: SEI's software for energy planning.

Link WEAP and LEAP for combined Water-Energy planning.
Watch a video demo!

User Forum

All Topics | Topic "Using Randomize() in script"
Log in to post new messages or reply to existing messages.
Author Message
Mr. Juan Sebastian Ossa Moreno

Subject: Using Randomize() in script   
Posted: 10/15/2017 Viewed: 5505 times
I am trying to calibrate a WEAP model by randomly changing some parameters in a script. However, for some reason the parameters do not change after each iteration when i use Rnd(). Furthermore, i would like to have the same parameter for the two scenarios I have (for the same iteration only of course), however, the code generates different numbers for each scenario for each iteration. This is a section of my code.

Does anyone know why this is happening or where the error is?
Thanks in advance

WEAP.Verbose = 1
WEAP.Logfile = WEAP.Directory + "WeapErrors.txt"

WEAP.ActiveArea = "UpperAco"
WEAP.ActiveScenario="Current Accounts"

WEAP.Branch("Key\NewSnowAlbedo").Variables("Annual Activity Level").Expression = Rnd()*0.5+0.5

FOR i= 1 to 2

WEAP.Branch("Key\OldSnowAlbedo").Variables("Annual Activity Level").Expression = Rnd()*0.5
...other changes in parameters...

CALL WEAP.LoadFavorite("JuncalObsSim%")
CALL WEAP.ExportResults("C:\Users\uqjossam\Desktop\SMI\Data&Models\New folder\InputData\ScriptCalibration\" + FormatNumber(i, 0) + ".csv", TRUE, TRUE,TRUE)

CALL WEAP.PrintToFile("C:\Users\uqjossam\Desktop\SMI\Data&Models\New folder\InputData\ScriptCalibration\BorrarAlbedo.txt",WEAP.Branch("Key\OldSnowAlbedo").Variables("Annual Activity Level").Value(2009,1))
...Print of other variables...


Mr. Jack Sieber

Subject: Re: Using Randomize() in script   
Posted: 10/19/2017 Viewed: 5497 times
Randomize() and Rnd() are Visual Basic functions, not WEAP functions. I do not know why you are not getting different random values each iteration.

WEAP does have a Random function, which generates a random time series, but it will be the same time series each run, for a given branch, variable and scenario. You could give it a different Seed value for each iteration, so that the two iterations had different random time series. However, from your code it looks like you want a single random number for OldSnowAlbedo, not a time series of random numbers.

Mr. Juan Sebastian Ossa Moreno

Subject: Re: Using Randomize() in script   
Posted: 10/20/2017 Viewed: 5487 times
Hello Jack

Thanks for your answer.

I know that Randomize() and Rnd() are VB functions thus i run the lines in a very similar context in VB Excel, and it worked fine there, but i still have the problems when using them in WEAP. Also, i saw that other users have used the functions without problems, thus i guess it should be a specific problem of my computer. I gave up with VB and moved to Python, which i know a bit more and managed to solve the problem.

However, am still having problems to define variables for all scenarios with a single line, is this possible? This could help me saving some time in each computation am doing to calibrate my model.

Currently i have something like this

import win32com.client, os, random, numpy


WEAP.ActiveScenario="Current Accounts"

WEAP.Branch("Key\NewSnowAlbedo").Variables("Annual Activity Level").Expression = 0.8
WEAP.Branch("Key\OldSnowAlbedo").Variables("Annual Activity Level").Expression = 0.3

WEAP.Branch("Key\NewSnowAlbedo").Variables("Annual Activity Level").Expression = 0.8
WEAP.Branch("Key\OldSnowAlbedo").Variables("Annual Activity Level").Expression = 0.3

and then i use a similar structure inside a foor loop to set the parameters of each one of the calibrations. Is there any alternative that is more efficient?

Thanks in advance.



Mr. Jack Sieber

Subject: Re: Using Randomize() in script   
Posted: 10/20/2017 Viewed: 5481 times
If you do not enter an expression for a scenario, WEAP will use the expression from its parent -- either the scenario it is derived from or the Current Accounts. Therefore, as long as you have not put an expression into NewSnowAlbedo or OldSnowAlbedo for the scenarios, they will automatically inherit and use the expression you set for the Current Accounts. (If you have set an expression for the scenario, just delete it and WEAP will revert to using the parent's expression.)

Topic "Using Randomize() in script"