Objective Function

An objective function defines the quantity to be optimized in an optimization problem. It maps decision variables to a numerical value representing the objective to maximize or minimize.

For example, in a linear program:

Maximize z = 3x + 5y

z is the objective function to maximize.

Java - Portfolio optimization:

1
2
3
4
5
6
7
8
9
double portfolioReturn(double[] allocations) {
  double return = 0;
  for (int i = 0; i < assets.length; i++) {
    return += allocations[i] * assets[i].return; 
  }
  return return;
}

// Objective is to maximize portfolioReturn

C++ - Traveling salesman problem:

1
2
3
4
5
6
7
8
9
double tourCost(vector<int> tour) {
  double cost = 0;
  for (int i = 0; i < tour.size(); i++) {
    cost += distances[tour[i]][tour[(i+1)%tour.size()]];
  }
  return cost;
}

// Objective is to minimize tourCost

Python - Linear regression:

1
2
3
4
5
6
7
def error(coefficients, X, y):
  predicted = coefficients[0] 
  for i in range(len(X[0])):
    predicted += coefficients[i+1] * X[:,i]
  return np.sum((predicted - y) ** 2)

# Objective is to minimize error  

The objective function quantifies optimality of solutions. Choosing a good objective function is key to formulating optimization problems.

In optimization problems, the objective function is a formula that defines the metric you aim to optimize. It takes a set of variables and produces a single value that you wish to minimize or maximize. For example, in a business model, an objective function could be to maximize profit or minimize costs.

Algorithm

  1. Define Variables: Variables that can be changed to optimize the objective.
  2. Expression: A mathematical expression using the variables.
  3. Optimize: Minimize or maximize the expression’s value.

Let’s consider an example where we aim to maximize the function (f(x, y) = 3x + 4y), subject to (x \geq 0) and (y \geq 0).

Java Code

In Java, you can define the objective function as a method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class ObjectiveFunction {
    public static double objective(double x, double y) {
        return 3 * x + 4 * y;
    }

    public static void main(String[] args) {
        double x = 5;
        double y = 2;
        double result = objective(x, y);
        System.out.println("Objective function value: " + result);
    }
}

C++ Code

In C++, you define it as a function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <iostream>

double objective(double x, double y) {
    return 3 * x + 4 * y;
}

int main() {
    double x = 5;
    double y = 2;
    double result = objective(x, y);
    std::cout << "Objective function value: " << result << std::endl;
    return 0;
}

Python Code

In Python, you can define it as a simple function.

1
2
3
4
5
6
7
def objective(x, y):
    return 3 * x + 4 * y

x = 5
y = 2
result = objective(x, y)
print(f"Objective function value: {result}")

In each of these examples, the objective function calculates the value for (f(x, y)). You can then feed it different (x) and (y) values to maximize or minimize this function according to your problem’s constraints.