C# how to use LOOPS for linear programming models
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C# how to use LOOPS for linear programming models

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    8

    C# how to use LOOPS for linear programming models

    Hi All;

    I need help on using loops to solve linear programming models.

    Here is my code in C# using Microsoft solver foundation to solve a linear program. But I don't want to write all constraints one by one, I just want to use loops to solve this linear model.

    If anyone knows, PLEASE help me!!!!

    Code:
    using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using Microsoft.SolverFoundation.Common;
     using Microsoft.SolverFoundation.Services;
    
    
     namespace PetroChem
     {
     class Program
     {
     static void Main(string[] args)
     {
     SolverContext context = SolverContext.GetContext();
     Model model = context.CreateModel();
    
     Decision x11 = new Decision(Domain.IntegerNonnegative, "x11");
     Decision x12 = new Decision(Domain.IntegerNonnegative, "x12");
     Decision x13 = new Decision(Domain.IntegerNonnegative, "x13");
     Decision x14 = new Decision(Domain.IntegerNonnegative, "x14");
     Decision x15 = new Decision(Domain.IntegerNonnegative, "x15");
     Decision x21 = new Decision(Domain.IntegerNonnegative, "x21");
     Decision x22 = new Decision(Domain.IntegerNonnegative, "x22");
     Decision x23 = new Decision(Domain.IntegerNonnegative, "x23");
     Decision x24 = new Decision(Domain.IntegerNonnegative, "x24");
     Decision x25 = new Decision(Domain.IntegerNonnegative, "x25");
     Decision x31 = new Decision(Domain.IntegerNonnegative, "x31");
     Decision x32 = new Decision(Domain.IntegerNonnegative, "x32");
     Decision x33 = new Decision(Domain.IntegerNonnegative, "x33");
     Decision x34 = new Decision(Domain.IntegerNonnegative, "x34");
     Decision x35 = new Decision(Domain.IntegerNonnegative, "x35");
     Decision x41 = new Decision(Domain.IntegerNonnegative, "x41");
     Decision x42 = new Decision(Domain.IntegerNonnegative, "x42");
     Decision x43 = new Decision(Domain.IntegerNonnegative, "x43");
     Decision x44 = new Decision(Domain.IntegerNonnegative, "x44");
     Decision x45 = new Decision(Domain.IntegerNonnegative, "x45");
     Decision x51 = new Decision(Domain.IntegerNonnegative, "x51");
     Decision x52 = new Decision(Domain.IntegerNonnegative, "x52");
     Decision x53 = new Decision(Domain.IntegerNonnegative, "x53");
     Decision x54 = new Decision(Domain.IntegerNonnegative, "x54");
     Decision x55 = new Decision(Domain.IntegerNonnegative, "x55");
    
     model.AddDecisions(x11, x12, x13, x14, x15,
     x21, x22, x23, x24, x25,
     x31, x32, x33, x34, x35,
     x41, x42, x43, x44, x45,
     x51, x52, x53, x54, x55 );
    
     model.AddConstraint("Row1", x11 + x12 + x13 + x14 + x15 >= 70);
     model.AddConstraint("Row2", x21 + x22 + x23 + x24 + x25 >= 80);
     model.AddConstraint("Row3", x31 + x32 + x33 + x34 + x35 >= 90);
     model.AddConstraint("Row4", x41 + x42 + x43 + x44 + x45 >= 85);
     model.AddConstraint("Row5", x51 + x52 + x53 + x54 + x55 >= 90);
     model.AddConstraint("Row6", x11 + x21 + x31 + x41 + x51 >= 110);
     model.AddConstraint("Row7", x12 + x22 + x32 + x42 + x52 >= 120);
     model.AddConstraint("Row8", x13 + x23 + x33 + x43 + x53 >= 130);
     model.AddConstraint("Row9", x14 + x24 + x34 + x44 + x54 >= 120);
     model.AddConstraint("Row10", x15 + x25 + x35 + x45 + x55 >= 100);
    
    
     model.AddGoal("Goal", GoalKind.Minimize, x11 + x12 + x13 + x14 + x15 +
     x21 + x22 + x23 + x24 + x25 +
     x31 + x32 + x33 + x34 + x35 +
     x41 + x42 + x43 + x44 + x45 +
     x51 + x52 + x53 + x54 + x55 );
    
     Solution solution = context.Solve(new SimplexDirective());
    
     Report report = solution.GetReport();
     Console.WriteLine("x1: {0}, x2: {1}, x3: {2}", x11, x12, x13, x14, x15,
     x21, x22, x23, x24, x25,
     x31, x32, x33, x34, x35,
     x41, x42, x43, x44, x45,
     x51, x52, x53, x54, x55);
     Console.Write("{0}", report);
     Console.ReadLine();
    
    
    
    
    
     }
     }
     }

  2. #2
    Join Date
    May 2008
    Posts
    36

    Re: C# how to use LOOPS for linear programming models

    If you want to use a loop then at each iteration of the loop you must be able to work out what to do.

    I can not see a way as is to loop the constraints but maybe with more information I might....
    Do your X variables represent a square matrix, and you are constraing the sum of columns and rows to certain amounts?


    Here is the code I have so far (untested):

    Code:
                SolverContext context = SolverContext.GetContext();
                Model model = context.CreateModel();
    
                List<Decision> allDecision = new List<Decision>();
                Term totalDecision = null;
    
                //Delcare all decisions
                {
                    for (int i = 1; i <= 25; i++)
                        allDecision.Add(new Decision(Domain.IntegerNonnegative, "x" + i));
                }
    
                //Add all decisions to model and get the total decision
                {
                    bool setTotalDecision = true;
                    foreach (Decision d in allDecision)
                    {
                        model.AddDecision(d);
    
                        if (setTotalDecision)
                        {
                            totalDecision = d;
                            setTotalDecision = false;
                        } else
                            totalDecision = totalDecision + d;
                    }
                }
    
                model.AddGoal("Goal", GoalKind.Minimize, totalDecision);
    Last edited by gleesonger; July 28th, 2013 at 07:37 PM.

  3. #3
    Join Date
    Jun 2013
    Posts
    8

    Re: C# how to use LOOPS for linear programming models

    Thank you gleesonger for helping me. using your code I solved my problem.

    Can you help me on another problem. I have a linear programming model/formulation and I have some data with some rows and columns like a matrix, the data is in a text file. my code needs to load the data from that text file and use it to solve the linear programming model.
    if you want to help me, please give me your email so i can send you the problem file (word file).

    Thanks

  4. #4
    Join Date
    May 2008
    Posts
    36

    Re: C# how to use LOOPS for linear programming models

    Quote Originally Posted by musademirtas View Post
    Thank you gleesonger for helping me. using your code I solved my problem.

    Can you help me on another problem. I have a linear programming model/formulation and I have some data with some rows and columns like a matrix, the data is in a text file. my code needs to load the data from that text file and use it to solve the linear programming model.
    if you want to help me, please give me your email so i can send you the problem file (word file).

    Thanks
    If you post up your question here I can have a look at it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center