Summing breeds netlogo - netlogo

I am fairly new to netlogo and modelling as a whole., but I am really enjoying it. Right now I am stuck…In my model I have breeds, each breed has “x” turtles, every turtle has a “x” variables, until this point I managed to get the value of each turtle and the value of all of them together but I cannot figure it out how to get the value of all the breeds together. I hope you can help me I really appreciate it.
This is the way i got the sum of the variables of the turtles.
show sum [(electricidad) + (transporte) + (prevención) + (atención)] of CM / Comercio.
In order to obtain the sum of breeds i tried
show sum [(SPA) + (SAT) + (SS) + (TT) + (CM) / 5]
The letters inside the "()" are the names of the breeds.

Do all of the breeds have the same variables? In that case you can use:
sum [electricidad + transporte + prevención + atención] of turtles
If there are different variables for different breeds, you can use something like:
(sum [x + y + z] of SPA) + (sum [a + b + c] of SAT) + ...
The parentheses in the preceding line aren't needed, but they might make the code easier to read. The parentheses in your example aren't needed and should probably be left out, unless you have a special reason to include them.

Related

Objective function of a Genetic Algorithm

This is my first effort about AI/ML.
I have the following problem given by my teacher,
Design a simple genetic algorithm in MATLAB, with binary-coded chromosomes, in
order to solve pattern finding problem in 16-bit strings.
The objective function is given by the following formula:
F(x) =
NoS("010") + 2NoS("0110") + 3NoS("01110") + 4NoS("011110") +
5NoS("0111110") + 6NoS("01111110") + 7NoS("011111110") +
6NoS("0111111110") + 5NoS("01111111110") + 4NoS("011111111110") +
3NoS("0111111111110") + 2NoS("01111111111110") +
NoS("011111111111110")
I couldn't understand the formula.
What does it mean by Nos?
What operations are they performing on those binary strings?
What is F(x)?
From what I can read, the NoS are Nitric oxide synthase like NoS1 or NoS2. They are some genes of different species.
These are probably referring to minimize functions: https://www.mathworks.com/help/gads/examples/coding-and-minimizing-a-fitness-function-using-the-genetic-algorithm.html
I hope I'm helping.

Unstable Closed Loop System

You are given a transfer function G(s)=1.81K(s+20)/(0.03338(s^3+10s^2+32s+32)). This system is connected with unity negative feedback . Determine the smallest positive value of K which makes the closed-loop system unstable. Give the answer to 3 d.p.
Correct Answer:
0.531 ± 0.02
This question is set by my lecturer and I have no idea how to do it.
The closed loop system will become unstable as soon as the rlocus function is no longer in the LHP. Either of the two lines when they intersect with 0 on the x-axis. Assume K=1 at first as we are multiplying by the gain.
>> G=tf([1.81 36.2],[0.03338 0.3338 1.0682 1.0682])
G =
1.81 s + 36.2
------------------------------------------
0.03338 s^3 + 0.3338 s^2 + 1.068 s + 1.068
Continuous-time transfer function.
>> rlocus(G)
you should see that when the real axis is 0 the gain is at 0.531.
if we want greater accuracy we can simply use the rlocfind(G,(point that it is 0)
A smart friend of mine showed me that "Routh Array" is the key for this equation.
Expand the polynomial as you have it, therefore you will have this:
VARS TERM VALUE
A s^3 0.03338
B s^2 0.3338
C s 1.06816 + 1.81Κ
D 1.06816 + 36.2Κ
Equate them as A*D=B*C and you will have the term "s^3" on both sides and then you can cancel it out and solve for K.
(0.03338)(1.06816 + 36.2K) s^3 = (0.3338)(1.06816 + 1.81K) s^3
0.0357 + 1.2084K = 0.3566 + 0.6042K
(1.2084 - 0.6042) K = 0.3566-0.0357
K = 0.5311155247
Otherwise use the rlocus function in MATLAB.

Split Anonymous Function into Terms

I am wanting to take a matlab anonymous function of something like #(x) = x + x^2 and split it into a cell array so that
f{1} = #(x) x
f{2} = #(x) x^2
I want to be able to do this with some arbitrary function handle. The best that I have come up with so far is taking in #(x) = x + x^2 as a string and splitting it by the addition signs and appending #(x) to the beginning of this. However, I would like to be able to directly use a function handle as the function argument. It would also be nice as using other variables could lead to some difficulty in the string approach.
I am also considering just taking in a cell array of function handles as an argument, which would be more difficult for the user but easier in my code.
For some background, I'm wanting to do this for some least squares data fitting code that I am writing for a class. This code will be for taking in a model function as an argument and I need to evaluate each term separately for the least squares process. I'm not limiting these models to polynomials and even if a polynomial is the model, I want the option to leave out certain powers in the polynomial. If someone has a better suggestion for taking a model function, that would be great too.
UPDATE: Someone wanted to know what I meant by
I'm not limiting these models to polynomials and even if a polynomial
is the model, I want the option to leave out certain powers in the
polynomial.
For some clarification, I was saying that I didn't want to limit the models to
c0 + c1*x + ... + cn*x^n
in which case I could just take in n as a parameter and create terms from that similar to what happens in polyfit. For example, if I know that my input data fits an even or odd function, I may want one of the following models
c0 + c1*x^2 + c2*x^4 + ... + ck*x^(2k)
c1*x + c2*x^3 + ... + cm*x^(2m-1)
Where k is even and m is odd. Or possibly a model that isn't strictly a polynomial, but keeps the coefficients linear, such as
c0 + exp(x) * ( c1 + c2*x + ... cn*x^(n-1) )
This is an interesting problem. The string should not be split at + signs that are within a parenthesis group. For example, with
f = #(x) x + (x+1)*sqrt(x) + x^2 + exp(x+2);
the string should be split at the first, but not at the second + sign.
This can be accomplished as follows. To detect only + signs that are outside parentheses, add 1 for each opening parenthesis and subtract 1 for each closing parenthesis. Then the desired + signs are those with count 0.
I'm assuming the output should be a cell array of function handles. If it should be a cell array of strings just remove the last line.
F = functions(f);
str = F.function; %// get string from function handle
[pref, body] = regexp(str, '#\(.+?\)', 'match', 'split'); %// pref is the '#(...)' part
body = body{2}; %// body is the main part
ind = cumsum((body=='(')-(body==')'))==0 & body=='+'; %// indices for splitting
body(ind) = '?'; %// '?' will be used as split marker
ff = strcat(pref, strsplit(body, '?')); %// split, and then add prefix
ff = cellfun(#str2func, ff, 'uniformoutput', 0); %// convert strings to functions
Result in this example:
ff{1} =
#(x)x
ff{2} =
#(x)(x+1)*sqrt(x)
ff{3} =
#(x)x^2
ff{4} =
#(x)exp(x+2)

How do I simplify minterms of a truth table?

I have a truth table and I need to convert it into sum-of-product canonical form. Here is my equation from the truth table.
We have 4 variables A, B, C, D and an output Y
Y = !A!B!C!D + !A!BC!D + !A!BCD + !ABC!D + !ABCD + A!B!C!D + A!BC!D
My question is, can I simply using the hamming distance of 1 trick?
For example, Y = AB + A!B = A because the B and !B would cancel out.
Here is what I did
1) !A!B!C!D + !A!BC!D = !A!B!D
2) !A!BCD + !ABC!D = !AC
3) !ABCD + A!B!C!D = nothing because they all cancel out
4) A!BC!D = A!BC!D
That gives me
Y = !A!B!D + !AC + A!BC!D
Would this be correct? or does ALL of the products need to have a hamming distance of 1 in order for me to cancel them out?
No, this is not correct. For example, if A=1, and the rest are 0, then the second-last term from the original equation is satisfied, so Y=1, but none of your three new terms are satisfied, which would imply that Y=0.
You can only eliminate one variable at a time using this method. So your first step was correct (and the fourth step was trivial, but correct), but the second and third step were wrong.
It's a lot easier to reduce this case using a Karnaugh map. If you do, I think you'll find that it just reduces to !AC+!B!D.

Use value in previous cell of matlab matrix for use in equation

Hi I am working with the following code (Brute force method).
"PV_supply" and "WT_supply" and "Demand" are 48x1 size.
what I am trying to do is calculate the "Energy_battery" value for each of the 48 rows. However to do this I need to use the value of "Energy_battery" from the previous row in the calculations of each row which I haven't figured out how to code and was hoping for some help on this. therefore the equation for "Energy_battery" in row 1 uses the "Energy_battery" value in row 1 for the equation etc
My code is:
for number_panels = 0:5
for number_turbines = 0:3
for h=1:24 %# hours
for d = 1:number_of_days %# which day
n = h + 24*(d-1);
Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;
Energy_battery(number_panels + 1, number_turbines + 1,n+1,1) =...
Energy_battery(number_panels + 1, number_turbines + 1,h,1) + ...
((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - ...
Demand(n)/inverter_efficiency)*battery_charging_efficiency;
This is an extended comment, only an answer to parts of your question, though I think it has relevance to your other recent questions too.
It's helpful to think of Matlab as an array processing language, the natural 'unit' of computation is an array rather than a scalar as in many other languages. If you find yourself writing loops to iterate over the elements of an array stop and think, there is likely to be a more 'natural' way of expressing the same calculation without the loops. There's nothing absolutely wrong with loops but over-reliance on them can have 2 deleterious effects:
Matlab code without loops is often (much) faster than the equivalent with loops;
Code with loops is often more cluttered, and requires more variables to be used, than the equivalent without loops. The clutter is a hindrance when it comes to understanding the code
So, for example, your statement
Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;
could be lifted out of your loop nest entirely and rewritten as
Energy_battery(:,:,1,1) = 100;
Now, for the main bulk of your code, if I've understood it correctly, you want to update each element at Energy_battery(:,:,n+1,1) based on the values in Energy_battery(:,:,n,1) and in element n of the other vectors you have. First, let's tidy this up
((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - Demand(n)/inverter_efficiency)*battery_charging_efficiency
could be rewritten as
((PV_supply*number_panels + WT_supply*number_turbines) - Demand/inverter_efficiency)*battery_charging_efficiency
for Matlab multiplying an array by a scalar applies the multiplication to each element of the array. And again, this doesn't need a loop over the values of an index such as n.
I'm afraid I have to go and do some work now, will come back later and finish the lesson if no-one else does. Feel free to edit this answer if you want to.