using ILOG.Concert;
using ILOG.CPLEX;
using System;

public class Rand {
    public int[] iResult = new int[1000];


    public void random_int(int iDown, int iUp , int number)
    {    
       Random ro = new Random();
           
        for (int i = 0; i < number; i++)
        {
           iResult[i] = ro.Next(iDown, iUp);         
        }  

    }
}

public class CLSP
{
    internal static double M = 10000;
    internal static int Up = 10;
    internal static int Down = 1;
    internal static int Period = 50;
    internal static int Product = 20;
    internal static int[][] Demand =new int[Product][];
    internal static double[] Process_time = new double[Product];


    internal static double[] Cost_production =new double[Product];
    internal static double[] Cost_inventory = new double[Product];
    internal static double[] Cost_setup = new double[Product];
    internal static double Cost_PM = 1000;
    internal static int Capacity = 500;
    internal static int PM_length = 1000;

    public static void Main(string[] args)
    {



        for (int i = 0; i < Product; i++)
        {
            Rand rand = new Rand();
             rand.random_int(1, 5, Product);
             Process_time[i] = rand.iResult[i];
             Console.WriteLine(" {0}/ ", Process_time[i]);
            Cost_production[i]=5 ;
            Cost_inventory[i] = 2;
            Cost_setup[i] = 20;
            Demand[i] = new int[Period];
        }

      //  Console.Read();

    


          for (int i= 0; i < Product; i++)
             {          
                 for (int j = 0; j < Period; j++)                       
                 {
                     Rand rand = new Rand();
                     rand.random_int(Down, Up, Period);
                  
                      Demand[i][j] = rand.iResult[j];
                     Console.Write(" {0}/ ",Demand[i][j]);
                 }
                 Console.WriteLine("  ");
             }
     
            
    //  Console.Read();

         

       
            try
            {
                Cplex cplex = new Cplex();
                INumVar[][] x = new INumVar[Product][];
                INumVar[] w = new INumVar[Period];
                INumVar[] u = new INumVar[Period];
                for (int i = 0; i < Product; i++)
                {
                    x[i] = cplex.IntVarArray(Period, 0, 1000);  //production is above or equal to zero
                }
                for (int i = 0; i < Period; i++)
                {
                    w[i] = cplex.NumVar(0, M);
                    u[i] = cplex.BoolVar(); 
                }
                INumVar[][] y = new INumVar[Product][];
                for (int i = 0; i < Product; i++)
                {
                    y[i] = cplex.BoolVarArray(Period);              //yit either 0 or 1
                }


                INumVar[][] I = new INumVar[Product][];
                for (int i = 0; i < Product; i++)
                {
                    I[i] = cplex.IntVarArray(Period, 0, 1000);      //inventory is above or equal to zero 
                }



                // Objective Function: Minimize Cost
                ILinearNumExpr ProdCost = cplex.LinearNumExpr();
                ILinearNumExpr SetCost = cplex.LinearNumExpr();
                ILinearNumExpr InvCost = cplex.LinearNumExpr();
                ILinearNumExpr PMCost = cplex.LinearNumExpr();

                for (int i = 0; i < Product; i++)
                {
                    for (int t = 0; t < Period; t++)
                    {
                        ProdCost.AddTerm(Cost_production[i], x[i][t]);
                        SetCost.AddTerm(Cost_setup[i], y[i][t]);
                        InvCost.AddTerm(Cost_inventory[i], I[i][t]);
                       
                    }
                }
                for (int t = 0; t < Period; t++)
                {
                    PMCost.AddTerm(Cost_PM, u[t]);
                }
                cplex.AddMinimize(cplex.Sum(ProdCost, SetCost, InvCost, PMCost));

                //the capacity constraint
                for (int t = 0; t < Period; t++)
                {
                    ILinearNumExpr time_sum= cplex.LinearNumExpr();
                    for (int i = 0; i < Product; i++)
                    {
                        time_sum.AddTerm(Process_time[i], x[i][t]);
                    }
                    cplex.AddEq(u[0], 1);
                  if (t == 0)
                    {
                        cplex.AddEq(w[0], time_sum);
                      
                    }
                   if(t>0) cplex.AddGe(cplex.Diff(w[t], time_sum), cplex.Diff(w[t-1], cplex.Prod(u[t],M)));
   
                        cplex.AddGe(w[t], time_sum);
                        cplex.AddLe(w[t],PM_length);
             
                        cplex.AddLe(time_sum, Capacity);
                  
              
                 
                }

                //constraint between Xit and Yit
                for (int i = 0; i < Product; i++)
                {
                    for (int t = 0; t < Period; t++)
                    {
                        cplex.AddLe(x[i][t], cplex.Prod(M, y[i][t]));
                    }
                }

                //equation of the production and inventory

                for (int i = 0; i < Product; i++)
                {

                    cplex.AddGe(x[i][0], Demand[i][0]);
                    cplex.AddEq(cplex.Sum(I[i][0], Demand[i][0]), x[i][0]);

                    for (int t = 1; t < Period; t++)
                    {
                        cplex.AddEq(cplex.Sum(x[i][t], I[i][t - 1]), cplex.Sum(Demand[i][t], I[i][t]));
                    }
                }
                for (int i = 0; i < Product; i++)
                {

                    cplex.AddLe(w[i], PM_length);
                    cplex.AddEq(cplex.Sum(I[i][0], Demand[i][0]), x[i][0]);

                    for (int t = 1; t < Period; t++)
                    {
                        cplex.AddEq(cplex.Sum(x[i][t], I[i][t - 1]), cplex.Sum(Demand[i][t], I[i][t]));
                    }
                }

                if (cplex.Solve())
                {



                    System.Console.WriteLine();
                    System.Console.WriteLine("Total cost = " + cplex.ObjValue);

                    System.Console.WriteLine();
                  System.Console.WriteLine("\tp\tt\tx\ty\tInventory");

                  for (int i = 0; i < Product; i++)
                  {
                      for (int t = 0; t < Period; t++)
                      {
                          double x_s = cplex.GetValue(x[i][t]);
                          double y_s = cplex.GetValue(y[i][t]);
                          double I_s = cplex.GetValue(I[i][t]);
                          System.Console.WriteLine("\t" + i + "\t" + (t + 1) + "\t" + cplex.GetValue(x[i][t])+ "  "+x_s +
                                                   "\t" + cplex.GetValue(y[i][t]) + "  "+y_s + "\t" + cplex.GetValue(I[i][t]));
                      }
                  }



                    System.Console.WriteLine("Total cost = " + cplex.ObjValue);         
                      
                           for (int t = 0; t < Period; t++)
                           {
                               System.Console.WriteLine("\t" + cplex.GetValue(u[t]) +"\t"+ cplex.GetValue(w[t]));
                           }
                      
                    cplex.End();
                }
            }
            catch (ILOG.Concert.Exception exc)
            {
                System.Console.WriteLine("Concert exception '" + exc + "' caught");
            }
        Console.Read();

    }



}

posted on 2011-05-17 08:57  Elitez  阅读(1267)  评论(0编辑  收藏  举报