avatarThe Pragmatic Programmers

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1269

Abstract

.</p><p id="53c7">To better understand penalty functions, consider this: the speed limit changes depending on the road you’re driving on. Law enforcement would love it if your car automatically detected the speed limit of a certain road and allowed you to drive only at or below that speed. Of course, implementing this would be impossible — there are too many roads, and speed limits change rapidly between them. Rather than putting constraints on cars, the law introduces a penalty for driving over the speed limit. The penalty is a speeding ticket. The cost of speeding outweighs the cost of driving within the speed limit. In this way, the law eliminates the need for explicit constraints and instead punishes drivers who don’t obey the rules.</p><p id="ac92">Penalty functions can be simple or complex depending on your problem. Oftentimes, it’s useful to make the penalty proportional to how much the constraint was violated. In the speeding example, you could fine somebody $10 for every mile per hour they drove above the speed limit. This means excessive speeders are punished more heavily than those who operate close to the limits.</p><h1 id="b885">Penalty Functions</h1><h2 id="118c">INFORMATION</h2><p id="598d">If you research penalty functions outside of

Options

this book, you’ll find several common penalty functions for optimization problems such as delta penalty, quadratic penalty, or closest valid penalty. It can be useful to know these for advanced problems, but it’s not necessary right now. As long as you understand what penalty functions are meant to accomplish, you’ll be able to understand and apply more complex methods of implementing them.</p><p id="b167"><i>👈 <a href="https://readmedium.com/optimizing-cargo-loads-dd0c4c83e6d0">Optimizing Cargo Loads</a> | <a href="https://readmedium.com/table-of-contents-879fc8614df">TOC</a> | <a href="https://readmedium.com/applying-a-penalty-to-the-shipping-problem-8a90107466cf">Applying a Penalty to the Shipping Problem</a> 👉</i></p><p id="895d"><i>Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats <a href="https://pragprog.com/titles/smgaelixir">directly from the Pragmatic Programmers</a>. If you notice a code error or formatting mistake, please let us know <a href="https://readmedium.com/how-to-report-errata-4e164674347a">here</a> so that we can fix it.</i></p><figure id="9ad8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OZt5jtL90ZqYviPotaxYkQ.jpeg"><figcaption></figcaption></figure></article></body>

Introducing Penalty Functions

Genetic Algorithms in Elixir — by Sean Moriarity (36 / 101)

👈 Optimizing Cargo Loads | TOC | Applying a Penalty to the Shipping Problem 👉

You need a way to account for solutions that aren’t considered valid — meaning they don’t meet the constraints defined by the problem. For example, in your cargo problem, you need a way to penalize solutions that exceed the weight limit so your algorithm doesn’t produce an invalid solution. A penalty function is a function applied to constraint satisfaction problems for the purpose of reducing the constraint satisfaction problem into an unconstrained problem. Rather than putting constraints on possible solutions, penalty functions incur a cost on solutions that violate a constraint of the original problem.

In layman’s terms, that means you take points away from solutions that aren’t valid so they don’t get considered better than solutions that are valid.

To better understand penalty functions, consider this: the speed limit changes depending on the road you’re driving on. Law enforcement would love it if your car automatically detected the speed limit of a certain road and allowed you to drive only at or below that speed. Of course, implementing this would be impossible — there are too many roads, and speed limits change rapidly between them. Rather than putting constraints on cars, the law introduces a penalty for driving over the speed limit. The penalty is a speeding ticket. The cost of speeding outweighs the cost of driving within the speed limit. In this way, the law eliminates the need for explicit constraints and instead punishes drivers who don’t obey the rules.

Penalty functions can be simple or complex depending on your problem. Oftentimes, it’s useful to make the penalty proportional to how much the constraint was violated. In the speeding example, you could fine somebody $10 for every mile per hour they drove above the speed limit. This means excessive speeders are punished more heavily than those who operate close to the limits.

Penalty Functions

INFORMATION

If you research penalty functions outside of this book, you’ll find several common penalty functions for optimization problems such as delta penalty, quadratic penalty, or closest valid penalty. It can be useful to know these for advanced problems, but it’s not necessary right now. As long as you understand what penalty functions are meant to accomplish, you’ll be able to understand and apply more complex methods of implementing them.

👈 Optimizing Cargo Loads | TOC | Applying a Penalty to the Shipping Problem 👉

Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Smgaelixir
Recommended from ReadMedium