⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 solving multidimensionalknapsackproblem.cs

📁 Solve multi soures Kinapping problem
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DPH_Multidimensional_Knapsack_Problem
{
    public partial class DP : Form
    {
        public DP()
        {
            InitializeComponent();
        }
         
        /*本次作業練習目的:
         * DP的精神以及Agorithim
         * I/O的使用
         */
        private void DP_Load(object sender, EventArgs e)
        {

            //this.reportViewer1.RefreshReport();
        }

      

        private void DPLoad_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            DataSet.Text = openFileDialog1.FileName;
        }
        //StreamReader DpReader;
        //Stream DpRead;

        List<string> Data = new List<string>();
        
        List<string> DataLine = new List<string>();
        int TaskCount = 0, ResourceCount = 0;


        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

            /**********************************************************************/
            //                    讀檔          
            //MessageBox.Show("OK");
            //F1說明教的 用Streamreader讀取 並將其NEW成唯讀的Stream

            StreamReader DpRead = new StreamReader(openFileDialog1.FileName);
            //DPRichBox.Text = DpRead.ReadToEnd();
            //如果先讀到底則已經讀到行底,再readline只會讀不到
            string tempt, tempt2;
            tempt = DpRead.ReadLine();
            DataLine.Add(tempt);
            while (tempt != null)
            {
                tempt2 = tempt + "\n";
                tempt = DpRead.ReadLine();
                DataLine.Add(tempt);
            }
            
            DpRead.Close();
            DpRead = null;
            for (int y = 0; y < DataLine.Count; y++)
            {
                DPRichBox.Text = DPRichBox.Text + DataLine[y] + "\n";
            }


            TaskCount = Convert.ToInt32(DataLine[0]);
            ResourceCount = Convert.ToInt32(DataLine[1]);

            List<string[]> Task_i = new List<string[]>();
            int[,] Weight = new int[TaskCount, ResourceCount + 1];
            for (int x = 0; x < TaskCount; x++)
            {
                string[] TemptString = new string[ResourceCount + 1];
                TemptString = DataLine[x + 2].Split(' ');
                //MessageBox.Show(TemptString[ResourceCount]);
                for (int y = 0; y <= ResourceCount; y++)
                {
                    Weight[x, y] = int.Parse(TemptString[y]);
                    //MessageBox.Show(Weight[x, y] .ToString()+" "+y.ToString());
                }

            }
            int[] ResourceBound = new int[ResourceCount];
            //int[,] ResourceBound = new int[2,ResourceCount];
            string[] TemptString2 = new string[ResourceCount];
            TemptString2 = DataLine[TaskCount + 2].Split(' ');
            for (int y = 0; y < ResourceCount; y++)
            {
                ResourceBound[y] = int.Parse(TemptString2[y]);
                //MessageBox.Show(ResourceBound[y].ToString());
            }


            //MessageBox.Show("TaskNumber:" + TaskCount.ToString() + "\nResourceNumber:" + ResourceCount.ToString());
            /*
            Point resource = new Point(1, 1);
            char x = DPRichBox.GetCharFromPosition(resource);
            tempt = Convert.ToString(x);
            MessageBox.Show(tempt);
           */
            /* while (DpRead.ReadLine() == null)
             {
                 //if (DpRead.ReadLine() != "")
                 tempt=DpRead.ReadLine();
                 //else
                     //break;
                     MessageBox.Show(tempt);
                     temp++;
             }
             */
            ///////////////////////////////////////////////////////////////////////////////
            
            //紀錄選擇策略
            
            //For V1
            //int[] Y_resourceused = new int[ResourceCount+1];
            //int[,] Y_resourceused = new int[ TaskCount+1,ResourceCount + 1];
            //List<int[]> Resourceused = new List<int[]>();
            //int[] tempR = new int[ResourceCount + 1];
            Boolean[] PeriodPolicy = new Boolean[TaskCount + 1];
            PeriodPolicy[0] = true;
            int[] policyvalue = new int[TaskCount + 1];
            policyvalue[0] = 0;
            int[] Optimalvalue = new int[2];
            Optimalvalue[0] = 0;
            Optimalvalue[1] = 0;

            int legal =1;
            int legalTemp=0;
            int[,] Policy = new int[2, 2];  
            int[,] Y_resourceused = new int[2, ResourceCount + 1];

            for (int period = 1; period <= TaskCount; period++)
            {
                //令出本次決策所有可能的State
                
                int [,]Rs_USD=new int[2*legal,ResourceCount+1];
                int[] Reward = new int[2 * legal];
                int [,] StrategyINT = new int[2*legal, period+1];

                Boolean[] IllegalorNot = new Boolean[2*legal];
                int[] RS_OK = new int[2*legal];
                ////////////將上次成功的strategy(Policy[])存入陣列StrategyINT[]
                if (period != 1)
                    for (int i = 0; i <legal; i++)
                    {  
                        for(int p = 1; p<period; p++)
                        {
                            StrategyINT[i,p] =Policy[i,p];
                        }
                    }
                else
                {
                    StrategyINT[0,0] = 0;
                    StrategyINT[1,0] = 1;
                }

                    
                    for (int i = 0; i < legal; i++)
                    {
                        for (int Ry = 1; Ry <= ResourceCount; Ry++)
                        {
                            if (Y_resourceused[i,Ry] + Weight[period-1, Ry] <= ResourceBound[Ry - 1])
                            {
                                RS_OK[i]++;
                            }
                        }
                        if (RS_OK[i] == ResourceCount)
                        {
                            IllegalorNot[i] = false;
                            for (int Ry = 1; Ry <= ResourceCount; Ry++)
                            {
                                Rs_USD[2*i,Ry] = Y_resourceused[i,Ry];
                            }
                            Reward[2 * i] = Optimalvalue[i];
                            legalTemp++;
                            for (int Ry = 1; Ry <= ResourceCount; Ry++)
                            {
                                Rs_USD[((i+1)*2-1),Ry] = Y_resourceused[i,Ry] + Weight[period-1, Ry];
                            }
                            Reward[((i + 1) * 2 - 1)] = Optimalvalue[i]+Weight[period-1,0];
                            legalTemp++;
                        }
                        else
                        { 
                            IllegalorNot[i] = true;
                            for (int Ry = 1; Ry <= ResourceCount; Ry++)
                            {
                                Rs_USD[i * 2,Ry] = Y_resourceused[i,Ry];
                            }
                            Reward[2 * i] = Optimalvalue[i];
                            legalTemp++;
                        }
                    }
                
                int [,] TempINT= new int[legal, period];

                Y_resourceused = new int[legalTemp,ResourceCount+1];
                
                Policy = new int[legalTemp,period+1];  
                Optimalvalue=new int [legalTemp];
                 
                int LegalIndex=0;
                     for(int i=0;i<legal;i++)
                {

                    if (IllegalorNot[i] == false)
                    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -