Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lab1/Lab1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
8 changes: 8 additions & 0 deletions Lab2/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lab2/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Lab2/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lab2/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Lab2/Lab2.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
18 changes: 13 additions & 5 deletions Lab2/src/RunMethod.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import elements.MyFunction;
import elements.Vector;
import methods.ConjugateGradientMethod;
import methods.GradientDescent;
import methods.FastestDescent;

import java.util.List;

public class RunMethod {
public static void main(final String[] args) {
final GradientDescent method = new GradientDescent(0.01);
final GradientDescent gradientDescent = new GradientDescent(0.01);
final FastestDescent fastestDescent = new FastestDescent(0.01);
final ConjugateGradientMethod conjugateGradientMethod = new ConjugateGradientMethod();
final MyFunction function = new MyFunction(
list -> {
final double first = list.get(0), second = list.get(1);
return first * first + second * second + second;
return first * first + 40 * second * second + second;
},
List.of(
list -> 2 * list.get(0),
list -> 2 * list.get(1) + 1.
list -> 80 * list.get(1) + 1.
)
);
final Vector x = new Vector(List.of(3., -4.));
method.calc(function, x);
final Vector x = new Vector(List.of(1., 1.));
gradientDescent.calc(function, x);
System.out.println("-----------------------");
fastestDescent.calc(function, x);
System.out.println("-----------------------");
conjugateGradientMethod.calc(function, x);
}
}
1 change: 0 additions & 1 deletion Lab2/src/elements/MyFunction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package elements;

import java.util.List;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down
22 changes: 21 additions & 1 deletion Lab2/src/elements/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleUnaryOperator;
import java.util.stream.Collectors;

public class Vector {
Expand All @@ -24,6 +23,14 @@ public Vector multiply(final double mul) {
);
}

public Vector negate() {
final List<Double> value = new ArrayList<>();
for (int i = 0; i < point.size(); ++i) {
value.add(-point.get(i));
}
return new Vector(value);
}

public Vector subtract(final Vector vector) {
final List<Double> value = new ArrayList<>();
for (int i = 0; i < point.size(); ++i) {
Expand All @@ -32,10 +39,23 @@ public Vector subtract(final Vector vector) {
return new Vector(value);
}

public Vector add(final Vector vector) {
final List<Double> value = new ArrayList<>();
for (int i = 0; i < point.size(); ++i) {
value.add(point.get(i) + vector.point.get(i));
}
return new Vector(value);
}


public double abs() {
return Math.sqrt(point.stream().reduce(0., (arg1, arg2) -> arg1 + arg2 * arg2));
}

public double absSqr() {
return point.stream().reduce(0., (arg1, arg2) -> arg1 + arg2 * arg2);
}

@Override
public String toString() {
return "Vector{" +
Expand Down
37 changes: 37 additions & 0 deletions Lab2/src/methods/ConjugateGradientMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package methods;

import elements.MyFunction;
import elements.Vector;
import methods.result.IterationResult;
import methods.result.Result;

import java.util.function.DoubleUnaryOperator;

public class ConjugateGradientMethod {

public void calc(final MyFunction function, Vector x) {
Vector gradientFX = function.applyGradient(x);
Double fx = null;
Vector p = function.applyGradient(x).negate();
System.out.println("gradient abs: " + gradientFX.abs());
for (int cnt = 0; cnt <= x.getPoint().size(); cnt++) {
Vector finalGradientFX = gradientFX;
Vector finalX = x;
DoubleUnaryOperator fAlpha = alpha -> function.applyFunction(finalX.subtract(finalGradientFX.multiply(alpha)));
Method<? extends IterationResult> method = new Dichotomy(1e-9);
Result<? extends IterationResult> result = method.calcAllIterations(fAlpha, 0, 100);
double alpha = result.getPoint();

Vector xNext = x.add(p.multiply(alpha));
double beta = function.applyGradient(xNext).absSqr() / function.applyGradient(x).absSqr();
Vector pNext = function.applyGradient(xNext).negate().add(p.multiply(beta));
x = xNext;
p = pNext;
fx = function.applyFunction(x);
gradientFX = function.applyGradient(x);
System.out.println(String.format("%s val: %.10f", x, fx));
System.out.println("gradient abs: " + gradientFX.abs());
}
System.out.println(String.format("Result:\n%s val: %.10f", x, fx));
}
}
39 changes: 39 additions & 0 deletions Lab2/src/methods/Dichotomy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package methods;

import methods.result.DefaultIterationResult;
import methods.result.Result;

import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleUnaryOperator;

public class Dichotomy implements Method<DefaultIterationResult> {
private final double epsilon;
private final double delta;

public Dichotomy(double epsilon) {
this.epsilon = epsilon;
this.delta = epsilon / 2;
}

@Override
public Result<DefaultIterationResult> calcAllIterations(DoubleUnaryOperator f, double a, double b) {
double x1, x2, e = (b - a) / 2;
List<DefaultIterationResult> results = new ArrayList<>();
while (compare(e, epsilon)) {
x1 = (a + b - delta) / 2;
x2 = (a + b + delta) / 2;
double f1 = f.applyAsDouble(x1);
double f2 = f.applyAsDouble(x2);
results.add(new DefaultIterationResult(a, b, x1, x2, f1, f2));
if (compare(f2, f1)) {
b = x2;
} else {
a = x1;
}
e = (b - a) / 2;
}
return new Result<>((a + b) / 2, f.applyAsDouble((a + b) / 2), results);
}

}
39 changes: 39 additions & 0 deletions Lab2/src/methods/FastestDescent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package methods;

import elements.MyFunction;
import elements.Vector;
import methods.Dichotomy;
import methods.result.IterationResult;
import methods.result.Result;

import java.util.function.DoubleUnaryOperator;
import java.util.function.UnaryOperator;


public class FastestDescent {
private final double epsilon;

public FastestDescent(final double epsilon) {
this.epsilon = epsilon;
}

public void calc(final MyFunction function, Vector x) {
Vector gradientFX = function.applyGradient(x);
Double fx = null;
System.out.println("gradient abs: " + gradientFX.abs());
while (epsilon < gradientFX.abs()) {
Vector finalGradientFX = gradientFX;
Vector finalX = x;
DoubleUnaryOperator fAlpha = alpha -> function.applyFunction(finalX.subtract(finalGradientFX.multiply(alpha)));
Method<? extends IterationResult> method = new Dichotomy(epsilon);
Result<? extends IterationResult> result = method.calcAllIterations(fAlpha, 0, 100);
double alpha = result.getPoint();
x = x.subtract(gradientFX.multiply(alpha));
fx = function.applyFunction(x);
gradientFX = function.applyGradient(x);
System.out.println(String.format("%s val: %.10f", x, fx));
System.out.println("gradient abs: " + gradientFX.abs());
}
System.out.println(String.format("Result:\n%s val: %.10f", x, fx));
}
}
3 changes: 1 addition & 2 deletions Lab2/src/methods/GradientDescent.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package methods;

import elements.MyFunction;
import elements.Vector;
import elements.MyFunction;

public class GradientDescent {
private final double epsilon;
Expand Down Expand Up @@ -30,7 +30,6 @@ public void calc(final MyFunction function, Vector x) {
System.out.println(String.format("%s val: %.10f", x, fx));
System.out.println("gradient abs: " + gradientFX.abs());
}

System.out.println(String.format("Result:\n%s val: %.10f", x, fx));
}
}
15 changes: 15 additions & 0 deletions Lab2/src/methods/Method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package methods;

import methods.result.IterationResult;
import methods.result.Result;

import java.util.function.DoubleUnaryOperator;

public interface Method<R extends IterationResult> {

Result<R> calcAllIterations(DoubleUnaryOperator f, double a, double b);

default boolean compare(double x, double y) {
return x - y >= 0;
}
}
69 changes: 69 additions & 0 deletions Lab2/src/methods/result/DefaultIterationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package methods.result;

public class DefaultIterationResult implements IterationResult {
private final double a;
private final double b;
private final double x1;
private final double x2;

private final double fx1;
private final double fx2;

public DefaultIterationResult(double a, double b, double x1, double x2, double fx1, double fx2) {
this.a = a;
this.b = b;
this.x1 = x1;
this.x2 = x2;
this.fx1 = fx1;
this.fx2 = fx2;
}

public double getA() {
return a;
}

public double getB() {
return b;
}

public double getX1() {
return x1;
}

public double getX2() {
return x2;
}

public double getFx1() {
return fx1;
}

public double getFx2() {
return fx2;
}

public String tableArgs() {
return "A & B & X1 & X2 & F(X1) & F(X2) & B - A";
}

public String asTable() {
return String.format("%.7f & %.7f & %.7f & %.7f & %.7f & %.7f & %.7f",
getA(),
getB(),
getX1(),
getX2(),
getFx1(),
getFx2(),
getB() - getA());
}

public String toString() {
return String.format("%.16f %.16f %.16f %.16f %.16f %.16f",
getA(),
getB(),
getX1(),
getX2(),
getFx1(),
getFx2());
}
}
6 changes: 6 additions & 0 deletions Lab2/src/methods/result/IterationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package methods.result;

public interface IterationResult {
String asTable();
String tableArgs();
}
Loading