I have written a simple code for a model in NetLogo. At the same time the model is well studied through ordinary differential equations in literature. Now I would like to compare some plots of model obtained by both NetLogo and Matlab (used to solve differential equations). I used "ticks" command to increment the time in NetLogo, where as Matlab uses time in seconds. What kind of precautions ( changes ) should I keep in mind in order to compare the plots obtained by NetLogo and Matlab.
In general, the ticks-axis of the plots from NetLogo should be some constant scalar of the time-axis of the MatLab plots. That scalar is often referred to in simulations as dt or "step size": the time per tick. If you were just using NetLogo to numerically solve differential equations (not recommended, though possible), you would likely explicitly set this to something (just as you do when numerically solving in MatLab). In most NetLogo models, however, the step size is implicit.
Some common parameters that correspond to step size in models:
speed of agents
rates of growth or decay
rates of diffusion
So, for example, if we're modeling traffic on a street with a speed limit of 100 kph (= (100000 m) / (60 min * 60 sec) = 27.8 m/s), and our patch-size is equal to 1 m and our agents travel at most 0.5 per tick, then we have:
27.8 m/s = (0.5 patches/tick) * (1 m/patches) / (step-size s/tick) = (0.5 m/tick) / (step-size s/tick)
step-size s/tick = (0.5 m/tick) / (27.8 m/s) = 0.018 s/tick
So, in this case, each tick is about 0.018 seconds.
Basically, you should try to find some "per tick" parameter in your model that corresponds to "per second" parameter in the differential equations. Then, you should be able to determine how many seconds per tick there are by comparing these parameters.
Alternatively, you could kind of cheat by just comparing plots, seeing how they line up, and then determine the step size like that. Then, you can work backwards to figure out what parameters in your models are determining the step size.
Related
I want to estimate the phase of harmonic distorted tones when having input tones with random initial phase within [-pi;pi]. I used atan2, build-in function of Matlab, however the phase jump at -pi or pi makes my calculation of phase difference having trouble.
The problem is as follow: there is a nonlinear system, and I want to estimate system's response (amplitude and phase responses). I insert many tones as x[n] = Acos(2pifn*t+b), where fn is frequency, b is the initial random phase within [-pi;pi], and it changes with various tones. If a tone has initial phase b, then 2nd order harmonic distortion (HD) has phase of c=2b, and 3rd HD has phase of d=3b. The phase differences of the HD2 and HD3 could be c/2-b and d/3-b, respectively. Because of the memory effect of the system, the phase differences are no longer zero.
Issues: if the phase of input signal is large, e.g., 70 degree, then HD3 phase is 210 degree, when the phase angle >180, atan2 operation makes a jump to -180 and the HD3 phase become -150 degree instead of 210. Theoretically, cosd(210) = cosd(-150). However, what I want to find is the phase difference. That why, (-150/3-70) leads to a wrong results (it should be 0 if there is no memory effect). Similar situation for the case if input signal has large negative phase and for both HD2 and HD3. The major cause is antan2 limitation of [-pi;pi], and I want to expand to [-3pi;3pi].
Please help.
Thank you very much.
Regards
The function you want is unwrap, e.g.,
>>> angles=[-0.7*pi -0.9*pi 0.9*pi 0.7*pi]
angles =
-2.1991e+00 -2.8274e+00 2.8274e+00 2.1991e+00
>>> unwrap(angles)/pi
ans =
-7.0000e-01 -9.0000e-01 -1.1000e+00 -1.3000e+00
Hi I am using tensorflow at my university to try to classify steering angles of a simulation program using only the images the simulation produces.
The Steering angles are values from -1 to 1 and I separated them into 50 "buckets". So the first value of my prediction vector would mean that the predicted steering angle is between -1 and -0.96.
The following shows the classification and optimization functions I am using.
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
y is a vector that with 49 zeros and a single 1 for the correct bucket. My question now is.
How do I take into account if e.g. the correct bucket is at index 25, that the a prediction of 26 is much better than a prediction of 48.
I didn't post the actual network since it is just a couple of conv2d and maxpool layers with a fully connected layer at the end.
Since you are applying Cross entropy or negative log likelihood. you are penalizing the system given the predicted output and the ground truth.
So saying that your system predicted different numbers on your 50 classes output and the highest one was the class number 25 but your ground truth is class 26. So your system will take the value predicted on 26 and adapt the parameters to produce the highest number on this output the next time it sees this input.
You could do two basic things:
Change your y and prediction to be scalars in the range -1..1; make the loss function be (y-prediction)**2 or something. A very different model, but perhaps more reasonable that the one-hot.
Keep the one-hot target and loss, but have y = target*w, where w is a constant matrix, mostly zeros, 1s on the diagonal, and smaller values on the next diagonal, elements (e.g. y(i) = target(i) * 1. + target(i-1) * .5 + target(i+1) * .5 + ...); kind of gross, but it should converge to something reasonable.
I have a system of linked differential equations that I am solving with the ode23 solver. When a certain threshold is reached one of the parameters changes which reverses the slope of my function.
I followed the behavior of the ode with the debugging function and noticed that it starts to jump back in "time" around this point. Basically it generates more data points.However, these are not all represented in the final solution vector.
Can somebody explain this behavior, especially why not all calculated values find their way into the solution vector?
//Edit: To clarify, the behavior starts when v changes from 0 to any other value. (When I write every value of v to a vector it has more than a 1000 components while the ode solver solution only has ~300).
Find the code of my equations below:
%chemostat model, based on:
%DCc=-v0*Cc/V + umax*Cs*Cc/(Ks+Cs)-rd
%Dcs=(v0/V)*(Cs0-Cs) - Cc*(Ys*umax*Cs/(Ks+Cs)-m)
function dydt=systemEquationsRibose(t,y,funV0Ribose,V,umax,Ks,rd,Cs0,Ys,m)
v=funV0Ribose(t,y); %funV0Ribose determines v dependent on y(1)
if y(2)<0
y(2)=0
end
dydt=[-(v/V)*y(1)+(umax*y(1)*y(2))/(Ks+y(2))-rd;
(v/V)*(Cs0-y(2))-((1/Ys)*(umax*y(2)*y(1))/(Ks+y(2)))];
Thanks in advance!
Cheers,
dahlai
The first conditional can also be expressed as
y(2) = max(0, y(2)).
As one can see, this is still a continuous function, but with a kink, i.e., a discontinuity in the first derivative. One can this also interpret as a point with curvature radius 0, i.e., infinite curvature.
ode23 uses an order 2 method to integrate, an order 3 method to estimate the error and probably the order 1 Euler step to estimate stiffness.
An integration step over the kink renders all discretization errors to be order 1 (or 2, depending on the convention), confounding the logic of the step size control. This forces a rather radical step-size reduction, but since that small step then falls, most probably, short of the kink, the correct orders are found again, resulting in a step-size increase in the next step which could again go over the kink etc.
The return array only contains successful integration steps, not the failed attempts of the step-size control.
What is the fitness function used to solve an inverted pendulum ?
I am evolving neural networks with genetic algorithm. And I don't know how to evaluate each individual.
I tried minimize the angle of pendulum and maximize distance traveled at the end of evaluation time (10 s), but this won't work.
inputs for neural network are: cart velocity, cart position, pendulum angular velocity and pendulum angle at time (t). The output is the force applied at time (t+1)
thanks in advance.
I found this paper which lists their objective function as being:
Defined as:
where "Xmax = 1.0, thetaMax = pi/6, _X'max = 1.0, theta'Max =
3.0, N is the number of iteration steps, T = 0.02 * TS and Wk are selected positive weights." (Using specific values for angles, velocities, and positions from the paper, however, you will want to use your own values depending on the boundary conditions of your pendulum).
The paper also states "The first and second terms determine the accumulated sum of
normalised absolute deviations of X1 and X3 from zero and the third term when minimised, maximises the survival time."
That should be more than enough to get started with, but i HIGHLY recommend you read the whole paper. Its a great read and i found it quite educational.
You can make your own fitness function, but i think the idea of using a position, velocity, angle, and the rate of change of the angle the pendulum is a good idea for the fitness function. You can, however, choose to use those variables in very different ways than the way the author of the paper chose to model their function.
It wouldn't hurt to read up on harmonic oscillators either. They take the general form:
mx" + Bx' -kx = Acos(w*t)
(where B, or A may be 0 depending on whether or not the oscillator is damped/undamped or driven/undriven respectively).
How does one implement ODE in netlogo?
Right now , I use the Euler method which is solves the the given equations:
R[t+1] = r[t] + v[t] * step-size
V[t+1] = v[t] + f * step-size
Found this similar question, but it suggest uni-directional link from mathematica to netlogo which is makes it not possible to implement.
I looking for something similar to ode solver in matlab?
Partial answer, compare accuracy-runtime estimates for Euler, improved Euler and RK4
Assume that the most time-consuming part is the evaluation of the system function, i.e., the vector arithmetic of the Runge-Kutta methods are a small overhead.
Given a time interval of T=1, restrict the methods to 100 function evaluations. Then
Euler performs 100 steps with step size h=0.01 and global error of the magnitude pow(h,1)=0.01.
improved Euler has global error order 2 and 2 function evaluations. Thus performs 50 steps with step size h=0.02 and global error of magnitude pow(h,2)=4e-4.
classical RK4 of order 4 and 4 function evaluations per step performs 25 steps of step size h=0.04 with global error magnitude pow(h,4)=2.56e-6.
Fixing the global error at about 1e-4 one obtains inversely for number of function evaluations
10000 for Euler, with h=1e-4
200 for improved Euler, with h=1e-2
40 for classical RK4, with h=0.1
which explains speed differences if the methods are tuned for the same level of the global error.
The strategy for the implementation of RK4 in the ball example is for an ODE system x'=f(t,x), where x is the, possibly very large, state vector
save t0=t, x0=x
evaluate k1 = f(t,x)
set t=t0+h/2, x=x0+h/2*k1
evaluate k2=f(t,x)
set x=x0+h/2*k2
evaluate k3=f(t,x)
set t=t+h, x=x0+h*k3
evaluate k4=f(t,x)
set x=x0+h/6*(k1+2*(k2+k3)+k4)
In an agent based system it is reasonable to store the components of the state vector belonging to the agent as internal variables of the agent. Then the vector operations are performed by iterating over the agent collection and computing the operation tailored to the internal variables.
A second order ODE is transformed to a first order system as indicated in the question, x''=a(x,x') gets transformed to [x',v']=[v, a(x,v)]. The big vector of the agent system is the composed of the collection of the pairs [x,v] or, if desired, as the concatenation of the collection of all x components and the collection of all v components.