how to generate uniformly random varaibles with inequality constraints

1 次查看(过去 30 天)
Hi,
I have two variables function f(c1,c2) and I would like to randomly generates c1 and c2 with the constraints that
  • Maximum >= f(c1,c2)>=C here C is a constant, which is less than the optima for f in a constraint area
  • c1>=0, c2>=0,
  • c1+c2<=100
Do you know you to realize this? For example, as in the image below, I would like take a random point of (c1,c2) in the darkest blue area.
  1 个评论
xueqi
xueqi 2013-8-1
I think maybe I can first generate c1 and c2 randomly with the constraints
  • Condition1: c1>=0, c2>-0
  • Condition2: c1+c2<=100
Then I check if the answers satisfies the constraints
  • Condition3: Maximum >= f(c1,c2)>=C
If it satisfies, then great. But if not, then I repeat the first step again and again until find a set of c1 and c2 satisfies condition3. So I think I need a loop with uncertain reiterations. Now my questions are
  1. If this method will violates the randomness of what I want actually
  2. How to do a loop with random reiterations

请先登录,再进行评论。

采纳的回答

xueqi
xueqi 2013-8-1
Add IF CONDITION BRKEAK END in the loop will do. Set the iteration to a really big number to make sure(with a really small probability to fail) that the loop will generate at least one set of c1 and c2 satisfy the condition3.
LOL why in this thread is only myself ask and answer...

更多回答(1 个)

the cyclist
the cyclist 2013-8-1
编辑:the cyclist 2013-8-1
What you describe is the "rejection method", and it does not violate the randomness that you want.
Here is an example that is similar to what you want:
% The function that defines one of the criteria
f = @(x,y) x.^2 + y.^2;
% Number of points to generate
nPoints = 1000;
% Pre-allocate memory for the random data
c1 = zeros(nPoints,1);
c2 = zeros(nPoints,1);
% Initialize counter for the number of points we have
i = 0;
% Run a loop until we have the number we need
while i < nPoints
% Generate trial points
c1_try = randi([0 100]);
c2_try = randi([0 100]);
% If these points meet the criteria, store them and increment the counter
if (c1_try+c2_try < 100) && f(c1_try,c2_try) > 3000
i = i+1;
c1(i) = c1_try;
c2(i) = c2_try;
end
end
% Plot the resulting points
figure
plot(c1,c2,'.')
This is not the most efficient method. (For example, you could speed this up by generating all the random numbers upfront.) But I thought it might be better to keep it simple so that you can see what is going on.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by