Browse Source

Merge branch 'featAlgDzh'

klmnvan 5 months ago
parent
commit
9a97147cb8

+ 93 - 0
MathModelingSimulator/Function/Johnson_sAlgorithm.cs

@@ -0,0 +1,93 @@
+using DynamicData;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MathModelingSimulator.Function
+{
+    public class Johnson_sAlgorithm
+    {
+        List<(double, double)> listValues = new List<(double, double)>(); //Лист изначальных значений
+        List<(double, double)> listValuesResult = new List<(double, double)>(); //Лист итоговых значений
+        List<(double, double)> ignore = new List<(double, double)>(); //Лист игнорируемых значений (тех, которые уже были добавлены в listValuesResult)
+        List<(double, double)> startValues = new List<(double, double)>(); //Значения 1 столбца
+        List<(double, double)> endValues = new List<(double, double)>(); //Значения 2 столбца
+        List<double> timeP = new List<double>(); //Время простоя 2 станка по изначальному плану
+        List<double> timePRezult = new List<double>(); //Время простоя 2 станка по итоговому плану
+
+        /// <summary>
+        /// Стартовый метод программы
+        /// </summary>
+        public string Start(int[,] arValues)
+        {
+            if(arValues.GetLength(1) == 2)
+            {
+                for(int i = 0; i < arValues.GetLength(0); i++)
+                {
+                    listValues.Add((arValues[i, 0], arValues[i, 1]));
+                }
+                MethodJohnson();
+                return timePRezult.Max().ToString();
+            } 
+            else
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// Метод Джонсона
+        /// </summary>
+        void MethodJohnson()
+        {
+            for (int i = 0; i < listValues.Count; i++)
+            {
+                double min1 = listValues.Where(it => !ignore.Contains(it)).Select(it => it.Item1).ToList().Min(); //Минимальное время обработки на 1 станке
+                double min2 = listValues.Where(it => !ignore.Contains(it)).Select(it => it.Item2).ToList().Min(); //Минимальное время обработки на 2 станке
+                if (min1 <= min2)
+                {
+                    var listMinStart = listValues.Where(it => it.Item1 == min1).ToList();
+                    double maxEl1St = listMinStart.Select(it => it.Item2).Max();
+                    int indexMax = listMinStart.Select(it => it.Item2).ToList().IndexOf(maxEl1St);
+                    startValues.Add(listMinStart[indexMax]);
+                    ignore.Add(listMinStart[indexMax]);
+                }
+                else
+                {
+                    var listMinEnd = listValues.Where(it => it.Item2 == min2).ToList();
+                    double maxEl1St = listMinEnd.Select(it => it.Item1).Max();
+                    int indexMax = listMinEnd.Select(it => it.Item1).ToList().IndexOf(maxEl1St);
+                    endValues.Insert(0, listMinEnd[indexMax]);
+                    ignore.Add(listMinEnd[indexMax]);
+                }
+            }
+            listValuesResult = startValues.Concat(endValues).ToList();
+            timeP.Add(listValues[0].Item1);
+            timePRezult.Add(listValuesResult[0].Item1);
+            for (int i = 1; i < listValuesResult.Count; i++)
+            {
+                double sumP = 0;
+                double sumPRezult = 0;
+                double sumP2 = 0;
+                double sumPRezult2 = 0;
+                for (int j = 0; j <= i; j++)
+                {
+                    sumP += listValues[j].Item1;
+                    sumPRezult += listValuesResult[j].Item1;
+                    if (j != i)
+                    {
+                        sumP2 += listValues[j].Item2;
+                        sumPRezult2 += listValuesResult[j].Item2;
+                    }
+                }
+                timeP.Add(sumP - sumP2);
+                timePRezult.Add(sumPRezult - sumPRezult2);
+            }
+        }
+
+    }
+
+}

+ 82 - 28
MathModelingSimulator/ViewModels/CreateSimulatorViewModel.cs

@@ -9,14 +9,15 @@ using DynamicData;
 using System.Diagnostics;
 using Avalonia.Media;
 using MathModelingSimulator.Views;
+using MathModelingSimulator.Function;
 
 namespace MathModelingSimulator.ViewModels
 {
 	public class CreateSimulatorViewModel : MainWindowViewModel
 	{
-		#region PropertyObjects
-		int countRows = 0;
-		public int CountRows
+        #region PropertyObjects
+        int? countRows = 0;
+		public int? CountRows
 		{
 			get => countRows;
 			set
@@ -26,8 +27,8 @@ namespace MathModelingSimulator.ViewModels
 			}
 		}
 
-		int countColumns = 0;
-		public int CountColumns
+        int? countColumns = 0;
+		public int? CountColumns
 		{
 			get => countColumns;
 			set
@@ -53,13 +54,19 @@ namespace MathModelingSimulator.ViewModels
 		public string SelectedSimulator { get => selectedSimulator; set => SetProperty(ref selectedSimulator, value); }
 
         string answer = "";
-        public string Answer { get => answer; set => answer = value; }
+        public string Answer { get => answer; set => SetProperty(ref answer, value); }
 		#endregion
 
 		int[,] _matrixBD;
 		int _idTask = 0;
 
-		public CreateSimulatorViewModel()
+        private string messageRezult = "";
+        public string MessageRezult { get => messageRezult; set => this.SetProperty(ref messageRezult, value); }
+
+        private bool isVisibleRezult = false;
+        public bool IsVisibleRezult { get => isVisibleRezult; set => this.SetProperty(ref isVisibleRezult, value); }
+
+        public CreateSimulatorViewModel()
 		{
 			listSimulators = ContextDb.Simulators.ToList();
 			listSimulatorsView = listSimulators.Select(it => it.Name).ToList<string>();
@@ -78,32 +85,79 @@ namespace MathModelingSimulator.ViewModels
 			this._idTask = idTask;
         }
 
+		public void GenerateAnswer()
+        {
+			if(CountRows != 0 && CountColumns != 0)
+			{
+                IsVisibleRezult = false;
+                var countRows = matrix.Children.Count;
+                var countColumns = (matrix.Children[0] as StackPanel).Children.Count;
+                _matrixBD = new int[countRows, countColumns];
+                for (int i = 0; i < countRows; i++)
+                {
+                    for (int j = 0; j < countColumns; j++)
+                    {
+                        var buffer = (matrix.Children[i] as StackPanel).Children[j].Name.Split(" ").Select(int.Parse).ToList();
+                        _matrixBD[Convert.ToInt32(buffer[0]), Convert.ToInt32(buffer[1])] = Convert.ToInt32(((matrix.Children[i] as StackPanel).Children[j] as TextBox).Text);
+                    }
+                }
+                switch (selectedSimulator)
+                {
+                    case "Ñèìïëåêñ ìåòîä": MessageRezult = "ß ïîêà íå óìåþ òàêîå ðåøàòü"; break;
+                    case "Çàäà÷à Êîììèâîÿæåðà": MessageRezult = "ß ïîêà íå óìåþ òàêîå ðåøàòü"; break;
+                    case "Òðàíñïîðòíûå çàäà÷è. Ìåòîä àïïðîêñèìàöèè Ôîãåëÿ": Answer = "ß ïîêà íå óìåþ òàêîå ðåøàòü"; break;
+                    case "Çàäà÷à Äæîíñîíà": GetAnswerZadDzhonsons(); break;
+                    case "Àëãîðèòì Äåéêñòðû": MessageRezult = "ß ïîêà íå óìåþ òàêîå ðåøàòü"; break;
+                }
+            }
+		}
+
+		void GetAnswerZadDzhonsons()
+		{
+            Johnson_sAlgorithm johnson_SAlgorithm = new Johnson_sAlgorithm();
+			var rezult = johnson_SAlgorithm.Start(_matrixBD);
+			if(rezult != null)
+			{
+                Answer = rezult.ToString();
+            }
+			else
+			{
+				IsVisibleRezult = true;
+                MessageRezult = "Âàøà ìàòðèöà íå ïîäõîäèò äëÿ ýòîé çàäà÷è";
+				Answer = "0";
+            }
+		}
+
         public void CreateTask()
 		{
-			var countRows = matrix.Children.Count;
-            var countColumns = (matrix.Children[0] as StackPanel).Children.Count;
-			_matrixBD = new int[countRows, countColumns];
-			for(int i = 0; i < countRows; i++)
+			if (CountRows != 0 && CountColumns != 0)
 			{
-                for (int j = 0; j < countColumns; j++)
+                var countRows = matrix.Children.Count;
+                var countColumns = (matrix.Children[0] as StackPanel).Children.Count;
+                _matrixBD = new int[countRows, countColumns];
+                for (int i = 0; i < countRows; i++)
+                {
+                    for (int j = 0; j < countColumns; j++)
+                    {
+                        var buffer = (matrix.Children[i] as StackPanel).Children[j].Name.Split(" ").Select(int.Parse).ToList();
+                        _matrixBD[Convert.ToInt32(buffer[0]), Convert.ToInt32(buffer[1])] = Convert.ToInt32(((matrix.Children[i] as StackPanel).Children[j] as TextBox).Text);
+                    }
+                }
+                SimulatorTask newTask = new SimulatorTask();
+                var idSimulator = listSimulators.First(it => it.Name == selectedSimulator).Id;
+                newTask.IdSimulator = idSimulator;
+                newTask.ZadanieMatrix = _matrixBD;
+                if (_idTask != 0)
                 {
-					var buffer = (matrix.Children[i] as StackPanel).Children[j].Name.Split(" ").Select(int.Parse).ToList();
-					_matrixBD[Convert.ToInt32(buffer[0]), Convert.ToInt32(buffer[1])] = Convert.ToInt32(((matrix.Children[i] as StackPanel).Children[j] as TextBox).Text);
+                    newTask.Id = _idTask;
                 }
+                newTask.Answer = Convert.ToInt32(answer);
+                ContextDb.SimulatorTasks.Add(newTask);
+                //Äîáàâèòü ôóíêöèþ ãåíåðàöèþ îòâåòà!
+                ContextDb.SaveChanges();
+                SimulatorsVM = new SimulatorsViewModel();
+                PageSwitch.View = new SimulatorsView();
             }
-			SimulatorTask newTask = new SimulatorTask();
-			var idSimulator = listSimulators.First(it => it.Name == selectedSimulator).Id;
-			newTask.IdSimulator = idSimulator;
-			newTask.ZadanieMatrix = _matrixBD;
-			if(_idTask != 0)
-			{
-                newTask.Id = _idTask;
-            } 
-            ContextDb.SimulatorTasks.Add(newTask);
-			//Äîáàâèòü ôóíêöèþ ãåíåðàöèþ îòâåòà!
-			ContextDb.SaveChanges();
-			SimulatorsVM = new SimulatorsViewModel();
-			PageSwitch.View = new SimulatorsView();
         }
 
 
@@ -129,7 +183,7 @@ namespace MathModelingSimulator.ViewModels
 			Matrix = new StackPanel();
 			if (countRows > 0 && countColumns > 0)
 			{
-				int[,] taskMatrix = new int[countRows, countColumns];
+				int[,] taskMatrix = new int[(int)countRows, (int)countColumns];
 				ShowMatrix(taskMatrix);
 			}
 		}

+ 50 - 28
MathModelingSimulator/Views/CreateSimulatorView.axaml

@@ -36,7 +36,35 @@
 
 				<Border Height="1" Grid.Column="0" Grid.Row="0" Background="#09A0B3"
 									HorizontalAlignment="Stretch" Margin="0 20 0 0"/>
+				
+				<Grid ColumnDefinitions="auto, *" RowDefinitions="*" Margin="0 40 0 0">
+
+					<TextBlock
+						Grid.Row="0"
+						Grid.Column="0"
+						Text="Тип тренажёра"
+						Margin="0 0 20 0"
+						FontSize="14"
+						VerticalAlignment="Center"
+						HorizontalAlignment="Left" />
+
+					<ComboBox
+						Grid.Row="0"
+						Grid.Column="1"
+						HorizontalAlignment="Stretch"
+						ItemsSource="{Binding CreateSimulatorVM.ListSimulatorsView}"
+						SelectedItem="{Binding CreateSimulatorVM.SelectedSimulator}"
+						Margin="0 0 0 0"
+						CornerRadius="10"
+						Foreground="#FFFFFF"
+						Background="#303238"
+						BorderBrush="#09A0B3"
+						BorderThickness="1"
+						Padding="20 8"
+						FontSize ="18"/>
 
+				</Grid>
+				
 				<Button
 					Content="Прикрепить файл (.txt, .csv)"
 					HorizontalContentAlignment="Center"
@@ -45,7 +73,7 @@
 					Foreground="#FFFFFF"
 					CornerRadius="10"
 					Padding="15 8"
-					Margin="0 40 0 0"
+					Margin="0 20 0 0"
 					Command="{Binding CreateSimulatorVM.AttachFileClick}"/>
 				
 				<TextBlock
@@ -120,34 +148,28 @@
 					Content="{Binding CreateSimulatorVM.Matrix}"
 					HorizontalAlignment="Center"
 					Margin="10"/>
+				
+				<Button
+					Content="Сгенерировать ответ"
+					HorizontalContentAlignment="Center"
+					HorizontalAlignment="Center"
+					Background="#09A0B3"
+					Margin="0 0 0 0"
+					Foreground="#FFFFFF"
+					CornerRadius="10"
+					Padding="50 8"
+					Command="{Binding CreateSimulatorVM.GenerateAnswer}"/>
 
-				<Grid ColumnDefinitions="auto, *" RowDefinitions="*" Margin="0 0 0 0">
-
-					<TextBlock
-						Grid.Row="0"
-						Grid.Column="0"
-						Text="Тип тренажёра"
-						Margin="0 0 20 0"
-						FontSize="14"
-						VerticalAlignment="Center"
-						HorizontalAlignment="Left" />
-
-					<ComboBox
-						Grid.Row="0"
-						Grid.Column="1"
-						HorizontalAlignment="Stretch"
-						ItemsSource="{Binding CreateSimulatorVM.ListSimulatorsView}"
-						SelectedItem="{Binding CreateSimulatorVM.SelectedSimulator}"
-						Margin="0 0 0 0"
-						CornerRadius="10"
-						Foreground="#FFFFFF"
-						Background="#303238"
-						BorderBrush="#09A0B3"
-						BorderThickness="1"
-						Padding="20 8"
-						FontSize ="18"/>
-					
-				</Grid>
+				<TextBlock
+					Grid.Column="1"
+					Grid.Row="0"
+					FontSize="14"
+					Foreground="#FFFFFF"
+					Margin="0 10 0 0"
+					HorizontalAlignment="Center"
+					TextWrapping="Wrap"
+					IsVisible="{Binding CreateSimulatorVM.IsVisibleRezult}"
+					Text="{Binding CreateSimulatorVM.MessageRezult}"/>
 
 				<Grid ColumnDefinitions="auto, auto" RowDefinitions="*" Margin="0 20 0 0" HorizontalAlignment="Center">