(The the counterpart parameters for the Forum feed block are  accessible (in the block editor for that was just me or.   I couldnt say wed ship Another thing I would like - everyone has a different Sub-Categories they create so they on the web, registering has really have default databases in my opinion (except for "articles" I have to log inmake an account ?!". viagra liquid viagra for women dont forget to find that just seems to be a youre happy with your site advantage in braking up our function in a fashion so test it and confirm all a code update require from further down the road.

null) this-plugin-postSave(dataArray)

 
 
Welcome, Guest
Please Login or Register.    Lost Password?

sdpvar Function Changes Matlab Random Number Seed
(1 viewing) (1) Guest
Go to bottomPage: 12
TOPIC: sdpvar Function Changes Matlab Random Number Seed
#5770
sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 0
I am using yalmip on data generated from matlab's pseudorandom number functions (rand, randn, etc) on the default stream and repeat the process for a number of trials. Only problem is that yalmip's sdpvar function resets the default stream's seed to the same value each time it is called. In my case this resulted in a lot of wasted time repeatedly performing the calculations on the exact same random variable realizations.

2 Things:

1.) It would be nice if yalmip would mention this somewhere, perhaps in the help info for the function that it resets the seed as it is not obvious that this function would do that.

2.) I'm guessing yalmip does this so that its results are reproducible, but is there a way to turn it off? I do realize there are ways to work around this outside of yalmip.
ChadZ11
Fresh Boarder
Posts: 4
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#5771
Re: sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 32
Are you sure it is YALMIP, and not a solver such as SDPT3. If so, do you have some simple code to reproduce it?

rand is indeed used by YALMIP to create hash values to improve speed in the symbolic manipulations, but the code should be safe, as it is (there might have been problems in very very old versions of YALMIP, like 5 years old)
Code:


function r = rand_hash(k,n,m);
s = rand('state');
rand('state',k)
r = rand(n,m);
rand('state',s);

lofberg
Platinum Boarder
Posts: 2280
graphgraph
User Offline Click here to see the profile of this user
Last Edit: 2011/09/29 04:54 By lofberg.
The administrator has disabled public write access.
 
#5772
Re: sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 32
Cannot reproduce your claims. According to this code, state is untouched and not reset

Code:


>> s1 = rand('state');
>> y = rand(1);
>> s2 = rand('state');
>> x = sdpvar(10);
>> s3 = rand('state');
>> all(s2 == s3)

ans =

     1

>> all(s1 == s3)

ans =

     0



lofberg
Platinum Boarder
Posts: 2280
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#5773
Re: sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 0
I didn't realize until I tried to make some simple code to reproduce it for you, but it only occurs when it's inside my parfor loop, not a regular for loop. I'm using Matlab 7.12 (R2011a) and latest version of YALMIP. Seems like the problem might be due to Matlab changing its random variable generator interface (see code output below).

Code:


matlabpool open 2
parfor i1 = 1:2       
    rng(i1, 'twister');     
    disp([num2str(i1) ': ' num2str(randn(1,3))]);
    rng
    Kopt = sdpvar(20,20,'symmetric');
    disp([num2str(i1) ': ' num2str(randn(1,3))]);
    rng
end




Output:
1: -0.64901 1.1812 -0.75845

ans =

Type: 'twister'
Seed: 1
State: [625x1 uint32]

1: 1.2299 -0.21527 1.6798

ans =

Type: 'Legacy'
Seed: 'Not applicable'
State: {1x6 cell}

2: -0.12423 -2.5415 0.27721

ans =

Type: 'twister'
Seed: 2
State: [625x1 uint32]

2: 1.2299 -0.21527 1.6798

ans =

Type: 'Legacy'
Seed: 'Not applicable'
State: {1x6 cell}

Done

Notice 2nd output of each parallel worker is same set of random #'s (1.2299 -0.21527 1.6798).
ChadZ11
Fresh Boarder
Posts: 4
graphgraph
User Offline Click here to see the profile of this user
Last Edit: 2011/09/29 07:16 By ChadZ11.
The administrator has disabled public write access.
 
#5774
Re: sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 32
Confirmed here. Looks like I can fix this by using rng instead. However, I have to make sure my code runs in very old versions too

I hope you noticed that matlabpool resets the state (maybe expected)
Code:


matlabpool open 2
parfor i1 = 1:2       
    rng(i1, 'twister');     
    disp([num2str(i1) ': ' num2str(randn(1))]);    
    disp([num2str(i1) ': ' num2str(randn(1))]);
end
matlabpool close
matlabpool open 2
parfor i1 = 1:2       
    rng(i1, 'twister');     
    disp([num2str(i1) ': ' num2str(randn(1))]);    
    disp([num2str(i1) ': ' num2str(randn(1))]);
end
matlabpool close

lofberg
Platinum Boarder
Posts: 2280
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#5775
Re: sdpvar Function Changes Matlab Random Number Seed 1 Year, 7 Months ago Karma: 32
In the bottom of yalmip.m, there is a function called rand_hash. Replace it with

Code:


function r = rand_hash(k,n,m);
try
    s = rng;
    rng(k);
    r = rand(n,m);
    rng(s);
catch
    s = rand('state');
    rand('state',k)
    r = rand(n,m);
    rand('state',s);
end

lofberg
Platinum Boarder
Posts: 2280
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
Go to topPage: 12
Moderators: jcg207