Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# code for simplex method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static string[] variables = { "a", "b", "c", "d", "e", "f", "g" };
        static string[] input = 
        {
            "max 4 5",
            "9 8 <= 45",
            "5 1 <= 29",
            "1 7 <= 15"
        };
        static  List<List<double>> coeifficiants1 = new List<List<double>>();
        static  List<List<double>> coeifficiants2 = new List<List<double>>();
        static List<string> operations = new List<string>();
        static string command;
        static List<double> constraints = new List<double>();
        static List<string> output = new List<string>();
        static void Main(string[] args)
        {
            ReadInput();
            CreateCommand();
        }
        static void ReadInput()
        {
            string[] fields;
            double newDouble;
            string operation = "";
            //read input lines
            foreach (string line in input)
            {
                fields = line.Split(' ');
                //check if line is command - not starting with a number
                if (double.TryParse(fields[0], out newDouble))
                {
                    Boolean firstMatrix = true;
                    List<double> matrix1row = new List<double>();
                    List<double> matrix2row = new List<double>();
                    //line contains coeifficants
                    foreach (string field in fields)
                    {
                        if (!double.TryParse(field, out newDouble))
                        {
                            operation = field;
                            firstMatrix = false;
                        }
                        else
                        {
                            newDouble = double.Parse(field);
                            if (firstMatrix == true)
                                matrix1row.Add(newDouble);
                            else
                                matrix2row.Add(newDouble);
                        }
                    }
                    coeifficiants1.Add(matrix1row);
                    coeifficiants2.Add(matrix2row);
                    operations.Add(operation);
                }
                else
                {
                    command = fields[0];
                    for (int i = 1; i < fields.Length; i++)
                    {
                        newDouble = double.Parse(fields[i]);
                        constraints.Add(newDouble);
                    }
                }
            }
        }
        static void CreateCommand()
        {
            string outputline = "";
            switch (command)
            {
                case "max":
                    outputline = "Maximize p = ";
                    break;
            }
            for(int i = 0; i < constraints.Count; i++)
            {
                if (i == 0)
                {
                    outputline += constraints[i].ToString();
                }
                else
                {
                    outputline += "+ ";
                    outputline += constraints[i].ToString();
                }
                outputline += variables[i];
             
            }
            outputline += " subject to";
            output.Add(outputline);
            
            for (int row = 0; row < coeifficiants1.Count; row++)
            {
                double[] coeifficiants = coeifficiants1[row].ToArray();
                outputline = "";
                for (int i = 0; i < coeifficiants.Length; i++)
                {
                    if (i == 0)
                    {
                        outputline += coeifficiants[i].ToString();
                    }
                    else
                    {
                        outputline += " + ";
                        outputline += coeifficiants[i].ToString();
                    }
                    outputline += variables[i];
                }
                outputline += " " + operations[row] + " "  +coeifficiants1[row][0];
                output.Add(outputline);
            }
 
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: sql to linq converter online tool free 
Csharp :: Console.WriteLine($"Hello {Ana.ToUpper($)}!"); 
Csharp :: array hw exercise 
Csharp :: wpf onpropertychanged not working 
Csharp :: cmd.executenonquery() error in c# 
Csharp :: Razor switch statement 
Csharp :: C# walk down a tree and back 
Csharp :: c# blazor update state 
Csharp :: htmlgenericcontrol class c# 
Csharp :: unity shader blend 
Csharp :: wpf string to byte array 
Csharp :: my object is falling unity 
Csharp :: asp.net stop page jumping to top on click 
Csharp :: asp net identity login failed for user 
Csharp :: c# capitalize first letter of each word 
Csharp :: c# string is all zeros 
Csharp :: initialize c# array property of class object site:stackoverflow.com 
Csharp :: remove language folders build visual studio 
Csharp :: List picking records from database 
Csharp :: log4net rollingfileappender c# 
Csharp :: c# sha512 salt 
Csharp :: c# check if list is empty 
Csharp :: Rotate Object with keyboard 
Csharp :: unity organize variables in inspector 
Csharp :: properties vs field c# 
Csharp :: player movement script unity 
Csharp :: c# fastest way to find item in list 
Csharp :: unity c# store gameobject in array 
Csharp :: c# changimg to one decimal place 
Csharp :: WixSharp-FirewallException 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =