Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from Alpaca-zip/fix/outputs_and_default_values
Browse files Browse the repository at this point in the history
Fix/outputs and default values
  • Loading branch information
Alpaca-zip committed Jul 31, 2023
2 parents ff88de7 + acad3df commit e947d34
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ $ git clone https://github.com/Alpaca-zip/gradient_descent_algorithm.git
```
### 2. Install dependencies and compile the program.
```
$ python3 -m pip install matplotlib numpy
$ sudo apt-get install -y python3-matplotlib python3-numpy python3-dev
$ bash build.sh
```
### 3. Run the program.
1. Upon running the program, you need to input the objective function as vectors for two variables (x1, x2), each defined by their degree and coefficient. You're asked to enter the order matrices for x1, x2, and the coefficient respectively.
1. Upon running the program, you need to input the objective function as vectors for two variables $(x_{1}, x_{2})$, each defined by their degree and coefficient. You're asked to enter the order matrices for $x_{1}$, $x_{2}$, and the coefficient respectively.
2. Then, you define the gradient vector similar to the objective function, with two components, each being a function of x1 and x2.
3. Next, you provide an initial pair of numbers (x1, x2) as the starting point for gradient descent.
3. Next, you provide an initial pair of numbers $(x_{1}, x_{2})$ as the starting point for gradient descent.

**Example**

Expand All @@ -27,7 +27,7 @@ $$
\nabla f(x_{1}, x_{2}) = [4x_{1}+x_{2}, x_{1}+2x_{2}]^T
$$
```
$ run.sh
$ bash run.sh
Objective function
Enter the order matrix of x1: 2 0 1
Enter the order matrix of x2: 0 2 1
Expand All @@ -48,24 +48,30 @@ x2-component of the gradient
Enter the starting point (x1, x2): 1.5 1.5
Default values:
max_iter = 10000
max_iter = 100000
precision = 0.0001
xi = 0.3
tau = 0.9
xi = 0.0001
tau = 0.5
If these values are not suitable, please modify them in the code.
====================
Iteration: 1
x_curr = [1.5, 1.5]
grad_curr = [7.5, 4.5]
Current x1 and x2 values = [1.5, 1.5]
Objective Function Value = 9
Gradient = [7.5, 4.5]
Gradient Norm = 8.74643
Elapsed Time = 277 microseconds
====================
Iteration: 2
x_curr = [-0.853579, 0.0878523]
grad_curr = [-3.32647, -0.677875]
Current x1 and x2 values = [-0.375, 0.375]
Objective Function Value = 0.28125
Gradient = [-1.125, 0.375]
Gradient Norm = 1.18585
Elapsed Time = 587 microseconds
====================
...
```
### 4. The results will be displayed as figure.
Finally, the program outputs the contour and the path of the gradient descent as plots. You can modify parameters like max iterations, precision, step size in the code itself.

<img src="https://github.com/Alpaca-zip/dijkstra_algorithm/assets/84959376/e33b595b-f3d9-4204-aa60-29cc5580e4a5" width="600px">
<img src="https://github.com/Alpaca-zip/gradient_descent_algorithm/assets/84959376/1bde4e46-b624-4155-a275-eefdd537be40" width="600px">
1 change: 1 addition & 0 deletions include/gradient_descent_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <chrono>
#include <cmath>
#include <iostream>
#include <sstream>
Expand Down
18 changes: 13 additions & 5 deletions src/gradient_descent_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ void GradientDescentNode::displayFunction(

void GradientDescentNode::solveGradientDescent()
{
const int max_iter = 10000;
auto start_time = std::chrono::high_resolution_clock::now();
const int max_iter = 100000;
const double precision = 1e-4;
const double xi = 0.3;
const double tau = 0.9;
const double xi = 1e-4;
const double tau = 0.5;
std::cout << "\n";
std::cout << "Default values:" << std::endl;
std::cout << " max_iter = " << max_iter << std::endl;
Expand All @@ -131,13 +132,20 @@ void GradientDescentNode::solveGradientDescent()
x_curr = {_x_start, _y_start};

for (int i = 0; i < max_iter; i++) {
auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - start_time)
.count();
alpha = 1.0;
grad_curr = calcGradient(x_curr[0], x_curr[1]);
_path.push_back({x_curr[0], x_curr[1]});
std::cout << "====================" << std::endl;
std::cout << "Iteration: " << i + 1 << std::endl;
std::cout << " x_curr = [" << x_curr[0] << ", " << x_curr[1] << "]" << std::endl;
std::cout << " grad_curr = [" << grad_curr[0] << ", " << grad_curr[1] << "]" << std::endl;
std::cout << " Current x1 and x2 values = [" << x_curr[0] << ", " << x_curr[1] << "]"
<< std::endl;
std::cout << " Objective Function Value = " << calcObjective(x_curr[0], x_curr[1]) << std::endl;
std::cout << " Gradient = [" << grad_curr[0] << ", " << grad_curr[1] << "]" << std::endl;
std::cout << " Gradient Norm = " << norm(grad_curr[0], grad_curr[1]) << std::endl;
std::cout << " Elapsed Time = " << elapsed_time << " microseconds" << std::endl;

if (norm(grad_curr[0], grad_curr[1]) < precision) {
break;
Expand Down

0 comments on commit e947d34

Please sign in to comment.