amir-shakh
جمعه 09 بهمن 1388, 17:35 عصر
سلام دوستان من میخوام ببینم کسی از دوستان میتونه این برنامه رو اجرا کنه و فایل EXE یا اجراییشو برام آپلود کنه تا من دانلود کنم ؟
package simulatedannealing
import java.util.*
import java.lang. Math .*
public class Main
{
static int maxLength = 8 //Default Size of the Chess Board
static double initialTemperature = 30
static double finalTemperature = 0.5
static double alpha = 0.99
static Random random = new Random ( System .currentTimeMillis())
static int iSolution[] = new int [maxLength] static int iSolutionH
static int itSolution[] = new int [maxLength] static int itSolutionH
static int iBastSolutionH
static String Upshot = ""
static int next = 0
static int previous = 0
static int bast = 0
static int calc = 0
public static void main ( String [] args )
{
if (args.length>0)
maxLength = Integer .parseInt(args[0])
System .out.println( "Size of the Chess Board : " + maxLength)
System .out.println()
int iBaseSolution[] = new int [maxLength]
iBaseSolution = baseSolution()
for ( int iRepeat = 0 iRepeat < 10 iRepeat++)
{
long time = System .currentTimeMillis()
next = previous = bast = calc = 0
System .out.println( "========== Max Step : " + Math .pow(2,iRepeat) + " ==========" )
copySolution(iBaseSolution, iSolution)
copySolution(iBaseSolution, itSolution)
iSolutionH = itSolutionH = iBastSolutionH = computeH(iSolution)
printSolution(iSolution, ’Q’ ’_’ "Base Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
int accepted = 0
double temperature = initialTemperature
while ( temperature > finalTemperature )
{
//accepted = 0
for ( int step = 0 step < Math .pow(2,iRepeat) step++)
{
System .out.println( "************** Step : " + step + " **************" )
changeSolution ( iSolution ) iSolutionH = computeH(iSolution)
printSolution(iSolution, ’q’ ’-’ "Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
if ( iSolutionH < itSolutionH ){
nextStateSolution()
} else {
calC++
double randomTest = smallRandom()
double delta = iSolutionH - itSolutionH
double calculation = Math .exp( -delta / temperature )
if ( calculation > randomTest )
{
accepted++
= Page 1 =
nextStateSolution()
} else {
previousStateSolution()
}
}
if ( iBastSolutionH < 1.0 ){ break }
}
temperature *= alpha
if ( iBastSolutionH < 1.0 ){ break }
}
System .out.println( "-------------------------------------" )
printSolution(iSolution, ’Q’ ’-’ "Final Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
System .out.println( "Temperature : " + temperature)
System .out.println( "\n\n" )
time = System .currentTimeMillis()-time
Upshot += Math .pow(2,iRepeat) + "\t\t" + iBastSolutionH + "\t\t" + (( double )time/1000)+ "s" + "\t" + accepted +
"\t\t" + next + "\t" + previous + "\t\t" + bast + "\t\t" + calc + "\t\t" + temperature + "\n"
}
System .out.println( "Upshot :" )
System .out.println( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^" )
System .out.println( "step \t Energy Solution \t Time \t Accepted \t Next \t Previous \t Bast \t Calculation \t\t Temperature
" )
System .out.println( "--------------------------------------------------------------------------------------------------------
----------------------------" )
System .out.println(Upshot)
}
static double smallRandom(){ return random.nextDouble()}
static int largeRandom( int maximum ){ return random.nextInt ( maximum )}
public static void nextStateSolution()
{
next++
copySolution(iSolution,itSolution) itSolutionH = iSolutionH
System .out.println( "Next State Solution " + bastStateSolution())
}
public static void previousStateSolution()
{
previous++
copySolution(itSolution,iSolution) iSolutionH = itSolutionH
System .out.println( "Previous State Solution" + bastStateSolution())
}
public static String bastStateSolution()
{
if ( itSolutionH < iBastSolutionH )
{
bast++
iBastSolutionH = itSolutionH
return " (Bast) "
}
return ""
}
public static void copySolution( int iSourceArray[], int iDestArray[])
{
for ( int i=0 i < maxLength i++)
iDestArray[i] = iSourceArray[i]
}
public static int [] baseSolution()
{
int [] iBuffer= new int [maxLength]
for ( int i=0 i < maxLength i++)
{
int x = largeRandom( maxLength )
for ( int j=0 j<i j++)
if ( x == iBuffer[j] )
while ( x == iBuffer[j] ){x = largeRandom( maxLength )j=0}
iBuffer[i] = x
}
return iBuffer
}
static int computeH ( int iArray[] )
{
int H = 0
for ( int i=0 i<(maxLength-1) i++)
{
int x = 0
for ( int j=(i+1) j<maxLength j++)
{
x++
if ( (iArray[i] - x) == iArray[j] ){ H++ }
if ( (iArray[i] + x) == iArray[j] ){ H++ }
}
}
return H
}
static void changeSolution ( int array[] )
= Page 2 =
{
int tempValue, x, y
x = largeRandom( maxLength )
y = largeRandom( maxLength )
while ( x == y ){ y = largeRandom( maxLength )}
tempValue = array[x]
array[x] = array[y]
array[y] = tempValue
System .out.println( "Change Solution ( " + array[x] + "<>" + array[y] + " )" )
}
static void printSolution( int [] array, char qu, char em, String Description)
{
System .out.println ( Description )
for int ( i=0i<array.length i++){
for int ( j=0j<array.length j++){
if (i==array[j]){ System .out.print (qu + " " )}
else { System .out.print (em + " " )}
}
System .out.println()
}
}
} //end public class SimualatedAnnealing]
package simulatedannealing
import java.util.*
import java.lang. Math .*
public class Main
{
static int maxLength = 8 //Default Size of the Chess Board
static double initialTemperature = 30
static double finalTemperature = 0.5
static double alpha = 0.99
static Random random = new Random ( System .currentTimeMillis())
static int iSolution[] = new int [maxLength] static int iSolutionH
static int itSolution[] = new int [maxLength] static int itSolutionH
static int iBastSolutionH
static String Upshot = ""
static int next = 0
static int previous = 0
static int bast = 0
static int calc = 0
public static void main ( String [] args )
{
if (args.length>0)
maxLength = Integer .parseInt(args[0])
System .out.println( "Size of the Chess Board : " + maxLength)
System .out.println()
int iBaseSolution[] = new int [maxLength]
iBaseSolution = baseSolution()
for ( int iRepeat = 0 iRepeat < 10 iRepeat++)
{
long time = System .currentTimeMillis()
next = previous = bast = calc = 0
System .out.println( "========== Max Step : " + Math .pow(2,iRepeat) + " ==========" )
copySolution(iBaseSolution, iSolution)
copySolution(iBaseSolution, itSolution)
iSolutionH = itSolutionH = iBastSolutionH = computeH(iSolution)
printSolution(iSolution, ’Q’ ’_’ "Base Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
int accepted = 0
double temperature = initialTemperature
while ( temperature > finalTemperature )
{
//accepted = 0
for ( int step = 0 step < Math .pow(2,iRepeat) step++)
{
System .out.println( "************** Step : " + step + " **************" )
changeSolution ( iSolution ) iSolutionH = computeH(iSolution)
printSolution(iSolution, ’q’ ’-’ "Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
if ( iSolutionH < itSolutionH ){
nextStateSolution()
} else {
calC++
double randomTest = smallRandom()
double delta = iSolutionH - itSolutionH
double calculation = Math .exp( -delta / temperature )
if ( calculation > randomTest )
{
accepted++
= Page 1 =
nextStateSolution()
} else {
previousStateSolution()
}
}
if ( iBastSolutionH < 1.0 ){ break }
}
temperature *= alpha
if ( iBastSolutionH < 1.0 ){ break }
}
System .out.println( "-------------------------------------" )
printSolution(iSolution, ’Q’ ’-’ "Final Solution : " , , + iSolutionH + " (Tadad Barkhordha)" )
System .out.println( "Temperature : " + temperature)
System .out.println( "\n\n" )
time = System .currentTimeMillis()-time
Upshot += Math .pow(2,iRepeat) + "\t\t" + iBastSolutionH + "\t\t" + (( double )time/1000)+ "s" + "\t" + accepted +
"\t\t" + next + "\t" + previous + "\t\t" + bast + "\t\t" + calc + "\t\t" + temperature + "\n"
}
System .out.println( "Upshot :" )
System .out.println( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^" )
System .out.println( "step \t Energy Solution \t Time \t Accepted \t Next \t Previous \t Bast \t Calculation \t\t Temperature
" )
System .out.println( "--------------------------------------------------------------------------------------------------------
----------------------------" )
System .out.println(Upshot)
}
static double smallRandom(){ return random.nextDouble()}
static int largeRandom( int maximum ){ return random.nextInt ( maximum )}
public static void nextStateSolution()
{
next++
copySolution(iSolution,itSolution) itSolutionH = iSolutionH
System .out.println( "Next State Solution " + bastStateSolution())
}
public static void previousStateSolution()
{
previous++
copySolution(itSolution,iSolution) iSolutionH = itSolutionH
System .out.println( "Previous State Solution" + bastStateSolution())
}
public static String bastStateSolution()
{
if ( itSolutionH < iBastSolutionH )
{
bast++
iBastSolutionH = itSolutionH
return " (Bast) "
}
return ""
}
public static void copySolution( int iSourceArray[], int iDestArray[])
{
for ( int i=0 i < maxLength i++)
iDestArray[i] = iSourceArray[i]
}
public static int [] baseSolution()
{
int [] iBuffer= new int [maxLength]
for ( int i=0 i < maxLength i++)
{
int x = largeRandom( maxLength )
for ( int j=0 j<i j++)
if ( x == iBuffer[j] )
while ( x == iBuffer[j] ){x = largeRandom( maxLength )j=0}
iBuffer[i] = x
}
return iBuffer
}
static int computeH ( int iArray[] )
{
int H = 0
for ( int i=0 i<(maxLength-1) i++)
{
int x = 0
for ( int j=(i+1) j<maxLength j++)
{
x++
if ( (iArray[i] - x) == iArray[j] ){ H++ }
if ( (iArray[i] + x) == iArray[j] ){ H++ }
}
}
return H
}
static void changeSolution ( int array[] )
= Page 2 =
{
int tempValue, x, y
x = largeRandom( maxLength )
y = largeRandom( maxLength )
while ( x == y ){ y = largeRandom( maxLength )}
tempValue = array[x]
array[x] = array[y]
array[y] = tempValue
System .out.println( "Change Solution ( " + array[x] + "<>" + array[y] + " )" )
}
static void printSolution( int [] array, char qu, char em, String Description)
{
System .out.println ( Description )
for int ( i=0i<array.length i++){
for int ( j=0j<array.length j++){
if (i==array[j]){ System .out.print (qu + " " )}
else { System .out.print (em + " " )}
}
System .out.println()
}
}
} //end public class SimualatedAnnealing]