Artificial Intelligence - هوش مصنوعی

Artificial Intelligence - هوش مصنوعی (http://artificial.ir/intelligence/)
-   حل مسائل معروف هوش مصنوعي (http://artificial.ir/intelligence/forum102.html)
-   -   حل مسئله 8 وزير(8Queen) با روش هاي مختلف! (http://artificial.ir/intelligence/thread619.html)

مريم نازنين ۰۸-۲۰-۱۳۸۹ ۱۱:۱۴ قبل از ظهر

نقل قول:

نوشته اصلي بوسيله aminkop (پست 12486)
یک صفحه شطرنج بردارید و مهره ها را مرحله به مرحله بچینید

ممنون از جوابتون اما من از كدش چيزي سر در نميارم:

void queens ( index i)
{
index j;
if ( promising(i))
if ( i == n)
cout << col [1] through col [n];
else
for ( j = 1 ; j ≤ n ; j++ ) {

col [ i +1 ] = j;
queens ( i + 1);
}
}
bool promising ( index i )
{
index k ;
bool switch;
k = 1;
switch = true ;
while ( k < i && switch ) {
if (col [i] == col[k] || abs(col[i] – col[k] == i-k)
switch = false;
k++;
}
return switch;
}


يعني ببينين اينجا منظور از promising وtrue .. چيه؟ فقط شرط آخر رو متوجه شدم كه قطري بودن و.. رو چك مي كنه.:2:

asmagan ۰۹-۱۹-۱۳۸۹ ۰۱:۴۶ قبل از ظهر

واي كاش يكي پيدا ميشد و 8 وزير را با زبان جاوا يا سي شارپ با الگوريتم هاي blind و hurrstic search رو به من مي داد:42:

Astaraki ۰۹-۱۹-۱۳۸۹ ۰۴:۲۱ بعد از ظهر

سورس 8 وزیر

كد:


#include <iostream.h>
#include<conio.h>

int test( int i , int j );
int board[8][8] = { 0 };
int main()
{
 for ( int a = 0 ; a < 8 ; a++ )
  if(test( 0 , a ))
  for ( int b = 0 ; b < 8 ; b++ )
    if(test( 1 , b ))
    for ( int c = 0 ; c < 8 ; c++ )
      if(test( 2 , c ))
      for ( int d = 0 ; d < 8 ; d++ )
        if(test( 3 , d ))
        for ( int e = 0 ; e < 8 ; e++ )
          if(test( 4 , e ))
          for ( int f = 0 ; f < 8 ; f++ )
            if(test( 5 , f ))
            for ( int g = 0 ; g < 8 ; g++ )
              if(test( 6 , g ))
              for ( int h = 0 ; h < 8 ; h++ )
                if(test( 7 , h )) {
                for ( int m = 0 ; m < 8 ; m++ ) {
                  for ( int n = 0 ; n < 8 ; n++ )
                  cout << board[m][n] << " ";
                  cout << endl;
                }
                cout << endl;
                getch();
                }
 cout << endl;
 return 0;
}
int test( int i , int j )
{
 for ( int a = 0 ; a < 8 ; a++ )
  board[i][a] = 0;
 for ( int b = 1 ; b < 8 ; b++ )
  if ( i - b >= 0 ) {
  if ( board[i - b][j] != 0 )
    return 0;
  if ( j - b >= 0 )
    if ( board[i - b][j - b] != 0 )
    return 0;
  if ( j + b <= 7 )
    if ( board[i - b][j + b] != 0 )
    return 0;
  }
 board[i][j] = 1;
 return 1;
}


Astaraki ۰۹-۱۹-۱۳۸۹ ۰۴:۲۴ بعد از ظهر

برنامه ی زیر که به صورت بازگشتی و با روش BackTrack نوشته شده رو میشه به N وزیر به راحتی تعمیم داد:

كد:


#include <iostream>
#include <stdlib.h>
#include<conio.h>
using namespace std;
#define N 8
bool mark[N];
int a[N];
void bt (int n)
{
if (n == N)
{
 for (int i = 0; i < N; i++, cout << endl)
  for (int j = 0; j < N; j++)
  if (a[i] == j)
    cout << "1 ";
  else
    cout << "0 ";
 cout << endl;
 getch ();
}
else
{
 for (int i = 0; i < N; i++)
  if (!mark[i])
  {
  bool b = true;
  for (int j = 0; j < n; j++)
    if (n - j == abs(i - a[j]))
    b = false;
  if (b)
  {
    mark[i] = true;
    a[n] = i;
    bt(n + 1);
    mark[i] = false;
  }
  }
}
}
int main ()
{
bt(0);
}


Astaraki ۰۹-۱۹-۱۳۸۹ ۰۴:۲۷ بعد از ظهر

2(ها)ضميمه
غیر بازگشتی:

كد:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace reversi
{
classeightQueen {
privateBoolean [,] board;
privateBoolean[, ,] memory;
privatestaticInt32 BoardNO = 0;
 
publicBoolean[, ,] calculate()
{
board = newBoolean [8,8];
memory = newBoolean[8, 8, 92];
 
for ( int a = 0 ; a < 8 ; a++ )
if(test( 0 , a ))
for ( int b = 0 ; b < 8 ; b++ )
if(test( 1 , b ))
for ( int c = 0 ; c < 8 ; c++ )
if(test( 2 , c ))
for ( int d = 0 ; d < 8 ; d++ )
if(test( 3 , d ))
for ( int e = 0 ; e < 8 ; e++ )
if(test( 4 , e ))
for ( int f = 0 ; f < 8 ; f++ )
if(test( 5 , f ))
for ( int g = 0 ; g < 8 ; g++ )
if(test( 6 , g ))
for ( int h = 0 ; h < 8 ; h++ )
if (test(7, h))
{
 
for ( int m = 0 ; m < 8 ; m++ ) {
for (int n = 0; n < 8; n++)
memory[m, n, BoardNO] = board[m,n];
}
BoardNO++;
}
return memory;
}
Boolean test( int i , int j )
{
for ( int a = 0 ; a < 8 ; a++ )
board[i, a] = false;
for ( int b = 1 ; b < 8 ; b++ )
if ( i - b >= 0 ) {
if ( board[i - b,j] != false )
returnfalse;
if ( j - b >= 0 )
if (board[i - b, j - b] != false)
returnfalse;
if ( j + b <= 7 )
if (board[i - b, j + b] != false)
returnfalse ;
}
board[i,j]= true ;
returntrue ;
}
}
}


بازگشتی: ( که در اين لينک هم قرار داده شده)

كد:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//calculate
namespace reversi
{
classeightQueen{
privateint [] board;
privateBoolean[,,] memory;
privateint number=0;
 
/**
* Each element of the board[] represents a row and the value of each element
* specifies the column number where the queen is placed on that particular row.
* This method is used to retrieve the board array.
*
* @return
*/
publicBoolean [,,] getBoard() {
return memory;
}
public eightQueen()
{
 
board = newint[8];
memory = newBoolean[8, 8, 93];
this.place(0);
}
/**
* Place a queen in a row and check its validity. If valid then place it in the
* next row else backtrack.
*
* @param row
*/
publicvoid place(int row) {
if (row == 8) {
 
for (int r = 0; r< 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (board[r] == c){
memory[r, c, number] = true;
// MessageBox.Show(r.ToString()+"/"+c.ToString()+"/"+number.ToString());
}else
memory[r, c, number] = false;
}
}
this.number++;
return;
} else {
for (int i = 0; i < 8; i++) {
board[row] = i;
if (valid(row)) place(row + 1);
}
}
}
/**
* Checks the validity of a position. If valid returns true else returns false.
*
* @param row
* @return
*/
publicBoolean valid(int row) {
for (int i = 0; i < row; i++) {
if ((board[i] == board[row]) || Math.Abs(board[row] - board[i]) == (row - i)) {
returnfalse;
}
}
returntrue;
}
}}



کد طراحی هر دوی آن به یک صورت است:

كد:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace reversi
{
publicpartialclassReversi : Form
{
private System.Windows.Forms.Panel squaresPanel;
private System.Windows.Forms.Panel boardPanel;
private System.Windows.Forms.Label cornerLabel;
privateLabel[] colLabels, rowLabels;
privateSquareControl[,] squareControls;
// For converting column numbers to letters and vice versa.
privatestaticString alpha = "ABCDEFGH";
// The game board.
privateBoard board;
 
privateeightQueen queen;
privateBoolean[, ,] memory;
privateInt32 moveNumber;
// Used to track which player made the last move.
// Defines a thread for running the computer move look ahead.
 
public Reversi()
{
InitializeComponent();
// Create the game board.
this.board = newBoard();
 
#region bord designer
// Create the controls for each square, add them to the squares
// panel and set up event handling.
this.squareControls = newSquareControl[8, 8];
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
// Create it.
this.squareControls[i, j] = newSquareControl(i, j);
// Position it.
this.squareControls[i, j].Left = j * this.squareControls[i, j].Width;
this.squareControls[i, j].Top = i * this.squareControls[i, j].Height;
// Add it.
this.squaresPanel.Controls.Add(this.squareControls[i, j]);
// Set up event handling for it.
this.squareControls[i, j].Click += newEventHandler(this.SquareControl_Click);
 
}
// Create the column and row labels.
this.colLabels = newLabel[8];
for (i = 0; i < 8; i++)
{
// Create a column label.
this.colLabels[i] = newLabel();
// Set its display properties.
this.colLabels[i].Text = Reversi.alpha.Substring(i, 1);
this.colLabels[i].BackColor = this.cornerLabel.BackColor;
this.colLabels[i].ForeColor = this.cornerLabel.ForeColor;
this.colLabels[i].TextAlign = ContentAlignment.MiddleCenter;
// Set its size and position.
this.colLabels[i].Width = this.squareControls[0, 0].Width;
this.colLabels[i].Height = this.cornerLabel.Height;
this.colLabels[i].Left = this.cornerLabel.Width + i * this.colLabels[0].Width;
this.colLabels[i].Top = 0;
// Add it.
this.boardPanel.Controls.Add(this.colLabels[i]);
}
this.rowLabels = newLabel[8];
for (i = 0; i < 8; i++)
{
// Create a row label.
this.rowLabels[i] = newLabel();
// Set its display properties.
this.rowLabels[i].Text = (i + 1).ToString();
this.rowLabels[i].BackColor = this.cornerLabel.BackColor;
this.rowLabels[i].ForeColor = this.cornerLabel.ForeColor;
this.rowLabels[i].TextAlign = ContentAlignment.MiddleCenter;
// Set its size and position.
this.rowLabels[i].Width = this.cornerLabel.Height;
this.rowLabels[i].Height = this.squareControls[0, 0].Height;
this.rowLabels[i].Left = 0;
this.rowLabels[i].Top = this.cornerLabel.Height + i * this.rowLabels[0].Width * 2;
 
// Add it.
this.boardPanel.Controls.Add(this.rowLabels[i]);
}
#endregion
// Initialize the game state.
 
queen = neweightQueen();
memory = queen.getBoard();
}
privatevoid UpdateBoardDisplay()
{
if (moveNumber < 93&&moveNumber>-1)
{
this.moveNumber++;
// Map the current game board to the square controls.
SquareControl squareControl;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
squareControl = (SquareControl)this.squaresPanel.Controls[row * 8 + col];
if (memory[row, col, this.moveNumber] == true)
{
 
squareControl.Contents = 1;
}
else squareControl.Contents = 0;
}
}
// Redraw the board.
this.squaresPanel.Refresh();
}
}
privatevoid SquareControl_Click(object sender, System.EventArgs e)
{
 
this.UpdateBoardDisplay();
}
}
}


استفاده از این برنامه به عنوان پروژه درسی مجاز نیست.دوستان تنها مجاز به استفاده از قسمت گرافیک این برنامه هستند.
برای پیاده سازی دیگر الگوریتم ها تنها کافیست که یک آرایه 3 بعدی را با الگوریتم تان برگردانید.
اندیس اول و دوم این آرایه برای ردیف و ستون صفحه شطرنج در نظر گرفته شده است،و اندیس سوم هم برای ذخیره 92 حالت ممکن در نظر گرفته شده است.

دیگر الگوریتم های ممکن را می توانید در همین فارم پیدا کنید.

Astaraki ۰۹-۱۹-۱۳۸۹ ۰۴:۳۸ بعد از ظهر

جاوا
 
1(ها)ضميمه
Solving the N-Queens Problem

asmagan ۰۹-۲۰-۱۳۸۹ ۱۱:۱۲ قبل از ظهر

ممنون از لطفت ريحانه جان واقعا متشكرم از لطفتون


دوستاي گلم مرتبه زماني و مكاني دو الگوريتم blind و hurrstic search رو اگر داريد لطف كنيد ممنون ميشم

mpour ۰۹-۲۸-۱۳۸۹ ۰۲:۴۰ بعد از ظهر

حل مساله nوزیر با الگوریتم ژنتیک
 
لطفا حل کامل مساله n وزیر ( 8 وزیر) را همراه با الگوریتم آن با الگوریتم ژنتیک قرار دهید .

mehdinajafinia ۱۰-۶-۱۳۸۹ ۱۰:۱۶ قبل از ظهر

ان وزیر
 
سلام
منم این ترم هوش دارم خواستی مقاله لاتین (ان وزیر) رو با کد پیاده سازی و power point میدم
خبر بده؟:112:

farzane1 ۱۰-۲۸-۱۳۸۹ ۱۱:۲۸ قبل از ظهر

سلام پروژه 8وزیر به روش ژنتیک
با نرم افزار مطلب


زمان محلي شما با تنظيم GMT +3.5 هم اکنون ۱۰:۳۹ قبل از ظهر ميباشد.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.