Click here to Skip to main content
12,996,709 members (78,126 online)
Rate this:
Please Sign up or sign in to vote.
Well I have a pretty doomed feeling right now that what seemed like a fantastic solution (free with MSDN subscription, distributable etc) is actually a no-goer.

I am trying to use Microsoft Solver Foundation in a C# visual studio project. Some of the parameters used in the optimization are random numbers from a statistical probability distribution so I need to use RandomParameter types (RandomParameter is an abstract base class from which classes such as NormalDistributionParamater are derived). There are only a few pre-defined RandomParameter types, so I am trying to figure out if there is any way to turn data from other statistical distributions into the right class type to be used in the AddConstraint method. It seems that ScenariosParameter is some kind of customizeable class that maybe I can populate with data, so I have adapted some code which I found on the internet to use a ScenariosParameter class which contains Poisson distribution data. I have tried to create a parameter:
ScenariosParameter poissonRandomVals;
The code compiles but it crashes on the line
model.AddConstraint("demand2", 0.3 * sa + 0.4 * vz + gasBuy >= poissonRandomVals);

I am very new to C# and MSF and cannot figure out why.The whole code is shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Solvers;
using Microsoft.SolverFoundation.Services;

namespace JLSolver
    class Program
        static void Main(string[] args)
            SolverContext context = SolverContext.GetContext();
            Model model = context.CreateModel();
            // decide how many barrels to buy from each crude oil source
            Decision sa = new Decision(Domain.RealRange(0, 9000), "SA");
            Decision vz = new Decision(Domain.RealRange(0, 6000), "VZ");
            model.AddDecisions(sa, vz);
            // decide how much refined oil to by, this decision must be made after random PD number has been generated,
            // and see if demand can be met without this option, hence recourse
            // so if demand can be met buy purchasing crude oil, no need to buy refined so do not need to make this decision
            RecourseDecision gasBuy = new RecourseDecision(Domain.RealNonnegative, "GasBuy");
            RecourseDecision jetFuelBuy = new RecourseDecision(Domain.RealNonnegative, "JetFuelBuy");
            RecourseDecision lubricantBuy = new RecourseDecision(Domain.RealNonnegative, "LubricantBuy");
            model.AddDecisions(gasBuy, jetFuelBuy, lubricantBuy);
            // goal is to minimise cost of buying crude and refined oil
            model.AddGoal("goal", GoalKind.Minimize, 20 * sa + 15 * vz + (38.4 * gasBuy + 35.2 * jetFuelBuy + 28.8 * lubricantBuy));
            // demand for each fraction varies and has a PD
            RandomParameter gasDemand = new NormalDistributionParameter("GasDemand", 1900, 50);
            RandomParameter jetFuelDemand = new NormalDistributionParameter("JetFuelDemand", 1500, 25);
            RandomParameter lubricantDemand = new NormalDistributionParameter("LubricantDemand", 500, 5);
            // create a poisson distribution random number generator
            Troschuetz.Random.Generator rndGenerator = new Troschuetz.Random.StandardGenerator();
            Troschuetz.Random.PoissonDistribution poissonDist = new Troschuetz.Random.PoissonDistribution(rndGenerator);
            // get an array of 100 poisson distribution random numbers
            Term[] poissonNumbers = new Term[100];
            for (int i = 0; i < 100; i++)
                poissonNumbers[i] = poissonDist.NextDouble(); // returns a poisson distributed random number, no seed defined
            Set poissonSet = new Set(poissonNumbers);
            RandomParameter poissonRandomVals = new ScenariosParameter("RandomParams", poissonSet);
            model.AddParameters(gasDemand, jetFuelDemand, lubricantDemand, poissonRandomVals);
            // must buy enough barrels of oil to meet requirements, from crude and refined sources
            model.AddConstraint("demand1", 0.4 * sa + 0.2 * vz + jetFuelBuy >= jetFuelDemand);
            model.AddConstraint("demand2", 0.3 * sa + 0.4 * vz + gasBuy >= poissonRandomVals); // THIS LINE CRASHES !!!
            model.AddConstraint("demand3", 0.2 * sa + 0.3 * vz + lubricantBuy >= lubricantDemand);
            // solve
            Solution solution = context.Solve();
            Report report = solution.GetReport();

There seems to be very little documentation on how to use Solver Foundation, particularly with the ScenariosParameter. Does anyone know of a good information source?

Now trying to remind myself what is good in my life and that this is just another one of those 'interesting' problems that make software such an enjoyable job :/
Posted 1-Jul-13 23:06pm
Updated 1-Jul-13 23:07pm

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170609.2 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100