In my problem I have seven global variables to be controlled by Behavior Space.
Variable weights are in a range of 0 to 1 with an increment of 0.1 like,
[“A” [0 0.1 1]],
[“B” [0 0.1 1]],
[“C” [0 0.1 1]],
[“D” [0 0.1 1]],
[“E” [0 0.1 1]],
[“F” [0 0.1 1]],
[“G” [0 0.1 1]]
The restriction in my problem is that in each scenario the combination of all seven weights must be equal to “1” e,g;
0.2+0.2+0.2+0.1+0.1+0.1+0.1=1
However Behavior Space, by default, makes all possible scenarios whether the sum of weights is equal to, greater than or lesser than “1”.
Can you please help me how to extract and use only those variable combinations in which the sum of weights is always equal to “1”.
Bundle of Thanks in anticipation.
I am a new NetLogo user without any programming background and in a desperate need of your help.
You can use the stop condition in Behavior Space
like
A + B + C + D != 1
Related
Good day!
I would like to select the whole numbers in my random data, at the same time it will also choose the adjacent numbers.
For example, I have this raw data
A = [0.1 0.2
0.2 0.1
1 0.3
0.3 0.2
0.4 0.4
2 0.5]
so would like to select the (1, 0.3) and (2, 0.5). then my final ouptut will be,
B= [1 0.3
2 0.4]
Thanks in advance!
You can use modulo:
B=A(sum(mod(A,1),2)==0,:)
========== EDIT ====================
Editing w.r.t. comments, if you are only checking for integers in the first column then you do not need to sum results:
B=A(mod(A(:,1),1)==0,:)
Alternative ways would use logicals instead of numericals:
B=A(all(A==round(A),2),:)
or if only the 1st column is checked:
B=A(A==round(A(:,1)),:)
I'm using MATLAB ga function for my optimization problem. In my problem, I have some decision variables which are integer (0 and 1: I specified lower bound, upper bound, and IntCon for it) plus two continues varibales. Otherwise, all integer variables can't be zero at same time, so at least, we need a single "one" variable among integers. How can I implement mentioned constraint in MATLAB?
This is a Mixed-Integer optimization problem and it can be solved using ga in MATLAB. As mentioned in the documentations: ga can solve problems when certain variables are integer-valued. Not all the variables but certain variables. So you should have at least one real variable among the other integers.
Whit IntCon options, you can specify which variables are integer, for instance IntCon=[1 3] means that your first and third variables are integer. To avoid both integer variables to be 0 at the same time, I think you can add some inequality constraints.
For instance look at the following example:
Let's say we want to find the optimum value for the Ackley function with 5 variables (e.g. in 5 dimensions), [x(1)...x(5)] and let's assume that the first and third variables, x(1) and x(3), are integers. We can write the following script:
nVar = 5;
lb = -5*ones(1,nVar); % define the upper bound
ub = 5*ones(1,nVar); % define the lower bound
rng(1,'twister') % for reproducibility
opts = optimoptions('ga','MaxStallGenerations',50,'FunctionTolerance',1e-3,'MaxGenerations',300);
[x,~,~] = ga(#ackleyfcn,nVar,[],[],[],[],lb,ub,[],[1 3],opts);
disp('solution:');disp(x)
On my machine, I get this solution:
solution:
0 -0.000000278963321 0 0.979067345808285 -0.000000280775000
It can be seen that x(1) and x(3) are integers and both 0. Now let's say as you mentioned, they both cannot be 0 at the same time and if one is 0 the other should be 1. Here the boundaries of the Ackley's problem allows the variables to be in the range defined by lower and upper bounds. However, in your case the lower and upper bounds should be defined as [0] and [1] for both integer variables.
Now I want to avoid both variables to be 0, so I can write the following linear inequality constraint:
% x(1) + x(3) >= 1
% x(1) >= 0
% x(3) > 0
These inequalities should be written in form Ax <= b:
A = [-1 0 -1 0 0
-1 0 0 0 0
0 0 -1 0 0];
b = [-1
0
0];
Now if we run the optimization problem again we see the effect of the constraints on the output:
[x,~,~] = ga(#ackleyfcn,nVar,A,b,[],[],lb,ub,[],[1 3],opts);
disp('solution');disp(x)
solution
1.000000000000000 -0.000005031565831 0 -0.000011740569861 0.000008060759466
Ive got a vector like this
a=[0 5 3 0 1]
and a corresponding vector, containing the same amount of numbers as there are zeros in the first vector
b=[2 4]
what I want to get is
x=[2 5 3 4 1]
I tried fiddling around with, and somewhat got the feeling that the find / full methods might help me here, but didn't get it to work
c=(a==0)
>[1 0 0 1 0]
Thank you!
It is as easy as this:
x=a;
As x==0 gives the vector of all the locations an element = 0, ie [0 1 0 0 1], x(x==0) is indexing x to get the actual elements of x that are equal to 0, which you can then assign values as if it were any other vector/matrix (where the values we are not interested in do not exist, and are not indexed), using the following:
x(x==0)=b;
Suppose that a data set can be divided into three groups (e.g., negatives, zeroes, and positives) and that one desires to represent these groups in a plot using different symbols, assigning < to negatives, o to zeroes, and > to positives; e.g.,
gscatter([-1 -1 0 0 1 1],[1 2 1 2 1 2],[-1 -1 0 0 1 1],'k','<o>',10); xlim([-3 3]); ylim([0 3]);
Suppose further that the input data to the gscatter function lack representation from all groups. The group-symbol relationship could then change because, according to the Matlab documentation, gscatter sequentially assigns symbols from the provided list to groups based upon the sorted order of the unique values of the grouping variable. The upshot of this grouping algorithm is that absence of representation from earlier-sorting groups produces a shift in the symbol/group assignment, thereby destroying symbolic significance (the precise symbol assigned to a group may be immaterial, but this question focuses on those cases where a particular symbol must invariably be assigned to a particular group). For instance, for a data set lacking negative values, gscatter would assign the < symbol to zeroes and the o symbol to positives (the > symbol going unused because a third symbol is extraneous when only two distinct groups exist); e.g.,
gscatter([-1 -1 0 0 1 1],[1 2 1 2 1 2],[-1 -1 0 0 1 1],'k','<o>',10); xlim([-3 3]); ylim([0 3]);
My question is whether one can deterministically assign a symbol to a particular group in cases where there is a possibility of missing groups from a data set (e.g., can one mandate assignment of < to negative values even when no such values are present in the data set, to avoid shifting of the symbol/group relationship described above). The Matlab documentation seems to indicate that such an operation is impossible, meaning that one would have to rely on a series of if statements to determine whether certain groups are missing and to appropriately redefine restricted symbol sets for each possible combination of group representations, but I seek to know whether this limitation can be circumvented more elegantly.
In general, for "processing data that may or may not be present" problems, there's always the horrible cheat of forcing the necessary parts of the data to exist:
x = [-1 -1 0 0 1 1];
y = [1 2 1 2 1 2];
group = [-1 -1 0 0 1 1];
gscatter([x NaN NaN NaN], [y NaN NaN NaN], [group -1 0 1], 'k', '<o>', 10);
(if, unlike plot, gscatter simply ignores a group of only NaN - I don't have it available to actually test - you could just use any coordinates well outside the final axes range)
Another possibility is changing the process itself to ensure consistency regardless of the data:
scatter(x(group == -1), y(group == -1), 10, 'k', '<');
hold on;
scatter(x(group == 0), y(group == 0), 10, 'k', 'o');
scatter(x(group == 1), y(group == 1), 10, 'k', '>');
hold off;
However, in this case explicitly checking the data and adjusting for what is present is almost certainly the nicest approach, given an appropriate Matlab idiom:
markers = '<o>';
midx = ismember([-1 0 1], group);
gscatter(x, y, group, 'k', markers(midx), 10);
Let say my matrix is x = [0.1 0.2; 0.3 0.4; 0.5 0.6; 0.8 0.9 ;1 0.1]
Now i want to check this matrix by threshold values 0
The resulting matrix for each condition should have same size as x and except the satisfied values all other values must go zero in the resulting matrix.For ex,for condition 1, x must be x = [0.1 0.2; 0 0; 0 0; 0 0;0 0.1]
I found a command ind = find(x>0), which gives only indices of those condition and I can get those values in this way :x(ind). But it is an array. If I use logical conditions say > or <, it will give only 1 or 0 based on true or false. It cant give the real matrix values.
Can anybody suggest an idea?
You can use logical indexing like so:
x(x>Value) = 0
You can change the logical expression in brackets to suit your particular requirements. Say you want values equal or larger than 0.3 to be 0 like you suggest in your post. Then you can write:
x(x>=0.3)=0
You can find out more about logical indexing at the bottom of this page:
http://www.mathworks.co.uk/company/newsletters/articles/matrix-indexing-in-matlab.html