i have non-linear optimization problem constraints. can solved in microsoft excel solver add-in, having trouble replicating in c#.
i installed microsoft solver foundation dll
this example adapted code : http://msdn.microsoft.com/en-us/library/gg261758(v=vs.93).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3
basically, in code, have 2 parameters called rho , nu. need optimal values minimizes sum of squares. have 1 value rho , 1 value nu, know putting rho[s] , nu[s] in code wrong, when replace them rho , nu, errors.
another issue that, in example, code looks 1 optimal variable called "fuel" of type "goal". in case, have 2 variables need optimized.
obviously, program breaks @ last line, says: "there no solution propagate".
anyone has idea how code corrected ?? appreciated !
this code:
class segment { public double strike { get; set; } public double volimp { get; set; } public double _rho_ { get; set; } public double _nu_ { get; set; } public double _alpha_ { get; set; } } segment[] segmentdata = new segment[] { new segment { strike = 5, volimp = 53.2608}, new segment { strike = 10, volimp = 48.3773}, new segment { strike = 20, volimp = 43.8949}, new segment { strike = 30, volimp = 40.9367}, new segment { strike = 40, volimp = 38.891}, new segment { strike = 50, volimp = 37.417}, new segment { strike = 60, volimp = 36.2838}, new segment { strike = 70, volimp = 35.3713}, new segment { strike = 80, volimp = 34.6192}, new segment { strike = 90, volimp = 33.9774}, new segment { strike = 100, volimp = 33.4359}, new segment { strike = 110, volimp = 32.9747}, new segment { strike = 120, volimp = 32.5635}, new segment { strike = 130, volimp = 32.2025}, new segment { strike = 140, volimp = 31.8917}, new segment { strike = 150, volimp = 31.6209}, new segment { strike = 160, volimp = 31.3702}, new segment { strike = 170, volimp = 31.1596}, new segment { strike = 180, volimp = 30.9591}, }; solvercontext context = solvercontext.getcontext(); model model = context.createmodel(); // parameters set segments = new set(domain.integer, "segments"); parameter rho = new parameter(domain.real, "rho", segments); rho.setbinding(segmentdata, "_rho_", "strike"); parameter nu = new parameter(domain.realnonnegative, "nu", segments); nu.setbinding(segmentdata, "_nu_", "strike"); parameter alpha = new parameter(domain.realnonnegative, "alpha", segments); alpha.setbinding(segmentdata, "_alpha_", "strike"); parameter mystrike = new parameter(domain.realnonnegative, "mystrike", segments); mystrike.setbinding(segmentdata, "strike", "strike"); parameter myimpvol = new parameter(domain.realnonnegative, "myimpvol", segments); myimpvol.setbinding(segmentdata, "volimp", "strike"); model.addparameters(rho, nu, alpha, mystrike, myimpvol); //constraints model.addconstraint("rho_bound", model.foreach(segments, s => (-1 <= rho[s] <= 1))) ; model.addconstraint("nu_bound", model.foreach(segments, s => (0 <= nu[s]) )) ; model.addconstraint("alpha_bound", model.foreach(segments, s => (0 < alpha[s]))) ; double forward = 100 ; //goal goal mygoal = model.addgoal("mygoal", goalkind.minimize, model.sum(model.foreach(segments, s => model.power((myimpvol[s] - (alpha[s] * (nu[s] / alpha[s] * model.log(forward / mystrike[s])) * (model.log((model.sqrt(1 - 2 * rho[s] * (nu[s] / alpha[s] * model.log(forward / mystrike[s])) + model.power((nu[s] / alpha[s] * model.log(forward / mystrike[s])), 2)) + (nu[s] / alpha[s] * model.log(forward / mystrike[s])) - rho[s]) / (1 - rho[s]))) * (1 + (0.25 * rho[s] * nu[s] * alpha[s] + (2 - 3 * rho[s] * rho[s]) / 24 * nu[s] * nu[s]) * 6.46407))), 2)))) ; //solve context.solve(); context.propagatedecisions();
Comments
Post a Comment