Thursday, May 16, 2013


used in operatonal research LP(linear programming)
The Simplex Algorithm

Simplex method

Resolve using the Simplex Method the following problem:
Maximize Z = f(x,y) = 3x + 2y
subject to: 2x + y ≤ 18
  2x + 3y ≤ 42
  3x + y ≤ 24
  x ≥ 0 , y ≥ 0
Are considerated the following phases:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simplex_Method
{
    class simplex
    {
        int countslock;
        int frow;
        int fcol;
        double[,] dt = { { 1, -8, -6, 0, 0, 0, 0 }, { 0, 2, 7, 1, 0, 100, 0 }, { 0, 1, 3, 0, 1, 50, 0 } };
        double[] temprowdata1 ;
       

        public simplex()
        {
           
            Console.WriteLine("the data is");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                  Console.Write( dt[i,j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine();
            temprowdata1 =  new double[dt.GetLength(1)];
            aditioniteration();
            //operation(dt);
            //double[] tempdt = {30,20,50,15,10,100};
            //ratiorow(tempdt);
           // Console.WriteLine(dt.GetLength(0));
        }
        public string check_cons(string a)
        {
            countslock++;
            if (a.Contains("<="))
            {
                return a.Replace("<", "+s" + countslock);
            }
            else if (a.Contains(">="))
            {
                return a.Replace(">", "-s" + countslock);
            }
            return "error";
        }
        public string[] dataextract(string a)
        {
           
            string[] data;
            int dtcounter =0;
            data = new string[a.Length];
            string sec = "";
            for (int i = 0; i <a.Length; i++)
            {
   
                if (a[i] != 'x')
                {
                    sec += a[i];
                }
                else { i++;
                    data[dtcounter] = sec; dtcounter++; sec = ""; }
             
            }
           data[dtcounter] = sec;
            for (int i = 0; i <data.Length; i++)
            {
                if (data[i] != null)
                {
                    if (data[i].Length == 1 && i > 0 )
                    { data[i] += "1"; }
                    if (data[i].Contains("="))
                    {
                 
                        string[] s = data[i].Split('=');
                        bool ass = false;
                        foreach (var item in s)
                        {
                            if (ass)
                            { data[i + 1] = item; }
                            if (item.Contains('-'))
                            { data[i] = "-1"; ass = true;  }
                            else if(!item.Contains('-') && !ass) { data[i] = "1"; ass = true; }
                        }
                    }
                }
           
            }
            //for (int i = 0; i < data.Length; i++)
            //{
            //    Console.WriteLine(data[i]);
            //}
         
            return data;
        }

        public void problemsolve(double[,] a)
        {
            if (minind(a) > 0)
            {

            }
        }


        public double[] operation(double[,] a)
        {
            int row = 0;
            int coloumn = 0;
            double pivotelement = 0;
            double m = 0;
            double[] ratiodata = new double[a.GetLength(0)];
            bool readytogo = false;
            for (int i = 0; i < a.GetLength(0); i++)
            {
                //least data containg coloumn index
                if (a[0, i] < m)
                {
                    m = a[0, i];
                    coloumn = i;
                }
            }
            if (readytogo)
            {
                for (int i = 1; i < a.GetLength(0); i++)
                {
                    //least ratio calculating
                    ratiodata[i] = (a[i, a.GetLength(1) - 2] / a[i, Convert.ToInt32(coloumn)]);
                }

                //selecting row data for next operation to bring zero in data one
                if (minratiocheck(ratiodata))
                {
                    double[] temprowdata = new double[a.GetLength(1)];

                    row = ratiorow(ratiodata);
                    pivotelement = a[row, coloumn];

                    if (pivotelement > 0 && pivotelement == 1)
                    {
                        // if pivot = 1 take data as it
                        for (int i = 0; i < a.GetLength(1); i++)
                        {
                            temprowdata1[i] = (a[row, i]);
                            Console.WriteLine(temprowdata[i]);
                        }
                    }
                    else
                    {
                        //pivot element row equalizing to one and then take data
                        for (int i = 0; i < a.GetLength(1); i++)
                        {
                            temprowdata1[i] = (a[row, i] / a[row, coloumn]);
                        }

                    }
                }
                frow = row;
                fcol = coloumn;
                return temprowdata1;
            }
        }
        public void aditioniteration()
        {
            double[] arr = operation(dt);
            double[] arrtemp = new double[arr.Length];
            double leastvalue = dt[0, fcol];
            for (int i = 0; i <arr.Length; i++)
            {
                 arrtemp[i] = arr[i] * (leastvalue *(-1));
                Console.WriteLine(arrtemp[i]);
            }
        }
        public bool minratiocheck(double[] ratio)
        {
            for (int i = 0; i < ratio.Length; i++)
            {
                if (ratio[i] > 0)
                {
                    return true;
                }
            }
            return false;
        }
        public int ratiorow(double[] ratio)
        {
            double m =500000000;
            int row = 0;
            for (int i = 0; i < ratio.Length; i++)
            {
                if (m > ratio[i] && ratio[i] != 0)
                {
                    if (ratio[i] > 0)
                    {
                        m = ratio[i];
                        row = i;
                    }

                }
     
            }
            return row;
        }
        public double minind(double[,] a)
        {
            double m = 0;
            double ind = 0;
            for (int i = 0; i < a.GetLength(0); i++)
            {
                if (a[0, i] < m)
                {
                    m = a[0, i];
                    ind = i;
                }
            }
            if (ind > 0)
            {
                return ind;
            }
            return 0.0;
        }
        public void ratiocheck(double[,] a)
        {
            double minvalue = minind(a);
            double[] ratiodt = new double[a.GetLength(0)];


            Console.WriteLine("minvalue   " + minvalue);
            for (int i = 1; i < a.GetLength(0); i++)
            {
                ratiodt[i] = a[i, Convert.ToInt32(minvalue)] / a[i, a.GetLength(1) - 2];
                Console.WriteLine(ratiodt[i]);
                Console.WriteLine(a[i, a.GetLength(1) - 1]+" world war");
            }          
        }
    }
    class Program
    {
       
   
        static void Main(string[] args)
        {
            simplex sim = new simplex();


            /*
           
            int[] arr = { 0, 0,50, 50, 60 ,-90,0,0,10,10,20};
            int m = 1000000;
            for (int i = 1; i < arr.Length; i++)
            {
                if (m > arr[i] && arr[i] != 0 )
                {
                    if (arr[i] > 0)
                    {
                        m = arr[i];
                        Console.WriteLine(m);
                        Console.WriteLine(i);
                    }
                }
            }
              important min function
            */
            /*
        //    sim.dataextract("");
            string[] constraint = { "20x1-10x2+0x3>=50", "1x1+3x2+x3>=25", "-1x1+2x2+0x3<=26" };
            //for (int i = 0; i < constraint.Length; i++)
            //{

            //    string[] values = sim.dataextract(sim.check_cons(constraint[i]));
         
            //    foreach (var item in values)
            //    {
            //        if (i >= 1)
            //        {
            //            Console.Write(item + " ");
            //        }
            //        Console.WriteLine();
                 
            //    }

            //}



            //string maxz = "Z=2x1+3x2+4x3";
            //maxz = maxz.Replace("+", "-");
            //maxz = maxz.Replace("=", "-");
            //maxz += "=0";
            //Console.WriteLine(maxz);


            for (int i = 0; i < constraint.Length; i++)
            {
                //   Console.WriteLine(sim.check_cons(constraint[i]));
                string[] values = sim.dataextract(sim.check_cons(constraint[i]));
                foreach (var item in values)
                {
                    Console.Write(item + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("refind data");
            sim.dataextract("90x1-7x2+1x3-s2=25");
            */
        }
    }
}

Android simple application by adding 2 values

ok this is csharp blog and quit inactive for a long time i have been not posting any thing about c-sharp and that is because i don't much work to do on c-sharp for. but main purpose of this is blog is online codes for programming whether its c-sharp or java or android.
so today i am going to post the complete code of a little android test application.

XML

<?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="ADDITION OF NUMBERS"
     android:id="@+id/textView1"
     android:layout_x="73dip"
     android:layout_y="28dip"></TextView>
    <TextView android:layout_width="wrap_content"
    android:layout_x="36dip"
    android:layout_height="wrap_content"
    android:text="First Amount"
    android:id="@+id/textView2"
    android:layout_y="80dip"></TextView>
    <EditText android:text=""
    android:layout_width="wrap_content"
    android:layout_x="172dip"
    android:id="@+id/amount1"
    android:layout_height="wrap_content"
    android:layout_y="62dip"></EditText>
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second Amount"
    android:id="@+id/textView3"
    android:layout_x="36dip"
    android:layout_y="169dip"></TextView>
    <EditText android:text=""
    android:layout_width="wrap_content"
    android:layout_x="169dip"
    android:id="@+id/amount2"
    android:layout_height="wrap_content"
    android:layout_y="152dip"></EditText>
    <Button android:layout_width="wrap_content"
    android:id="@+id/calculate"
    android:layout_x="41dip"
    android:layout_height="wrap_content"
    android:text="Calculate"
    android:layout_y="232dip"></Button>
    <EditText android:text=""
    android:layout_width="wrap_content"
    android:layout_x="172dip"
    android:id="@+id/tt"
    android:layout_height="wrap_content"
    android:layout_y="232dip"></EditText>
    </AbsoluteLayout>

Main Activity
package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText amount1;
 EditText amount2;
 TextView tt;
 Button calculate;
 double x=0;
 double y=0;
 double z=0;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     initControls();
 }
 private void initControls()
 {
     amount1=(EditText)findViewById(R.id.amount1);
     amount2=(EditText)findViewById(R.id.amount2);
     tt=(TextView)findViewById(R.id.tt);
     calculate=(Button)findViewById(R.id.calculate);
     calculate.setOnClickListener(new Button.OnClickListener(){public void onClick
     (View  v) { calculate();}});
 }
 private void calculate()
 {
     x=Double.parseDouble(amount1.getText().toString());
     y=Double.parseDouble(amount2.getText().toString());
     z=x+y;
     tt.setText(Double.toString(z));
     calculate.setText(Double.toString(z));
 }
}

used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...