Hello everyone, I have used this forum many times in the past to try and troubleshoot my Stata issues but this is my first post. I have read the FAQ but if there is something I am not doing correctly, or if this is the wrong place, I apologize.
I am currently developing a paper on optimal road toll management systems and within my paper I am trying to see how the proposed system performs compared to the best possible allocation of traffic. As a part of this I am looking to also see how the system reacts to different traffic loads so it is important that the do file can easily be altered to reflect different traffic levels.
How I am accomplishing this so far is to create a certain number of observations (1 per simulated driver) that is assigned a value from a normal distribution (their unique value for using the road). Their total payoff is calculated as a function of how many other drivers have made the same lane selection, as more people enter one lane and congestion increases each individual driver receives a decreased payoff.
The segment of my do file that I use to control these factors is shown below:
clear
version 13.0
set more off
*DRIVER GENERATOR
loc Mean_Value "3" //Change these to update experiment values
loc SD_Value "0.5"
loc Drivers "25"
set obs `Drivers'
gen Driver_ID=_n
gen Value=rnormal(`Mean_Value',`SD_Value')
I have coded it so far so that there is a variable named "Lane_Choice" which will hold the values 0 or 1 depending which lane each driver is in. Then through some code which is somewhat irrelevant to my question I have the individual drivers rearrange until each one can no longer improve their payoff by switching to another lane (Note: each driver is maximizing their individual payoff, not the joint maximization of the group as a whole).
I would then like to compare the sum of all driver payoffs from this scenario with the case where they would be arranged to jointly maximize the total payoff. What I am thinking is to calculate the total payoff from each possible combination of drivers in lanes and then select the largest value. My issue lies in being able to have a code that lists each possible lane allocation that will still respond to my original macro for drivers. The point of this is to be able to quickly change the number of drivers I am simulating and run the file start to finish.
I have found some useful code that can manually be changed depending how many drivers I have but I am having issues finding a way to nicely have this expand or decrease with my number of drivers.
Example showing all possible combination of lanes for 3 drivers:
tempname myfile
postfile `myfile' str10 name time place using myfile, replace
local name "0 1" //
foreach n of local name {
forvalues time = 0/1 {
forvalues place = 0/1 {
post `myfile' ("`n'") (`time') (`place')
}
}
}
postclose `myfile'
use myfile, clear
I have also spent quite a bit of time trying to use the -input- command within a -while- loop but I always seem to receive some type of error called --Break--. Here is the code I had written:
clear
loc Drivers "4"
loc Cells=2^(`Drivers') //how many possible combinations there will be
set obs `Cells'
local w=1
while `w'<=`Drivers' {
local y=1
while `y'<=2^(`w'-1) {
input str1 Driver_`w'
1
end
local y = `y' + 1
}
local z=1
while `z'<=2^(`w'-1) {
input str1 Driver_`w'
0
end
local z = `z' + 1
}
}
What I would ideally be able to produce is some code that reacts to my specified driver amount to create a matrix of all possible combinations, such as what I am showing below:
2 Drivers
Driver_1_Choice Driver_2_Choice
1,1
0,1
1,0
0,0
But then be able to expand when I increase drivers
3 Drivers
Driver_1_Choice Driver_2_Choice Driver_3_Choice
1,1,1
0,1,1
1,0,1
0,0,1
1,1,0
0,1,0
1,0,0
0,0,0
etc.
Sorry if this has been a bit long, I just wanted to make sure I provided enough information to help me troubleshoot this.
Thanks,
Andrew
I am currently developing a paper on optimal road toll management systems and within my paper I am trying to see how the proposed system performs compared to the best possible allocation of traffic. As a part of this I am looking to also see how the system reacts to different traffic loads so it is important that the do file can easily be altered to reflect different traffic levels.
How I am accomplishing this so far is to create a certain number of observations (1 per simulated driver) that is assigned a value from a normal distribution (their unique value for using the road). Their total payoff is calculated as a function of how many other drivers have made the same lane selection, as more people enter one lane and congestion increases each individual driver receives a decreased payoff.
The segment of my do file that I use to control these factors is shown below:
clear
version 13.0
set more off
*DRIVER GENERATOR
loc Mean_Value "3" //Change these to update experiment values
loc SD_Value "0.5"
loc Drivers "25"
set obs `Drivers'
gen Driver_ID=_n
gen Value=rnormal(`Mean_Value',`SD_Value')
I have coded it so far so that there is a variable named "Lane_Choice" which will hold the values 0 or 1 depending which lane each driver is in. Then through some code which is somewhat irrelevant to my question I have the individual drivers rearrange until each one can no longer improve their payoff by switching to another lane (Note: each driver is maximizing their individual payoff, not the joint maximization of the group as a whole).
I would then like to compare the sum of all driver payoffs from this scenario with the case where they would be arranged to jointly maximize the total payoff. What I am thinking is to calculate the total payoff from each possible combination of drivers in lanes and then select the largest value. My issue lies in being able to have a code that lists each possible lane allocation that will still respond to my original macro for drivers. The point of this is to be able to quickly change the number of drivers I am simulating and run the file start to finish.
I have found some useful code that can manually be changed depending how many drivers I have but I am having issues finding a way to nicely have this expand or decrease with my number of drivers.
Example showing all possible combination of lanes for 3 drivers:
tempname myfile
postfile `myfile' str10 name time place using myfile, replace
local name "0 1" //
foreach n of local name {
forvalues time = 0/1 {
forvalues place = 0/1 {
post `myfile' ("`n'") (`time') (`place')
}
}
}
postclose `myfile'
use myfile, clear
I have also spent quite a bit of time trying to use the -input- command within a -while- loop but I always seem to receive some type of error called --Break--. Here is the code I had written:
clear
loc Drivers "4"
loc Cells=2^(`Drivers') //how many possible combinations there will be
set obs `Cells'
local w=1
while `w'<=`Drivers' {
local y=1
while `y'<=2^(`w'-1) {
input str1 Driver_`w'
1
end
local y = `y' + 1
}
local z=1
while `z'<=2^(`w'-1) {
input str1 Driver_`w'
0
end
local z = `z' + 1
}
}
What I would ideally be able to produce is some code that reacts to my specified driver amount to create a matrix of all possible combinations, such as what I am showing below:
2 Drivers
Driver_1_Choice Driver_2_Choice
1,1
0,1
1,0
0,0
But then be able to expand when I increase drivers
3 Drivers
Driver_1_Choice Driver_2_Choice Driver_3_Choice
1,1,1
0,1,1
1,0,1
0,0,1
1,1,0
0,1,0
1,0,0
0,0,0
etc.
Sorry if this has been a bit long, I just wanted to make sure I provided enough information to help me troubleshoot this.
Thanks,
Andrew
Comment