ИДЗ / МО ИДЗ вариант Б
.pdfxi[1] = (x1[1] + x2[1] + x3[1]) - (heavy[1] + light[1]);
centre[0] = (1 / Convert.ToDecimal(n)) * (xi[0] + light[0]);
centre[1] = (1 / Convert.ToDecimal(n)) * (xi[1] + light[1]);
decimal[] mirror = { centre[0] + alpha * (centre[0] - heavy[0]), centre[1] + alpha * (centre[1] - heavy[1]) };
if (F45(mirror[0], mirror[1]) < F45(light[0], light[1]))
{
decimal[] stretch = { centre[0] + gamma * (mirror[0] - centre[0]), centre[1] + gamma * (mirror[1] - centre[1]) };
if (F45(stretch[0], stretch[1]) < F45(light[0], light[1]))
{
heavy = stretch;
if (Ostanov(heavy, light, xi, centre) < e)
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
31
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
x1 = heavy;
x2 = light;
x3 = xi;
NelderMead();
}
}
else
{
heavy = mirror;
if (Ostanov(heavy, light, xi, centre) < e)
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
32
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
x1 = heavy;
x2 = light;
x3 = xi;
NelderMead();
}
}
}
else
{
if ((F45(mirror[0], mirror[1]) > F45(xi[0], xi[1])) || (F45(mirror[0], mirror[1]) > F45(light[0], light[1])))
{
if (F45(mirror[0], mirror[1]) > F45(heavy[0], heavy[1]))
{
heavy[0] = light[0] + (0.5m * (heavy[0] - light[0]));
heavy[1] = light[1] + (0.5m * (heavy[1] - light[1]));
xi[0] = light[0] + (0.5m * (xi[0] - light[0]));
xi[1] = light[1] + (0.5m * (xi[1] - light[1]));
if (Ostanov(heavy, light, xi, centre) < e)
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
33
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
Console.Write($"({Decimal.Round(light[0], 3, MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) |
"); |
|
Console.Write($"z = {Decimal.Round(F45(light[0], |
||
light[1]), 3, MidpointRounding.AwayFromZero)} |
"); |
|
Console.WriteLine($"{iteration}"); |
||
x1 |
= heavy; |
|
x2 |
= light; |
|
x3 |
= xi; |
|
NelderMead();
}
}
else
{
decimal[] compress = { centre[0] + beta * (centre[0] - heavy[0]), centre[1] + beta * (centre[1] - heavy[1]) };
if (F45(compress[0], compress[1]) > F45(heavy[0], heavy[1]))
{
heavy[0] = light[0] + (0.5m * (heavy[0] - light[0])); heavy[1] = light[1] + (0.5m * (heavy[1] - light[1]));
xi[0] = light[0] + (0.5m * (xi[0] - light[0])); xi[1] = light[1] + (0.5m * (xi[1] - light[1]));
if (Ostanov(heavy, light, xi, centre) < e)
34
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
Console.Write($"({Decimal.Round(light[0], 3, MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) |
"); |
|
|
|
Console.Write($"z = {Decimal.Round(F45(light[0], |
||
light[1]), 3, MidpointRounding.AwayFromZero)} |
"); |
||
|
Console.WriteLine($"{iteration}"); |
||
|
x1 |
= heavy; |
|
|
x2 |
= light; |
|
|
x3 |
= xi; |
|
NelderMead();
}
}
else
{
heavy = compress;
if (Ostanov(heavy, light, xi, centre) < e)
{
Console.WriteLine("Минимум:");
35
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
x1 = heavy;
x2 = light;
x3 = xi;
NelderMead();
}
}
}
}
else
{
heavy = mirror;
if (Ostanov(heavy, light, xi, centre) < e)
{
Console.WriteLine("Минимум:");
36
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
}
else
{
iteration++;
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(F45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iteration}");
x1 = heavy;
x2 = light;
x3 = xi;
NelderMead();
}
}
}
}
NelderMead();
37
Приложение Г Метод наискорейшего спуска
decimal a0 = 0;
decimal b0 = 2;
decimal k = 0.0006m;
decimal e = 0.001m;
int iteration = 0;
decimal delta;
decimal F45(decimal x, decimal y)
{
decimal z1 = 5 * Convert.ToDecimal(Math.Pow(Convert.ToDouble(x) - 2, 2)) + 5 * Convert.ToDecimal(Math.Pow(Convert.ToDouble(y) - 3, 2));
return z1;
//decimal z2 = Convert.ToDecimal(Math.Pow(Convert.ToDouble(x) - 5, 2) + Math.Pow(Convert.ToDouble(y) - 6, 2) + 50 * Math.Pow(Convert.ToDouble(y + (4 * x) - 6), 2) + 1.4);
//return z2;
}
decimal[] x0 = { 0, 0 };
void FastDescent()
{
decimal tFD;
decimal[] Gradient(decimal x, decimal y)
{
decimal[] gradient1 = { (10 * x) - 20, (10 * y) - 30 };
return gradient1;
38
//decimal[] gradient2 = { (1602 * x) + (400 * y) - 2410, (400 * x) + (102 * y) - 612 };
//return gradient2;
}
decimal Fi(decimal tFD)
{
decimal zfi = F45(x0[0] - (tFD * Gradient(x0[0], x0[1])[0]), x0[1] - (tFD * Gradient(x0[0], x0[1])[1]));
return zfi;
}
decimal DichotomyFD()
{
decimal x1 = (a0 + b0 - k) / 2; decimal x2 = (a0 + b0 + k) / 2;
decimal f1 = Fi(x1); decimal f2 = Fi(x2);
if (f1 <= f2)
{
b0 = x2;
}
else
{
a0 = x1;
}
delta = b0 - a0;
39
if (delta < e)
{
return x1;
}
else
{
return DichotomyFD();
}
}
tFD = DichotomyFD();
decimal[] xnext = { x0[0] - (tFD * Gradient(x0[0], x0[1])[0]), x0[1] - (tFD * Gradient(x0[0], x0[1])[1]) };
if (Math.Abs(Gradient(xnext[0], xnext[1])[0]) < e && Math.Abs(Gradient(xnext[0], xnext[1])[1]) < e)
{
Console.WriteLine("Минимум:");
Console.WriteLine($"x = {Decimal.Round(xnext[0], 3, MidpointRounding.AwayFromZero)}, y = {Decimal.Round(xnext[1], 3, MidpointRounding.AwayFromZero)}, z = {Decimal.Round(F45(xnext[0], xnext[1]), 3, MidpointRounding.AwayFromZero)} {iteration}");
}
else
{
iteration++;
x0 = xnext;
Console.WriteLine($"x = {Decimal.Round(xnext[0], 3, MidpointRounding.AwayFromZero)}, y = {Decimal.Round(xnext[1], 3, MidpointRounding.AwayFromZero)}, z = {Decimal.Round(F45(xnext[0], xnext[1]), 3, MidpointRounding.AwayFromZero)} {iteration}");
40
