Hello,
Excuse me for my limited English. If you have trouble understanding what I'm saying, don't hesitate to ask me, I really need help.
I need to convert an Excel file to C# software.
The current operator uses a large Excel file to perform complex calculations. But to get those results, he uses solvers in Excel.
I have to create software that automates all the measurement part then the calculations and therefore the solvers.
I admit it's really vague to me how solvers work, whether in Excel or C#.
What I have tried:
I saw that in C# there is a "Microsoft Solver foundation" library. I found a tutorial on this site: https://www.codeproject.com/Articles/1183168/Solving-optimization-problems-with-Microsoft-Solve
But it remains complex and I find it difficult to make the link with my situation.
What I have the most trouble understanding is how to make the interaction between the variables and the result.
In excel I visualize the solver on each tab concerned, example:
objective to be defined : R18
At: Min
Variable cells: L18;Q18
Constraint:
L18 >= 0
L18 >= 0
J18 <= K18
N18 >= C54
L18 <= 4
L18 <= 4
J18 >= I18
J18 <= K18
J18 >= I18
Checked: make unconstrained variables non-negative.
Select a resolution = GRG Nonlinear
(For information, I removed the $, I was unable to put an image.)
How to transcribe this to C#.
in the excel solver i tried to do some tests and if i change the "goal" cell to a blank cell the solver tells me that the goal must contain a formula. Which is normally the case, but how to give it a formula in C#. Also knowing that the formula will look for data in multiple tabs which are also formulas etc.
Another potential solution:
Have the excel file in the background. The C# software updates the data before running a macro that would launch the solvers (a macro that already exists). Then I retrieve the data in Excel in order to display it.
It's not super clean, but maybe faster for development, although I never found a quick fix to use Excel from Dotnet, before I was in VB and it was long enough to access read and write cells.
And then, the excel solver indicates whether the resolution of the problem has been found or not. In the case of the execution of the macro in C#, can we recover these messages?
In macro I have this:
...
Sheets("SheetX").Select
Range("R18").Select
SolverOk SetCell:="$R$18", MaxMinVal:=2, ValueOf:=0, ByChange:="$L$18,$Q$18", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOk SetCell:="$R$18", MaxMinVal:=2, ValueOf:=0, ByChange:="$L$18,$Q$18", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
I find some data, but not the constraints, is this normal?
Thank you for your precious help.