below you can find a trivial optimisation code I have written to check how Foundation Solver works. I am then trying to re-write this code using reflection, starting from the end of the code (step 6 in the code below).
I get a NullReferenceException when it comes to getting the type of the "Solution class": it seems that the fully-qualified name ("Microsoft.SolverFoundation.Services.Solution") of the "Solution" Class is wrong.
Can someone help me with that?
Thanks
class Program
{
static void Main(string[] args)
{
// Get two input values
Console.WriteLine("Input values of X and Y");
Console.WriteLine("X value: ");
double Xinput = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Y value: ");
double Yinput = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("");
/* Create the optimisation problem */
// 1) Creat the solver and model objects
var solver = SolverContext.GetContext();
var model = solver.CreateModel();
// 2) Create variables
var X = new Decision(Domain.RealNonnegative, "X");
var Y = new Decision(Domain.RealNonnegative, "Y");
// 3) Add them to the model
model.AddDecision(X);
model.AddDecision(Y);
// 4) Add goals
model.AddGoal("Goal", GoalKind.Minimize, X + Y);
// 5) Add constraints
model.AddConstraint("X_constraint", 0 <= X <= 500);
model.AddConstraint("Y_constraint", 120 <= Y <= 3120);
// 6) Solve optimisation
// Solution solution = solver.Solve(); // This is what should be without reflection
// rewritten with reflection
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Type solutionType = executingAssembly.GetType("Microsoft.SolverFoundation.Services.Solution"); // <-- something wrong here??
object solution = Activator.CreateInstance(solutionType); // HERE I GET THE ARGUMENT NULL EXCEPTION!
MethodInfo solveMethod = solutionType.GetMethod("Solve");
solveMethod.Invoke(solution,null);
// 7) Print the outputs
Console.WriteLine("Results:");
Console.WriteLine("X: " + Math.Round(X.GetDouble(), 2));
Console.WriteLine("Y: " + Math.Round(Y.GetDouble(), 2));
Console.WriteLine("");
}
}
Aucun commentaire:
Enregistrer un commentaire