Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Visual-Studio .NET4
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();
            context.ClearModel();
            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();
            Console.WriteLine(report);
            Console.ReadLine();
        }
    }
}
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 2-Jul-13 0:06am
Edited 2-Jul-13 0:07am
v2

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2014
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