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