Lets gooooo

This commit is contained in:
Zastian Pretorius
2022-08-02 12:42:49 +01:00
parent 782f31810c
commit eb9f7c6c67
88 changed files with 3246 additions and 2066 deletions

View File

@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{02E85643-5DA8-4A1E-AF07-10184FC132A0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DataAccess</RootNamespace>
<AssemblyName>DataAccess</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ModelReader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Common.csproj">
<Project>{04C11623-F02E-45C6-8C51-82244CFF4780}</Project>
<Name>Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="acme.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="branch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="cut.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="farmer.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="korean.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="limpopo.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="santa.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

164
DataAccess/ModelReader.cs Normal file
View File

@@ -0,0 +1,164 @@
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess
{
public class ModelReader
{
public static Model ReadModelFromFile(string path)
{
List<string> lines = new List<string>();
try
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(fileStream))
{
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine());
}
}
}
return ConvertToModel(lines);
}
catch (FileNotFoundException)
{
throw new CustomException("Could not find the specified file");
}
catch (DirectoryNotFoundException)
{
throw new CustomException("Could not find the specified directory");
}
catch (ArgumentException)
{
throw new CustomException("The specified path was invalid");
}
catch (IOException)
{
throw new CustomException("There was an error reading the specified file");
}
}
private static Model ConvertToModel(List<string> lines)
{
Model model = new Model();
try
{
if (lines.Count < 1)
{
throw new CustomException("The specified file is empty");
}
string[] objectiveFunction = lines[0].Split(' ');
if (!(objectiveFunction[0].ToLower().Equals("max") || objectiveFunction[0].ToLower().Equals("min")))
{
throw new CustomException("The given problem does not specify whether to maximize or minimize the objective function");
}
model.ProblemType = objectiveFunction[0].ToLower().Equals("max") ? ProblemType.Maximization : ProblemType.Minimization;
for (int i = 1; i < objectiveFunction.Length; i++)
{
model.ObjectiveFunction.DecisionVariables.Add(new DecisionVariable() { Coefficient = double.Parse(objectiveFunction[i]) });
}
if (lines.Count < 2)
{
throw new CustomException("The given problem does not contain any constraints");
}
for (int i = 1; i < lines.Count - 1; i++)
{
string[] constraintArr = lines[i].Split(' ');
Constraint constraint = new Constraint();
for (int j = 0; j < constraintArr.Length - 2; j++)
{
constraint.DecisionVariables.Add(new DecisionVariable() { Coefficient = double.Parse(constraintArr[j]) });
}
switch (constraintArr[constraintArr.Length - 2])
{
case "=":
{
constraint.InequalitySign = InequalitySign.EqualTo;
}
break;
case "<=":
{
constraint.InequalitySign = InequalitySign.LessThanOrEqualTo;
}
break;
case ">=":
{
constraint.InequalitySign = InequalitySign.GreaterThanOrEqualTo;
}
break;
default:
{
throw new CustomException($"Constraint {model.Constraints.Count + 1} does not have a valid inequality symbol");
}
}
constraint.RightHandSide = double.Parse(constraintArr[constraintArr.Length - 1]);
model.Constraints.Add(constraint);
}
string[] signRestrictions = lines[lines.Count - 1].Split(' ');
foreach (var restriction in signRestrictions)
{
switch (restriction.ToLower())
{
case "+":
{
model.SignRestrictions.Add(SignRestriction.Positive);
}
break;
case "-":
{
model.SignRestrictions.Add(SignRestriction.Negative);
}
break;
case "urs":
{
model.SignRestrictions.Add(SignRestriction.Unrestricted);
}
break;
case "int":
{
model.SignRestrictions.Add(SignRestriction.Integer);
}
break;
case "bin":
{
model.SignRestrictions.Add(SignRestriction.Binary);
}
break;
default:
{
throw new CustomException("Invalid sign restriction found");
}
}
}
}
catch (FormatException)
{
throw new CustomException("One of the decision variables has an invalid coefficient");
}
return model;
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DataAccess")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DataAccess")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("02e85643-5da8-4a1e-af07-10184fc132a0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

5
DataAccess/acme.txt Normal file
View File

@@ -0,0 +1,5 @@
min +4 +1
+3 +1 >= 10
+1 +1 >= 5
+1 +0 >= 3
+ +

4
DataAccess/branch.txt Normal file
View File

@@ -0,0 +1,4 @@
max +8 +5
+1 +1 <= +6
+9 +5 <= +45
int int

4
DataAccess/cut.txt Normal file
View File

@@ -0,0 +1,4 @@
min +6 +8
+3 +1 >= 4
+1 +2 >= 4
int int

5
DataAccess/farmer.txt Normal file
View File

@@ -0,0 +1,5 @@
max +100 +30
+1 +1 <= +7
+10 +4 <= 40
+0 +1 >= 3
+ +

4
DataAccess/korean.txt Normal file
View File

@@ -0,0 +1,4 @@
min +50 +100
+7 +2 >= +28
+2 +12 >= +24
+ +

6
DataAccess/limpopo.txt Normal file
View File

@@ -0,0 +1,6 @@
max +60 +30 +20
+8 +6 +1 <= +48
+4 +2 +1,5 <= +20
+2 +1,5 +0,5 <= +8
+0 +1 +0 <= +5
+ + +

5
DataAccess/santa.txt Normal file
View File

@@ -0,0 +1,5 @@
max +3 +2
+2 +1 <= 100
+1 +1 <= 80
+1 +0 <= 40
+ +