Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using C# and the Solver to fit a 2D Gaussian. Using simulated data (no noise) with various sigmas, intensities and center, it was working perfectly. However, with real data, it works only about 50% of time even though all the test data are images of about the same quality and approx. same sigmas for both x & y. Can I submit the code so someone who has more experience can see what I did wrong.

Images are about 100x100 with 5 mm resolution and sigmas of about 7mm for both x & y.

When failed, it failed with values of sigmas and intensity both close to 0.0.

It is such a simple problem, I don't think the Solver can't handle it and the data are not that noisy at all. It must be something subtle that I didn't construct the model properly.

I would greatly appreciate if someone can help.

Thanks,

Daniel

Here is the main code
var solver = SolverContext.GetContext();
            solver.ClearModel();
            var model = solver.CreateModel();
                 
            var decisionsA = Spot_Params.Spot_ParamsA.Select(
                it => new Decision(Domain.RealNonnegative, it.Key));
            model.AddDecisions(decisionsA.ToArray());
            var decisionsB = Spot_Params.Spot_ParamsB.Select(
                it => new Decision(Domain.Real, it.Key));
            model.AddDecisions(decisionsB.ToArray());
          
            var objective = new SumTermBuilder(gaussMatrix.Data_1D.Count());
            var sx = model.Decisions.ElementAt(0);           
            var sy = model.Decisions.ElementAt(1);           
            var sf = model.Decisions.ElementAt(2);            
            var cx = model.Decisions.ElementAt(3);            
            var cy = model.Decisions.ElementAt(4);           

            var sx_sq_2_inv = 1.0 / (sx * sx * 2.0);
            var sy_sq_2_inv = 1.0 / (sy * sy * 2.0);
                      
            int n = 0;
            for (int j = 0; j < gaussMatrix.Nrow; j++)
            {
                var dy = gaussMatrix.Ypos[j] - cy;

                for (int i = 0; i < gaussMatrix.Ncol; i++)
                {                              
                    var dx = gaussMatrix.Xpos[i] - cx;
                    var v = (dx * dx) * sx_sq_2_inv + (dy * dy) * sy_sq_2_inv;                  
                    var value = sf * Model.Exp(-v);
                    var diff = gaussMatrix.Data_1D[n++] - value;

                    objective.Add(diff * diff);
                }
            }

            model.AddGoal("OptimizedSpotParams", GoalKind.Minimize, objective.ToTerm());
                      
            // Extract fitted parameters
          
            var spot_params = fittedParams.Spot_Params;
            Solution solution;
            solution = await Task.Run(() => solver.Solve());


What I have tried:

Reviewed code and input data. Couldn't see anything obviously wrong
Posted
Updated 12-Feb-19 20:27pm
v3

Quote:
When failed, it failed with values of sigmas and intensity both close to 0.0.


Keep testing and find out "where" it fails; "close to" is only good in horse shoes.
 
Share this answer
 
Quote:
It is such a simple problem, I don't think the Solver can't handle it and the data are not that noisy at all. It must be something subtle that I didn't construct the model properly.

your code work depending on data, we have no way to find what is wrong without that data.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
dyeung 13-Feb-19 3:34am    
One thing I potentially not doing right is that I assume the variables defined in the Dictionary are in the order I added. As it has been warned that the order of the hash table is un-deterministic. However, using the debugger, the variable sx, sy, etc. did pair with the expected keys. Maybe during runtime, that is not necessary the case.

Will try to change code to assign the variable using 'keys' instead and see if it helps.

Daniel
dyeung 13-Feb-19 6:32am    
The issue was not related to the Dictionary. Problem persists even by adding the decisions one by one to be sure of the exact order. No help.
dyeung 13-Feb-19 6:36am    
Anyone can show me how to add a constraint to the model? For example, such that decision sx ("SigmaX") must be greater than 1. Optimization won't stop after adding constraint:

model.AddConstraint("sx_min", "SigmaX > 1.0");
Thanks for the response.
I have gone through the debugger. But the optimizing code doesn't show you the actual run except the 'variables' defined without actual values so there is no way (as I know of) to figure out what is happening during optimization.

The data and code are very simple. Can I provide a zip file to someone to try it out?

I have data that works, and data that fails.

Thanks,

Daniel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900