diff --git a/demopohjat/demo10/PuuRekursio/PuuRekursio.cs b/demopohjat/demo10/PuuRekursio/PuuRekursio.cs index 055f92aba4e24097b6619df8540c4d5dea5ada4a..6f3e6663341cacc04c93804e82119f856b2aa86d 100644 --- a/demopohjat/demo10/PuuRekursio/PuuRekursio.cs +++ b/demopohjat/demo10/PuuRekursio/PuuRekursio.cs @@ -52,7 +52,7 @@ public class PuuRekursio // Pääohjelma, jossa luodaan puun solmut ja aloitetaan tulostaminen. - public static void Main(String[] args) + public static void Main(string[] args) { Solmu s2 = new Solmu(2, new Solmu(1), new Solmu(3)); Solmu s6 = new Solmu(6, new Solmu(5), new Solmu(7)); diff --git a/demopohjat/demo11/PPTeht2d/Muuttujia.cs b/demopohjat/demo11/PPTeht2d/Muuttujia.cs index 8ee77486a4af9fbbf2653ad54bbf0a93ec02c2c2..f144898096f39596922309512f2238b20e1fe7e0 100644 --- a/demopohjat/demo11/PPTeht2d/Muuttujia.cs +++ b/demopohjat/demo11/PPTeht2d/Muuttujia.cs @@ -1,38 +1,36 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -namespace PPTeht2 +namespace PPTeht2d; + +/// @author: Simo Haatainen +/// @version: 14.11.2011 +/// +/// <summary> +/// Harjoitellaan muuttujien tyyppejä +/// </summary> +public class Muuttujia { - /// @author: Simo Haatainen - /// @version: 14.11.2011 - /// - /// <summary> - /// Harjoitellaan muuttujien tyyppejä - /// </summary> - public class Muuttujia - { - public static void Main(string[] args) - { - /* - muuttuvaMerkkijono = new - muuttuvaMerkkijono.Append("Tämä on jokin merkkijono, jota pystyy muokkaamaan"); - - merkkijono = "Tämä on tavallinen merkkijono"; - - merkki = merkkijono[0]; - - kokonaisLuku = 4; - - jakoJaannos = kokonaisLuku % 2; - - osaMaara = kokonaisLuku / 3; - - kokonaisLukuTaulukko = new { kokonaisLuku, 2, 4, 6 }; - - merkkijonoTaulukko = new { "kissa", "koira", "kala" }; - */ - } - } + public static void Main(string[] args) + { + /* + muuttuvaMerkkijono = new + muuttuvaMerkkijono.Append("Tämä on jokin merkkijono, jota pystyy muokkaamaan"); + + merkkijono = "Tämä on tavallinen merkkijono"; + + merkki = merkkijono[0]; + + kokonaisLuku = 4; + + jakoJaannos = kokonaisLuku % 2; + + osaMaara = kokonaisLuku / 3; + + kokonaisLukuTaulukko = new { kokonaisLuku, 2, 4, 6 }; + + merkkijonoTaulukko = new { "kissa", "koira", "kala" }; + */ + } } + diff --git a/demopohjat/demo11/Robot/Robot.cs b/demopohjat/demo11/Robot/Robot.cs index 1cf6061b2598073c5e8c71889a12e8ffdc5b15b1..4985fd2ef2990f00c13ca17f3d250d1e4a505724 100644 --- a/demopohjat/demo11/Robot/Robot.cs +++ b/demopohjat/demo11/Robot/Robot.cs @@ -13,6 +13,7 @@ public class Robot /// </summary> public static void Main() { + // ReSharper disable once UnusedVariable RobotCommander rc = new RobotCommander(); } diff --git a/demopohjat/demo11/demo11.sln b/demopohjat/demo11/demo11.sln index ff0c90f87418b672587830f43440e14034a7de57..dd0bf9727be8b1c16b42238ef45e0c9927fca8ba 100644 --- a/demopohjat/demo11/demo11.sln +++ b/demopohjat/demo11/demo11.sln @@ -11,9 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PPTeht1", "PPTeht1\PPTeht1. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2b", "PPTeht2b\PPTeht2b.csproj", "{40C9CE62-8683-47DA-B9F9-5B575A72E194}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2d", "PPTeht2c\PPTeht2d.csproj", "{77516ECA-8C05-4624-ABA0-3D4616E0D95B}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PPTeht2Test", "PPTeht2Test\PPTeht2Test.csproj", "{CE612254-98E0-482D-B1EB-C938CC2E4ED0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2d", "PPTeht2d\PPTeht2d.csproj", "{77516ECA-8C05-4624-ABA0-3D4616E0D95B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -41,10 +39,6 @@ Global {77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Debug|Any CPU.Build.0 = Debug|Any CPU {77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Release|Any CPU.ActiveCfg = Release|Any CPU {77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Release|Any CPU.Build.0 = Release|Any CPU - {CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/demopohjat/demo9/Kuva/Kuva.cs b/demopohjat/demo9/Kuva/Kuva.cs index 85c705713f3df59609b31f4d9a85d18005dd4a12..fbe4071950fb68d430d2e28d3983b6b2bbfe4660 100644 --- a/demopohjat/demo9/Kuva/Kuva.cs +++ b/demopohjat/demo9/Kuva/Kuva.cs @@ -148,7 +148,7 @@ public class Kuva : PhysicsGame /// <summary> - /// Lasketaan konvoluutio uint[,] kuva-matriisille käytten painomatriisia. + /// Lasketaan konvoluutio uint[,] kuva-matriisille käyttäen painomatriisia. /// Konvoluutio lasketaan kunkin pikselin kohdalla kullekin värille (R,B,G) /// erikseen. /// Reunat unohdetaan. @@ -158,17 +158,18 @@ public class Kuva : PhysicsGame /// <returns>uusi kuva-matriisi konvoloituna</returns> /// <example> /// <pre name="test"> - /// uint[,] kuva = { { 0x010905,0x55020202,0x7f030303,0x040404 }, - /// { 0x010101,0x66020202,0x7f030803,0x050505 }, - /// { 0x010102,0x77090909,0x7f030303,0x060606 } }; + /// uint[,] kuva = { { 0x010905,0x55020202,0x7f030303,0x040404 }, + /// { 0x010101,0x66020202,0x7f030803,0x050505 }, + /// { 0x010102,0x77090909,0x7f030303,0x060606 }, + /// { 0x010102,0x23111524,0x66778800,0x080808 } }; /// int[,] painot = {{1,0,0},{0,1,0},{0,0,2}}; /// /// uint[,] kuva2 = Muunna(kuva,painot); - /// kuva2[0,0] === 0U; // reunat jäävät mustiksi - /// kuva2[2,1] === 0U; /// Color.GetBlue(kuva2[1,1]) === 3; - /// kuva2[1,2] === 0x7f040504U; /// kuva2[1,1] === 0x66020403U; + /// kuva2[1,2] === 0x7f040504U; + /// kuva2[2,1] === 0x773E4602U; + /// kuva2[2,2] === 0x7F050505U; /// </pre> /// </example> public static uint[,] Muunna(uint[,] kuva, int[,] painot) diff --git a/demopohjat/demo9/Kuva/Ohjelma.cs b/demopohjat/demo9/Kuva/Ohjelma.cs index ae98e3bc9b917311d0b9ae7694f69075ba990c33..33e9e61555163f6489f2c7b4dd91d401808ea942 100644 --- a/demopohjat/demo9/Kuva/Ohjelma.cs +++ b/demopohjat/demo9/Kuva/Ohjelma.cs @@ -1,8 +1,4 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -#endregion namespace Demo9; @@ -15,9 +11,9 @@ public static class Program /// The main entry point for the application. /// </summary> [STAThread] - static void Main() + public static void Main() { - using (var game = new Demo9.Kuva()) - game.Run(); + using var game = new Demo9.Kuva(); + game.Run(); } } diff --git a/demopohjat/demo9/Palapeli/Ohjelma.cs b/demopohjat/demo9/Palapeli/Ohjelma.cs index 608b929658b580d28cb0ff0bd500826f8d6c729a..0ef448c9b39ea51ca7241800b1e15fe9f94ba381 100644 --- a/demopohjat/demo9/Palapeli/Ohjelma.cs +++ b/demopohjat/demo9/Palapeli/Ohjelma.cs @@ -1,10 +1,4 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -#endregion - -namespace Demo9; /// <summary> /// The main class. @@ -15,9 +9,9 @@ public static class Program /// The main entry point for the application. /// </summary> [STAThread] - static void Main() + public static void Main() { - using (var game = new Demo9.Palapeli()) - game.Run(); + using var game = new Demo9.Palapeli(); + game.Run(); } } diff --git a/demopohjat/demo9/PortaatYlos/Ohjelma.cs b/demopohjat/demo9/PortaatYlos/Ohjelma.cs new file mode 100644 index 0000000000000000000000000000000000000000..499a9c55cc1901eabae04232b9f3c7ad752de081 --- /dev/null +++ b/demopohjat/demo9/PortaatYlos/Ohjelma.cs @@ -0,0 +1,18 @@ +using System; + +namespace PortaatYlos; +/// <summary> +/// The main class. +/// </summary> +public static class Program +{ + /// <summary> + /// The main entry point for the application. + /// </summary> + [STAThread] + public static void Main() + { + using var game = new PortaatYlos(); + game.Run(); + } +} diff --git a/demopohjat/demo9/PortaatYlos/PortaatYlos.cs b/demopohjat/demo9/PortaatYlos/PortaatYlos.cs new file mode 100644 index 0000000000000000000000000000000000000000..ff9dbdf4c3fc4ae0c7ee2ba1082abbed0ba02a5e --- /dev/null +++ b/demopohjat/demo9/PortaatYlos/PortaatYlos.cs @@ -0,0 +1,42 @@ +using Jypeli; + +namespace PortaatYlos; + +public class PortaatYlos : PhysicsGame +{ + const double LAATIKON_KOKO = 80; + // BYCODEBEGIN + public override void Begin() + { + Level.Background.Color = Color.Black; + Vector piste = new Vector(0, 0); + // Poista rivien alusta kommenttimerkit + // piste = PiirraLaatikko(this, piste); + // piste = PiirraLaatikko(this, piste); + // piste = PiirraLaatikko(this, piste); + PhysicsObject pallo = new PhysicsObject(5, 5, Shape.Circle); + Add(pallo, 1); + pallo.Color = Color.Red; + Camera.ZoomToAllObjects(100); + } + + /* poista tämä rivi ja vastaava kommentin lopetusrivi + /// <summary> + /// Aliohjelma piirtää ruutuun yhden neliön, jonka + /// sivun pituus on 80 ja vasemman alakulman koordinaatti p. + /// </summary> + /// <param name="peli">Peli, johon neliö piirretään</param> + /// <param name="p">Neliön vasemman alanurkan koordinaatti.</param> + /// <returns>Neliön oikean ylänurkan koordinaatti</returns> + public static ??? PiirraLaatikko(???) + { + double s = LAATIKON_KOKO; + GameObject nelio = new GameObject(s, s, Shape.Rectangle); + nelio.Position = ??? + peli.Add(nelio); + ??? + } + */ + // BYCODEEND + +} \ No newline at end of file diff --git a/demopohjat/demo9/PortaatYlos/PortaatYlos.csproj b/demopohjat/demo9/PortaatYlos/PortaatYlos.csproj new file mode 100644 index 0000000000000000000000000000000000000000..fb98d03a9ae4d4aa96925e3188b491c8294da81d --- /dev/null +++ b/demopohjat/demo9/PortaatYlos/PortaatYlos.csproj @@ -0,0 +1,13 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>WinExe</OutputType> + <TargetFramework>net7.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Jypeli.NET" Version="11.*"/> + <PackageReference Include="Jypeli.FarseerPhysics.NET" Version="2.*"/> + </ItemGroup> + +</Project> diff --git a/demopohjat/demo9/PortaatYlosAlas/Ohjelma.cs b/demopohjat/demo9/PortaatYlosAlas/Ohjelma.cs new file mode 100644 index 0000000000000000000000000000000000000000..dbaf293328a367b93478249987b9570881edb84e --- /dev/null +++ b/demopohjat/demo9/PortaatYlosAlas/Ohjelma.cs @@ -0,0 +1,18 @@ +using System; + +namespace PortaatYlosAlas; +/// <summary> +/// The main class. +/// </summary> +public static class Program +{ + /// <summary> + /// The main entry point for the application. + /// </summary> + [STAThread] + public static void Main() + { + using var game = new PortaatYlosAlas(); + game.Run(); + } +} diff --git a/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.cs b/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.cs new file mode 100644 index 0000000000000000000000000000000000000000..c8a5d4724685d1c84749db414b7fe9a2e54e0785 --- /dev/null +++ b/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.cs @@ -0,0 +1,30 @@ +using Jypeli; +namespace PortaatYlosAlas; + +public class PortaatYlosAlas : PhysicsGame +{ + const double LAATIKON_KOKO = 80; + // BYCODEBEGIN + public override void Begin() + { + Level.Background.Color = Color.Black; + Vector piste = new Vector(0, 0); + // Poista rivien alusta kommenttimerkit + // piste = PiirraLaatikko(this, piste); + // piste = PiirraLaatikko(this, piste); + // piste = PiirraLaatikko(this, piste); + // piste -= new Vector(0,LAATIKON_KOKO); + // piste = PiirraLaatikkoAlas(this,piste); + // piste = PiirraLaatikkoAlas(this,piste); + PhysicsObject pallo = new PhysicsObject(5, 5, Shape.Circle); + Add(pallo, 1); + pallo.Color = Color.Red; + Camera.ZoomToAllObjects(100); + } + + // TODO: Kopioi tähän edellisen tehtävän PiirraLaatikko + // TODO: Jatka tekemällä PiirraLaatikkoAlas + + // BYCODEEND + +} \ No newline at end of file diff --git a/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.csproj b/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.csproj new file mode 100644 index 0000000000000000000000000000000000000000..fb98d03a9ae4d4aa96925e3188b491c8294da81d --- /dev/null +++ b/demopohjat/demo9/PortaatYlosAlas/PortaatYlosAlas.csproj @@ -0,0 +1,13 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>WinExe</OutputType> + <TargetFramework>net7.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Jypeli.NET" Version="11.*"/> + <PackageReference Include="Jypeli.FarseerPhysics.NET" Version="2.*"/> + </ItemGroup> + +</Project> diff --git a/demopohjat/demo9/demo9.sln b/demopohjat/demo9/demo9.sln index 83cbde4a0059edfc151e860f30c107cccad52ebb..43166cf86741e693fec5d624223718780f3368da 100644 --- a/demopohjat/demo9/demo9.sln +++ b/demopohjat/demo9/demo9.sln @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarienVaihtoKonsolilla", "V EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlosAlasKonsolilla", "PortaatYlosAlasKonsolilla\PortaatYlosAlasKonsolilla.csproj", "{CE21768D-76C1-40D6-B875-89051B14486C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlos", "PortaatYlos\PortaatYlos.csproj", "{BBC7FA41-52CD-417C-9725-4649E3833118}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlosAlas", "PortaatYlosAlas\PortaatYlosAlas.csproj", "{18EA707A-815F-4041-90DF-AD4C46730C56}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +43,14 @@ Global {CE21768D-76C1-40D6-B875-89051B14486C}.Debug|Any CPU.Build.0 = Debug|Any CPU {CE21768D-76C1-40D6-B875-89051B14486C}.Release|Any CPU.ActiveCfg = Release|Any CPU {CE21768D-76C1-40D6-B875-89051B14486C}.Release|Any CPU.Build.0 = Release|Any CPU + {BBC7FA41-52CD-417C-9725-4649E3833118}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBC7FA41-52CD-417C-9725-4649E3833118}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBC7FA41-52CD-417C-9725-4649E3833118}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBC7FA41-52CD-417C-9725-4649E3833118}.Release|Any CPU.Build.0 = Release|Any CPU + {18EA707A-815F-4041-90DF-AD4C46730C56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18EA707A-815F-4041-90DF-AD4C46730C56}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18EA707A-815F-4041-90DF-AD4C46730C56}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18EA707A-815F-4041-90DF-AD4C46730C56}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/.gitignore b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b65a01dcbc10f0ad67e60596002626151d77329b --- /dev/null +++ b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.Konsolimalleja.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/encodings.xml b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/encodings.xml new file mode 100644 index 0000000000000000000000000000000000000000..df87cf951fb4858ab7a76b68dd479c98b2df2404 --- /dev/null +++ b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/encodings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" /> +</project> \ No newline at end of file diff --git a/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/indexLayout.xml b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/indexLayout.xml new file mode 100644 index 0000000000000000000000000000000000000000..7b08163cebc50fb3e777eea4881b68fcebc10590 --- /dev/null +++ b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/indexLayout.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="UserContentModel"> + <attachedFolders /> + <explicitIncludes /> + <explicitExcludes /> + </component> +</project> \ No newline at end of file diff --git a/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/vcs.xml b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..b2bdec2d71b6a5ce4ae49efc37516809c50e4d5e --- /dev/null +++ b/mallit/Konsolimalleja/.idea/.idea.Konsolimalleja/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$/../.." vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.cs b/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.cs new file mode 100644 index 0000000000000000000000000000000000000000..eb2b3c61d297605d92c2a94c5ee06a5ce7a3e405 --- /dev/null +++ b/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; + +/// @author vesal +/// @version 04.11.2023 +/// <summary> +/// Knapsack problem. See +/// - https://en.wikipedia.org/wiki/Knapsack_problem +/// - https://www.youtube.com/watch?v=hagBB17_hvg +/// - https://www.youtube.com/watch?v=cJ21moQpofY +/// TODO: Implement dynamic version +/// </summary> +public class KnapsackProblem +{ + public record struct Item(string name, int weight, int value); + + + /// <summary> + /// Try functions + /// </summary> + public static void Main() + { + Item[] items = { + new Item("micro", 8, 50), + new Item("drone", 2, 150), + new Item("monitor", 6, 210), + new Item("kettle", 1, 30) + }; + + + (long bestValue, List<Item> used) = KnapsackBruteForce(items, 10); + Console.Write($"Best value {bestValue}: "); + foreach (Item item in used) Console.Write($"{item.name} {item.weight} kg, "); + + (bestValue, used) = KnapsackDynamic(items, 10); + Console.Write($"Best value {bestValue}: "); + foreach (Item item in used) Console.Write($"{item.name} {item.weight} kg, "); + } + + + // ReSharper disable InvalidXmlDocComment + /// <summary> + /// Solve Knapsack 0/1 problem with brute force. + /// O(n 2^n)), max 64 items. + /// </summary> + /// <param name="items">Items to fit</param> + /// <param name="maxCapacity">capacity of knapsack</param> + /// <returns>(bestValue, list of used items)</returns> + /// <example> + /// <pre name="test"> + /// #EQUALSMARKER2="==>" + /// #import System.Linq + /// Item[] items = { + /// new Item("micro", 8, 50), + /// new Item("drone", 2, 150), + /// new Item("monitor", 6, 210), + /// new Item("kettle", 1, 30) + /// }; + /// + /// (long bestValue, List<Item> used) = KnapsackDynamic(items, 10); + /// bestValue === 390; + /// used.Select(item => item.name) =J= "drone, monitor, kettle"; + /// (bestValue, used) = KnapsackDynamic(items, 8); + /// bestValue === 360; + /// used.Select(item => item.name) =J= "drone, monitor"; + /// (bestValue, used) = KnapsackDynamic(items, 3); + /// bestValue === 180; + /// used.Select(item => item.name) =J= "drone, kettle"; + /// </pre> + /// </example> + public static (long, List<Item>) KnapsackDynamic(Item[] items, int maxCapacity) + { + // TODO: toteuta + List<Item> used = new List<Item>(); + return (0, used); + } + + + // ReSharper disable InvalidXmlDocComment + /// <summary> + /// Solve Knapsack 0/1 problem with brute force. + /// O(n 2^n)), max 64 items. + /// </summary> + /// <param name="items">Items to fit</param> + /// <param name="maxCapacity">capacity of knapsack</param> + /// <returns>(bestValue, list of used items)</returns> + /// <example> + /// <pre name="test"> + /// #EQUALSMARKER2="==>" + /// #import System.Linq + /// Item[] items = { + /// new Item("micro", 8, 50), + /// new Item("drone", 2, 150), + /// new Item("monitor", 6, 210), + /// new Item("kettle", 1, 30) + /// }; + /// + /// (long bestValue, List<Item> used) = KnapsackBruteForce(items, 10); + /// bestValue === 390; + /// used.Select(item => item.name) =J= "drone, monitor, kettle"; + /// (bestValue, used) = KnapsackBruteForce(items, 8); + /// bestValue === 360; + /// used.Select(item => item.name) =J= "drone, monitor"; + /// (bestValue, used) = KnapsackBruteForce(items, 3); + /// bestValue === 180; + /// used.Select(item => item.name) =J= "drone, kettle"; + /// </pre> + /// </example> + public static (long, List<Item>) KnapsackBruteForce(Item[] items, int maxCapacity) + { + int n = items.Length; + uint nMax = (uint)Math.Pow(2, n); + long bestValue = 0; + ulong bestItems = 0; + + for (ulong itemsMask = 1; itemsMask < nMax; itemsMask++) + { + ulong mask = 1; + long sumWeights = 0, sumValues = 0; + for (int i = 0; i < n && mask <= itemsMask; i++, mask <<=1) + { + if ((itemsMask & mask) == 0L) continue; + sumWeights += items[i].weight; + if (sumWeights > maxCapacity) break; + sumValues += items[i].value; + } + + if (sumWeights > maxCapacity || bestValue >= sumValues) continue; + bestValue = sumValues; + bestItems = itemsMask; + } + + List<Item> used = new List<Item>(); + ulong m = 1; + for (int i = 0; i < n; i++, m <<= 1) if ((bestItems & m) != 0) used.Add(items[i]); + + return (bestValue, used); + } + +} + + diff --git a/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.csproj b/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.csproj new file mode 100644 index 0000000000000000000000000000000000000000..df22a1ba07d539b07cf0adbaa6457daadeb433db --- /dev/null +++ b/mallit/Konsolimalleja/KnapsackProblem/KnapsackProblem.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net7.0</TargetFramework> + <ExternalConsole>true</ExternalConsole> + </PropertyGroup> + +</Project> diff --git a/mallit/Konsolimalleja/KnapsackProblem/comtest.ini b/mallit/Konsolimalleja/KnapsackProblem/comtest.ini new file mode 100644 index 0000000000000000000000000000000000000000..19ec9ee75af614f94302eea940277535a421e598 --- /dev/null +++ b/mallit/Konsolimalleja/KnapsackProblem/comtest.ini @@ -0,0 +1 @@ +#EQUALSMARKER2="==>" \ No newline at end of file diff --git a/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.cs b/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..652af52673840341554115f6e49697787029c30f --- /dev/null +++ b/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.cs @@ -0,0 +1,32 @@ +// ReSharper disable all +using System; +using System.Collections.Generic; +using NUnit.Framework; +using static KnapsackProblem; +using System.Linq; + + [TestFixture] + [DefaultFloatingPointTolerance(0.000001)] + public class TestKnapsackProblem + { + [Test] + public void Teststatic47() + { + Item[] items = { + new Item("micro", 8, 50), + new Item("drone", 2, 150), + new Item("monitor", 6, 210), + new Item("kettle", 1, 30) + }; + (long bestValue, List<Item> used) = KnapsackBruteForce(items, 10); + Assert.AreEqual( 390, bestValue , "in method static, line 58"); + Assert.AreEqual(( "drone, monitor, kettle").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 59"); + (bestValue, used) = KnapsackBruteForce(items, 8); + Assert.AreEqual( 360, bestValue , "in method static, line 61"); + Assert.AreEqual(( "drone, monitor").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 62"); + (bestValue, used) = KnapsackBruteForce(items, 3); + Assert.AreEqual( 180, bestValue , "in method static, line 64"); + Assert.AreEqual(( "drone, kettle").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 65"); + } + } + diff --git a/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.csproj b/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.csproj new file mode 100644 index 0000000000000000000000000000000000000000..213a06aec2843b4c21b4a515e394283f97c6cc73 --- /dev/null +++ b/mallit/Konsolimalleja/KnapsackProblemTest/KnapsackProblemTest.csproj @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <IsPackable>false</IsPackable> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="NUnit" Version="3.13.1"/> + <PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3"/> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\KnapsackProblem\KnapsackProblem.csproj"/> + </ItemGroup> +</Project> diff --git a/mallit/Konsolimalleja/Konsolimalleja.sln b/mallit/Konsolimalleja/Konsolimalleja.sln new file mode 100644 index 0000000000000000000000000000000000000000..58d68e6d8854ae8ba2b85a5589454ccce7d7d76b --- /dev/null +++ b/mallit/Konsolimalleja/Konsolimalleja.sln @@ -0,0 +1,23 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 + +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KnapsackProblem", "KnapsackProblem\KnapsackProblem.csproj", "{41029A20-BE21-4E40-AF2F-E321BEB16B68}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KnapsackProblemTest", "KnapsackProblemTest\KnapsackProblemTest.csproj", "{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.Build.0 = Release|Any CPU + {522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/mallit/Konsolimalleja/Konsolimalleja.sln.ctbackup b/mallit/Konsolimalleja/Konsolimalleja.sln.ctbackup new file mode 100644 index 0000000000000000000000000000000000000000..c399d9d80fe62d977b41dcb27b5890773de1d66f --- /dev/null +++ b/mallit/Konsolimalleja/Konsolimalleja.sln.ctbackup @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KnapsackProblem", "KnapsackProblem\KnapsackProblem.csproj", "{41029A20-BE21-4E40-AF2F-E321BEB16B68}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal