Sunteți pe pagina 1din 5

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei


Catedra Automatica si Tehnologii Informationale

RAPORT
Lucrarea de laborator nr.1
la Cercetari Operaionale
Tema: Optimizarea necondiionat
Varianta 9

A efectuat:
st. gr. SI-141

E. Cucu

A verificat:
lector superior

V.Bagrin

Chiinu 2016

Scopul lucrrii

Studiul metodelor de optimizare neliniar fr restricii.


Definirea i utilizarea unor proceduri pentru minimizarea funciilor cu ajutorul
metodei gradientului i a metodelor de direcii conjugate cu o eroare mai mic
= 105 .
Analiza rezultatelor obinute, inclusiv stabilirea tipului minimului local sau global.
S se compare rezultatele, lund n considerare numrul de iteraii, evalurile pentru
funcie si gradient.

Mersul lucrrii
S se determine minimul global al funciei (, ) = 2 + 2 + 2 2 3
Varianta 9 : a = 2, b = 4.
Aproximaia iniiala (0 , 0 ) = (1,1)
Argumentarea solutiei:
4 + 2 2 = 0 => = 0.3
2 + 8 3 = 0 => = 0.2
4 2
det(1) = det(4) = 4 > 0 ; det(2) = 20 > 0 deci (0.3,0,2) este punct minim.
2 8
1. Determinam gradientul funciei (, ) = (

4 + 2 2
4
) = (1,1) = ( )
2 + 8 3
7

2. Determinam (alfa) din formula


(2(1-4 )^2+2*(1-4 )(1-7 )+4(1-7 )^2-2(1-4 )-3(1-7 ))' = 0
3. Cu ajutorul metodei iterative cu condiia de stopare ||( , )|| < 105,
determinam , minimul global al funciei (, ) cu dupa forula
( , ) = (1 , 1 ) (1 , 1 ).

Listingul programului
import javacalculus.core.CALC;
import javacalculus.core.CalcParser;
import javacalculus.evaluator.CalcSUB;
import javacalculus.struct.CalcDouble;
import javacalculus.struct.CalcObject;
import javacalculus.struct.CalcSymbol;
//2*x^2+2*x*y+4*y^2-2*x-3*y
import java.util.Scanner;
public class MinimumCalculate
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
System.out.println("Enter expression:");
String expressionFunction = in.nextLine();
CalcParser parser = new CalcParser();
CalcObject resultX = parser.parse("DIFF(" + expressionFunction + ", "

+ "x" + ")").evaluate();
CalcObject resultY = parser.parse("DIFF(" + expressionFunction + ", "
+ "y" + ")").evaluate();
CalcObject resultFunction =
parser.parse(expressionFunction).evaluate();
System.out.println("Gradient Method");
gradientMethod(expressionFunction, resultX, resultY, resultFunction);
System.out.println("Hestenes-Stiefel Method");
hestenesStiefelMethod(4,2,8,1,1,resultX, resultY, resultFunction);
}
private static void hestenesStiefelMethod(double h11, double h12, double
h22, double firstX, double firstY, CalcObject resultX, CalcObject resultY,
CalcObject resultFunction) throws InterruptedException {
double x = 0,y = 0;
double alpha;
double[] d0 = new double[2];
double[] d1 = new double[2];
double gradient0X = getValue(firstX, firstY, resultX);
double gradient0Y = getValue(firstX, firstY, resultY);
if((int)gradient0X == 0 && (int)gradient0Y == 0)
System.out.println("x = " + firstX + "\ny = " + firstY + "\nf(x) = " +
getValue(firstX,firstY,resultFunction));
else {
d0[0] = gradient0X * (-1);
d0[1] = gradient0Y * (-1);
alpha = ((gradient0X*d0[0]+gradient0Y*d0[1])/((2*d0[0]+1*d0[1])*d0[0])+((1*d0[0]+1*d0
[1])*d0[1]));
x = firstX + (alpha*d0[0]);
y = firstY + (alpha*d0[1]);
if((int)getValue(x,y,resultX) == 0 && (int)getValue(x,y,resultY)
== 0) {
System.out.println("x = " + x + "\ny = " + y + "\nf(x) = " +
getValue(x,y,resultFunction));
}
else {
double x1,y1;
x1 = getValue(x,y,resultX);
y1 = getValue(x,y,resultY);
while((int)x1 != 0 && (int)y1 != 0) {
x1 = getValue(x, y, resultX);
y1 = getValue(x, y, resultY);
d1[0] = -x1 + ((((x1 * x1) + (y1 * y1)) / ((gradient0X *
gradient0X) + gradient0Y * gradient0Y)) * d0[0]);
d1[1] = -y1 + ((((x1 * x1) + (y1 * y1)) / ((gradient0X *
gradient0X) + gradient0Y * gradient0Y)) * d0[1]);
alpha = -((x1 * d1[0] + y1 * d1[1]) / (((h11 * d1[0] +
h12 * d1[1]) * d1[0]) + ((h12 * d1[0] + h22 * d1[1]) * d1[1])));
gradient0X = x1;
gradient0Y = y1;
x = x + (alpha * d1[0]);
y = y + (alpha * d1[1]);
}
System.out.println("x = " + x + "\ny = " + y + "\nf(x) = " +
getValue(x,y,resultFunction));
}
}
}
private static double getValue(double prevX, double prevY, CalcObject
result) {
result = substituteVariable(result, "x", prevX);
result = substituteVariable(result, "y", prevY);
result = CALC.SYM_EVAL(result);
return Double.parseDouble(result.toString());
}

private static String getAlpha(double prevX, double prevY, String


expression, CalcObject resultX, CalcObject resultY) {
resultX = substituteVariable(resultX, "x", prevX);
resultX = substituteVariable(resultX, "y", prevY);
resultY = substituteVariable(resultY, "x", prevX);
resultY = substituteVariable(resultY, "y", prevY);
resultX = CALC.SYM_EVAL(resultX);
resultY = CALC.SYM_EVAL(resultY);
expression = expression.replace("x","(1-" +
(int)Double.parseDouble(resultX.toString()) + "*alpha)");
expression = expression.replace("y","(1-" +
(int)Double.parseDouble(resultY.toString()) + "*alpha)");
return expression;
}
static CalcObject substituteVariable(CalcObject input, String var, double
number) {
CalcSymbol symbol = new CalcSymbol(var);
CalcDouble value = new CalcDouble(number);
return CalcSUB.numericSubstitute(input, symbol, value);
}
private static void gradientMethod(String expression, CalcObject resultX,
CalcObject resultY, CalcObject resultFunction) {
double prevX = 1,prevY = 1;
double x = 0,y = 0;
double alpha = 0.5;
double gradientX;
double gradientY;
double error = 1000000;
int iterator = 0;
while(error > 0.001) {
iterator++;
gradientX = getValue(prevX, prevY, resultX);
gradientY = getValue(prevX, prevY, resultY);
x = prevX - (alpha * gradientX);
y = prevY - (alpha * gradientY);
if(getValue(x,y,resultFunction) getValue(prevX,prevY,resultFunction) > 0) {
alpha /= 2;
iterator--;
}
else {
error = Math.sqrt(Math.pow(gradientX, 2) +
Math.pow(gradientY, 2));
prevX = x;
prevY = y;
}
}
System.out.println("x = " + x);
if (expression.contains("y")) {
System.out.println("y = " + y);
System.out.println("f(x,y) = " + getValue(x, y, resultFunction));
}
else System.out.println("f(x) = " + getValue(x,y,resultFunction));
System.out.println("iterator = " + iterator);
}
}

Afisarea rezulatelor obinute

Concluzie
n urma efecturii lucrrii de laborator, am obinut cunotine in determinarea
minimului global al unei funcii.
n programul elaborat utilizatorul introduce expresia funciei si automat se
genereaza funcia pentru determinarea lui alfa, la rezolvarea acestei ecuatii(derivarea
apoi determinarea lui alfa din egalarea ecuatiei cu 0) automat de program am ntilnit
greuti deoarece nu am putut realiza determinarea necunoscutei, de pe alta parte
programul calculeaza automat gradientul lui x si y astfel generindu-se o functie pentru
determinarea lui alfa, usor determnd manual sau folosindu-ne de platforma
wolframalpha.com unde copiem functia obtinuta si obtinem rezultatul.
Dupa determinarea lui alfa l introducem in program si automat se calculeaz
minimul global al funciei i afind la ecran valoarea minima a lui x,y funcia in
punctul dat si numarul de iteraii realizate.