0
using System;
using System. Collections.Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items. Clear();
comboBox2.Items. Clear();
comboBox3.Items. Clear();
int NS;
int NA;
double SMALL;
NA = 2;
NS = 2;
double[, ,] tpm = new double[NA - 1, NS - 1, NS - 1];
double[, ,] trm = new double[NA - 1, NS - 1, NS - 1];
SMALL = -100000;
tpm[0, 0, 0] = 0.7;
tpm[0, 0, 1] = 0.3;
tpm[0, 1, 0] = 0.4;
tpm[0, 1, 1] = 0.6;
tpm[1, 0, 0] = 0.9;
tpm[1, 0, 1] = 0.1;
tpm[1, 1, 0] = 0.2;
tpm[1, 1, 1] = 0.8;
trm[0, 0, 0] = 6;
trm[0, 0, 1] = -5;
trm[0, 1, 0] = 7;
trm[0, 1, 1] = 12;
trm[1, 0, 0] = 10;
trm[1, 0, 1] = 17;
trm[1, 1, 0] = -14;
trm[1, 1, 1] = 13;
int state, next_state, iteration, done, row, col, action, best_action;
int[] policy = new int[NS - 1];
double[,] G = new double[NS - 1, NS];
double[] x = new double[NS - 1];
double large, sum, rho;
for (state = 0; state <= NS - 1; state++)
{
policy[state] = 0;
}
iteration = 0;
done = 1;
while (done == 1)
{
for (row = 0; row <= NS - 1; row++)
{
for (col = 0; col <= NS - 1; col++)
{
if (col == 0)
{
G[row, col] = 1;
}
else
{
if (row == col)
{
G[row, col] = 1 - tpm[policy[row], row, col];
}
else
{
G[row, col] = -tpm[policy[row], row, col];
}
}
}
}
for (state = 0; state <= NS - 1; state++)
{
sum = 0;
for (next_state = 0; next_state <= NS - 1; next_state++)
{
sum = sum + (tpm[policy[state], state, next_state] * trm[policy[state], state, next_state]);
}
G[state, NS] = sum;
}
///Solver
int row_a, col_a, row1_a, col2_a, col1_a, pivot_row_a;
double factor_a, pivot_a, temp_a;
for (col_a = 0; col_a <= NS - 1; col_a++)
{
pivot_a = -0.1;
for (row_a = col_a; row_a <= NS - 1; row_a++)
{
if (Math.Abs(G[row_a, col_a]) > pivot_a)
{
pivot_a = Math.Abs(G[row_a, col_a]);
pivot_row_a = row_a;
}
}
if (pivot_a <= 1E-05)
{
break;
}
if (pivot_row_a != col_a)
{
for (col1_a = col_a; col1_a <= NS; col1_a++)
{
temp_a = G[col_a, col1_a];
G[col_a, col1_a] = G[pivot_row_a, col1_a];
G[pivot_row_a, col1_a] = temp_a;
}
}
for (row1_a = 0; row1_a <= NS - 1; row1_a++)
{
if (row1_a != col_a)
{
factor_a = G[row1_a, col_a] / G[col_a, col_a];
for (col2_a = col_a; col2_a <= NS; col2_a++)
{
G[row1_a, col2_a] = G[row1_a, col2_a] - factor_a * G[col_a, col2_a];
}
}
}
}
for (row_a = 0; row_a <= NS - 1; row_a++)
{
x[row_a] = G[row_a, NS] / G[row_a, row_a];
}
rho = x[0];
comboBox1.Items.Add(Convert. ToString(iteration) + ", " + Convert. ToString(x[0]));
x[0] = 0;
done = 0;
for (state = 0; state <= NS - 1; state++)
{
large = SMALL;
best_action = 0;
for (action = 0; action <= NA - 1; action++)
{
sum = 0;
for (next_state = 0; next_state <= NS - 1; next_state++)
{
sum = sum + (tpm[action, state, next_state] * (trm[action, state, next_state] + x[next_state]));
}
if (sum > large)
{
large = sum;
best_action = action;
}
}
if (policy[state] != best_action)
{
policy[state] = best_action;
done = 1;
}
}
iteration = iteration + 1;
}
for (state = 0; state <= NS - 1; state++)
{
comboBox2.Items.Add(Convert. ToString(state + 1) + ", " + Convert. ToString(policy[state] + 1));
}
for (state = 0; state <= NS - 1; state++)
{
comboBox2.Items.Add(Convert. ToString(state + 1) + ", " + Convert. ToString(x[state]));
}
textBox1.Text = "iterasyon Sayısı = " + iteration;
}
private void button2_Click(System. Object sender, System. EventArgs e)
{
Form1.Activate();
}
}
}
Tümünü Göster