Skip to content

Commit

Permalink
baslangic
Browse files Browse the repository at this point in the history
  • Loading branch information
Naim Buru authored and Naim Buru committed Apr 4, 2017
0 parents commit 35adf6c
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 0 deletions.
Binary file added .vs/8queens_simulated/v14/.suo
Binary file not shown.
22 changes: 22 additions & 0 deletions 8queens_simulated.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8queens_simulated", "8queens_simulated\8queens_simulated.csproj", "{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
60 changes: 60 additions & 0 deletions 8queens_simulated/8queens_simulated.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{3AD7C5A2-5CA1-477A-9E17-835E8C6A35CD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>_8queens_simulated</RootNamespace>
<AssemblyName>8queens_simulated</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
6 changes: 6 additions & 0 deletions 8queens_simulated/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
226 changes: 226 additions & 0 deletions 8queens_simulated/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _8queens_simulated
{
class Program
{
static void Main(string[] args)
{
List<string> tablo = new List<string>();
int totalTime = 0;
int totalMoves = 0;
int totalRestarts = 0;

for (int i = 0; i < 35; i++)
{
bool[,] board = initializeBoard(8); //initialize for 8x8 (8 queens)

int Moves = 0;
int restartCount = 0;
Stopwatch stopwatch = new Stopwatch();

stopwatch.Start(); // Begin timing.

while (calculateCollisions(board) > 0)
{
applyHillClimbingAlgorithmToTheBoard(board);
Moves++;
if (Moves > 20) //if it stucks at the local minimum
{
board = initializeBoard(8); //random new board
Moves = 0;
restartCount++;
}
}
stopwatch.Stop();
drawTheBoard(board);
Console.WriteLine(i + 1 + ") Total Moves: " + Moves + ", Restart Count: " + restartCount + ", Time elapsed: {0} ms", stopwatch.Elapsed.Milliseconds);
tablo.Add(i + 1 + ") Total Moves: " + Moves + ", Restart Count: " + restartCount + ", Time elapsed:" + stopwatch.Elapsed.Milliseconds + " ms");

totalMoves += Moves;
totalTime += stopwatch.Elapsed.Milliseconds;
totalRestarts += restartCount;

Thread.Sleep(300); //to see the board
Console.Clear();
}

foreach (String istatistik in tablo)
{
Console.WriteLine(istatistik);
}
Console.WriteLine("++Average Moves: " + totalMoves / 35 + ", Average Restarts: " + totalRestarts / 35 + ", Average Time: " + totalTime / 35 + " ms ");

Console.ReadKey();
}

// returns 2d array with specified size (if size=4, 2d array is 4 x 4 and there are 4 queens)
static bool[,] initializeBoard(int size)
{
bool[,] board = new bool[size, size];
Random rnd = new Random();


for (int i = 0; i < size; i++)
{
int randomSquare = rnd.Next(8); //placing queen to a random square in the row
for (int j = 0; j < size; j++)
{
if (j != randomSquare)
board[i, j] = false;
else
board[i, j] = true;
}
}

return board;
}

static int calculateCollisions(bool[,] board)
{
int size = Convert.ToInt32(Math.Sqrt(board.Length)); //if the board is 8x8, board.Length will be 64
int totalCollisions = 0;

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (board[i, j] == true) // if there is a queen on that square
{
//for (int z = j + 1; z < size; z++) //check the row //not necessary with this put-a-queen-to-each-row initialization
//{
// if (board[i, z] == true)
// {
// totalCollisions++;
// break;
// }
//}

for (int z = i + 1; z < size; z++) //check the column
{
if (board[z, j] == true)
{
totalCollisions++;
//break; //if indirect attacking does not count
}
}

for (int z = 1; i + z < size && j + z < size; z++) //check the bottom-right diagonal
{
if (board[i + z, j + z] == true)
{
totalCollisions++;
//break;
}
}

for (int z = 1; i - z >= 0 && j + z < size; z++) //check the top-right diagonal
{
if (board[i - z, j + z] == true)
{
totalCollisions++;
//break;
}
}

}
}
}
return totalCollisions;

}

static void applyHillClimbingAlgorithmToTheBoard(bool[,] board)
{
int size = Convert.ToInt32(Math.Sqrt(board.Length));
int[,] successors = new int[size, size];

for (int i = 0; i < size; i++)
{
//find the queen of the row first
int indexOfQueen = -1;
for (int j = 0; j < size; j++)
{
if (board[i, j] == true)
{
indexOfQueen = j;
board[i, j] = false;
}
}
//try all the moves on the row and save collisions
for (int j = 0; j < size; j++)
{
if (j != indexOfQueen)
{
board[i, j] = true;
successors[i, j] = calculateCollisions(board);
board[i, j] = false;
}
else
{
successors[i, j] = 999; //to ignore old position
}
}
board[i, indexOfQueen] = true; //fixing the row to its first position
}

//select lowest value of successors
int min = 998;
int indexI = -1;
int indexJ = -1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (successors[i, j] < min)
{
min = successors[i, j];
indexI = i;
indexJ = j;
}
}
}

//make the move, remove queen of the row first and put the new one
for (int j = 0; j < size; j++)
{
if (board[indexI, j] == true)
{
board[indexI, j] = false;
}
}
board[indexI, indexJ] = true;


drawTheBoard(board);
Console.WriteLine("Total Collisions: " + calculateCollisions(board));
//Thread.Sleep(100); //to follow steps
Console.Clear();
}

static void drawTheBoard(bool[,] board)
{
int size = Convert.ToInt32(Math.Sqrt(board.Length)); //if the board is 8x8, board.Length will be 64

for (int k = 0; k < size; k++)
{
for (int j = 0; j < size; j++)
{
if (board[k, j] == true)
Console.Write("O ");
else
Console.Write("- ");
}
Console.WriteLine();
}
}


}
}
36 changes: 36 additions & 0 deletions 8queens_simulated/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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("8queens_simulated")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("8queens_simulated")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[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("3ad7c5a2-5ca1-477a-9e17-835e8c6a35cd")]

// 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")]
Binary file not shown.
6 changes: 6 additions & 0 deletions 8queens_simulated/bin/Debug/8queens_simulated.exe.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Binary file added 8queens_simulated/bin/Debug/8queens_simulated.pdb
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
11 changes: 11 additions & 0 deletions 8queens_simulated/bin/Debug/8queens_simulated.vshost.exe.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\bin\Debug\8queens_simulated.exe.config
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\bin\Debug\8queens_simulated.exe
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\bin\Debug\8queens_simulated.pdb
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\obj\Debug\8queens_simulated.csprojResolveAssemblyReference.cache
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\obj\Debug\8queens_simulated.exe
c:\users\naim\documents\visual studio 2015\Projects\8queens_simulated\8queens_simulated\obj\Debug\8queens_simulated.pdb
Binary file not shown.
Binary file not shown.
Binary file added 8queens_simulated/obj/Debug/8queens_simulated.pdb
Binary file not shown.
Binary file not shown.
Empty file.
Empty file.
Empty file.

0 comments on commit 35adf6c

Please sign in to comment.