|
Wow! First of all thank you _very very much_ for your time and your great answer. I Need some time to go through all this, especially "Simulated Annealing" which I do not know until now.
Quote: But did you mean this Yes exactly, I'm using Amoeba. But meanwhile (it looks like) I recognized, that I'm on the wrong path (not completely sure about it).
Basically I Need to solve a more or less easy Thing:
t= w * b + A * c
where
t is a vector (400 items)
w is a scalar
b is a vector (400 items)
A is a 400x4 matrice (4 can be 1...8)
c is a vector (4 items, 1...8)
t is the target, b is the current Situation (captured by a measurement). The question is: what are the optimal w and c to come from b to t while w should not differ too much from 1 (1-w as small as possible and w <= 1) and c should be as small as possible and all items need to be >= 0. In other words: How much I Need to throw away from b and what I Need to add with c to bring b as good as possible to t. At the Moment "as good as possible" means best curve fit, but Needs later to be Extended by a completely non linear function (spectroscopy analysis), that's why the simplex approach.
All this sounds so easy, but the Problem is: Sometimes (when the math model fits the physics well), b is a linear combination of A and that is a Problem, that the simplex method usually Ends in a w=0...
Ok, using simplex here is most probably the wrong approach. Meanwhile I calculated this with "Singular-value decomposition" of A.The results are great, but I'm afraid I choose too much nice test data. With SVD I calculate a scan for different w's: t-w*b= A*c. At the Moment I don't trust the results because I always get c vector with positive items. This would be great, because this is the what I need, but for me it Looks more like a mistake from my side. Why I should be that lucky to get always positive c values...
Sorry to text you that much.
Bruno
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Hello again
I'll try to reformulate your problem in a more formal way.
First, you have an expression with 5 unknowns (w and the 4 components of c: c1, c2, c3 and c4). By your description, everything else are known values. In other words you have a function of 5 variables
f(w,c1,c2,c3,c4) = w*b+A*c
Next, you want this expression to come as close as possible to a target vector t. You can express this by defining a new function g that computes the difference between f(w,c) and t:
g(w, c) = w*b + A*c - t
And you want the resulting vector to be as small as possible:
norm(g(w0, c0)) = Minimum(norm(g(w, c))) for any w in [0,1] and any ci in [0,infinity]
Since this is a linear problem, none of the algorithms I previously suggested will be preferable. If you use the Euclidian norm, you can minimize the square of the norm instead, to avoid the square root, and turn the function into a quadratic function. That means there is only one minimum, and you don't need to care about getting stuck in local minima.
A quick search also found me this algorithm which I wasn't aware of before: Benson's algorithm - Wikipedia[^] . Maybe this will help.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Dear Stefan
Thank you very much again. I will makes some tests based on your Input.
Thanks again.
Regards
Bruno
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Building on my previous explanations, I realized that the solution may in fact be rather easy to find. the only possible complication is that the global minimum may not lie within the required ranges for w and c. In that case the solution would lie somewhere on the boundary of the parameter space.
The algorithm will work like this: since you want to find the minimum for the norm of the function g, an equivalent task would be to find the minimum for the squared norm of g! Therefore the target function to minimize is:
h(w,c) = square(norm(g(w,c))) = g(w,c)*g(w,c) = (w*b + A*c - t)*(w*b + A*c - t)
A necessary condition for the minimum is that all partial derivatives are 0:
dh/dw = dh/dc1 = dh/dc2 = dh/dc3 = dh/dc4 = 0
This gets you:
dh/dw = 2*(w*b + A*c - t)*b = 0
dh/dci = 2*(w*b + A*c - t)*Ai = 0 , where Ai is the i'th column of A, and i = 1, 2, 3, or 4
These are five linear equations in w, c1, c2, c3, and c4, that can be solved easily with a linear equation solver.
Once you have the solution to this LES, you will have to verify that the solution is actually a minimum and not a maximum, and that the constraints for the parameters are fulfilled. If this is not the case, the solution must be at the boundary of the constrained parameter space, i. e. one or more of the parameters must be fixed at a limit of their valid range. This means you can only use the linear equations above for the partial derivatives of the other variables, leaving you with a LES of 4 or less variables that can also be solved easily.
The only problem here is the complexity of searching each part of the boundary: if there were three restricted parameters, and the restricted range would describe a 3d cube, you'd have to search each of the sides, and each of the edges, and each of the corners of the parameter cube for a local minimum, and then you'd have to compare all minima you've found to find the global minimum. Since you have 5 variables, it gets a lot more complex than that...
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I tested (I am testing, not yet sure all is bug free) and at the moment I face the problem in case b can be expressed very well by A. As expected w does go then down near zero. But I need to test more.
Thank you for your big inputs, it opened my mind for new approaches.
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
If b is a linear combination of the matrix columns Ai, then you can choose w to be equal to 1, without constraining the solution space.
If it only comes close, it gets tricky: in that case a standard algorithm for solving the LES I described - e. g. Gauss Elimination - may run into numerical problems due to cumulated rounding effects. A simple method to minimize such numerical issues is to rearrange the LES with a Pivot Search. There are more advanced and stable methods, but for a system with just 5 equations Pivot Search should work just fine.
The Gaussian elimination - Wikipedia[^] entry shows pseudo code for Gauss Elimination with Pivot Search at the end.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Quote: If b is a linear combination of the matrix columns Ai, then you can choose w to be equal to 1, without constraining the solution space.
Not in my opinion, because to correct it -under some circumstances, e.g. one component has a to high ci- I need to "throw away" a part of b and that means w has to be < 1.
Quote: If it only comes close That is also a Point, I have to fight with all circumstances.
1. b is exactly enough a linear combination (even here I have a Problem to define "exactly enough")
2. b is not a linear combination (_but_ good enough [what ever again how this will be qualified], but we are "near enough" to correct it with w*b + A*c.
Finally, you gave me so much good Inputs, you are the greatest for me! Thank you very much and please don't feel presured to answer. This also because at the Moment I'm something overhelmed with all your great tips and to try them all
Bruno
Oh God, my English is sometimes(usually) very cryptic.... I think I start again with crypto stuff
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
#define FUNC_1(x) (x+1)
#define FUNC_2(x) (x+2)
#define FUNC_3(x) (x+3)
#define FUNC_4(x) (x+4)
#define FUNC_N(n, x) (FUNC_##n(x))
----------------------------------------
int result1 = FUNC_N(1, 100);//OK
but i want to below:
int a = 1;
int result2 = FUNC_N(a, 100);//NG
please help ~
|
|
|
|
|
You can't do that. Macros are expanded early in the compilation process, and only then is the resulting C (or C++) code compiled. The a in your code is therefore treated as a text string "a", rather than the number 1.
In C++, you could do something similar:
template <int n, class T> T FUNC(T x) { return x + n; }
...
int const a = 1;
int result = FUNC<a>(100);
Note the const before the a.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Why would you want all those extra macros? Why not just:
#define FUNC_N(x, y) ((x) + (y))
int result2 = a + 100;
|
|
|
|
|
if you insist on using macros, then you will need to do something ugly, like:
#define FUNC_N(n,x) (n==1 ? FUNC_1(x) : n==2 ? FUNC_2(x) : n==3 ? FUNC_3(x) : n==4 ? FUNC_4(x) : 0)
it's much better to just make a function that can switch on n:
int FUNC_N(int n, int x)
{
switch (n)
{
case 1: return FUNC_1(x);
}
}
|
|
|
|
|
|
Sorry, but it would take far too much time and space to explain that code in detail. If you do not understand the C language there are plenty of on-line tutorials, or you could find a book on Amazon. Alternatively, you could go back to StackOverflow and ask the person who wrote it.
|
|
|
|
|
Actually i just asked to explanation code which green sign sembol answer from stackoverflow . I try to ask my question with using comment but need 50 reputation .
|
|
|
|
|
You should ask the person who posted the code on Stackoverflow. This is CodeProject.
|
|
|
|
|
I have a table with 2 columns; here’s two typical examples:
TABLE 1 TABLE 2
X Y X Y
-46.3 16.0 -50.3 71.2
-40.1 -28.1 -43.6 117.7
-34.0 -154.0 -36.9 165.7
-27.8 -171.8 -30.2 176.9
-21.6 178.0 -23.5 -179.2
-15.4 166.2 -16.8 -173.3
-9.3 120.1 -10.1 -149.3
-3.1 -2.0 -3.4 -86.2
3.1 -28.6 3.4 -67.0
9.3 -80.7 10.1 -72.8
15.4 -147.7 16.8 -93.5
21.6 -175.5 23.5 -151.7
27.8 162.6 30.2 112.9
34.0 120.2 36.9 80.4
40.1 49.4 43.6 67.7
46.3 15.4 50.3 71.4
In the table 1, the Y decreases, while in the table 2 the Y increases. But notice the ambiguity in the table 1 for Y= 150 and in the table 2 for Y= 70.
I generate one table at runtime (I plan to use from 20 to 50 rows), the column Y is an angle (I use radians from –pi to pi and double precision number, but here I used degrees for the sake of simplicity).
The program generates an angle from –pi to pi and I need to find the two X’s that bracket the angle. For example, if the angle is 150, for the table 1 the function should find [-15.4, -9.3] and [27.8, 34.0].
|
|
|
|
|
If you wonder why no one answers your question, it may be because of the confusing way you are describing your problem:
Member 3648633 wrote: In the table 1, the Y decreases, while in the table 2 the Y increases.
No! In both tables, X increases, but Y does not strictly increase or decrease over the full range.
Member 3648633 wrote: But notice the ambiguity in the table 1 for Y= 150 and in the table 2 for Y= 70.
Ambiguity of what? What are you talking about?
Member 3648633 wrote: the column Y is an angle (I use radians from –pi to pi
That would be the interval [-3.1415, +3.1415]. Your values are much greater than that! If these are angles, your values are in degrees, not radians!
Member 3648633 wrote: I need to find the two X’s that bracket the angle. For example, if the angle is 150, for the table 1 the function should find [-15.4, -9.3] and [27.8, 34.0].
Well, finally! Why didn't you start your posting with that simple description of the task? It all makes more sense now. Moreover, there's no 'ambiguity' - there are simply multiple solutions to your problem.
Now there's only one thing left to do: pose a question. I didn't find any.
P.S.: Since you didn't pose a question, I have one for you: What should the program return for an angle input of 179 degrees? That is a possible input after all...
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Let us consider, 100n + 5 = O(n). For this function there are multiple n_0 and c possible.
Solution1: 100n+5 ≤100n+n=101n≤101n,for all n ≥5,n_0=5 and c=101 is a solution.
Solution2:100n+5≤100n+5n=105n≤105n,for all n≥1,n_0=1 and c=105 is also a solution
|
|
|
|
|
Thank you for that bit of enlightenment.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
The difficulty of understanding when and how it began.
|
|
|
|
|
Thanks for replying!
|
|
|
|
|
But how could time "begin"? Surely that implies a transition from one state to another, which requires the concept of "time" to already exist.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The scientists tell us it started with an enormous bang.
|
|
|
|
|
But again, "started" requires the concept of time to already exist.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|