mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 08:17:01 +00:00
Fix sphinx/build_docs warnings for other (#12482)
* Fix sphinx/build_docs warnings for other * Fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
ce036db213
commit
3622e940c9
|
@ -10,9 +10,10 @@ developed by Edsger Dijkstra that tests for safety by simulating the allocation
|
||||||
predetermined maximum possible amounts of all resources, and then makes a "s-state"
|
predetermined maximum possible amounts of all resources, and then makes a "s-state"
|
||||||
check to test for possible deadlock conditions for all other pending activities,
|
check to test for possible deadlock conditions for all other pending activities,
|
||||||
before deciding whether allocation should be allowed to continue.
|
before deciding whether allocation should be allowed to continue.
|
||||||
[Source] Wikipedia
|
|
||||||
[Credit] Rosetta Code C implementation helped very much.
|
| [Source] Wikipedia
|
||||||
(https://rosettacode.org/wiki/Banker%27s_algorithm)
|
| [Credit] Rosetta Code C implementation helped very much.
|
||||||
|
| (https://rosettacode.org/wiki/Banker%27s_algorithm)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
@ -75,7 +76,7 @@ class BankersAlgorithm:
|
||||||
def __need(self) -> list[list[int]]:
|
def __need(self) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Implement safety checker that calculates the needs by ensuring that
|
Implement safety checker that calculates the needs by ensuring that
|
||||||
max_claim[i][j] - alloc_table[i][j] <= avail[j]
|
``max_claim[i][j] - alloc_table[i][j] <= avail[j]``
|
||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
list(np.array(self.__maximum_claim_table[i]) - np.array(allocated_resource))
|
list(np.array(self.__maximum_claim_table[i]) - np.array(allocated_resource))
|
||||||
|
@ -86,7 +87,9 @@ class BankersAlgorithm:
|
||||||
"""
|
"""
|
||||||
This function builds an index control dictionary to track original ids/indices
|
This function builds an index control dictionary to track original ids/indices
|
||||||
of processes when altered during execution of method "main"
|
of processes when altered during execution of method "main"
|
||||||
Return: {0: [a: int, b: int], 1: [c: int, d: int]}
|
|
||||||
|
:Return: {0: [a: int, b: int], 1: [c: int, d: int]}
|
||||||
|
|
||||||
>>> index_control = BankersAlgorithm(
|
>>> index_control = BankersAlgorithm(
|
||||||
... test_claim_vector, test_allocated_res_table, test_maximum_claim_table
|
... test_claim_vector, test_allocated_res_table, test_maximum_claim_table
|
||||||
... )._BankersAlgorithm__need_index_manager()
|
... )._BankersAlgorithm__need_index_manager()
|
||||||
|
@ -100,7 +103,8 @@ class BankersAlgorithm:
|
||||||
def main(self, **kwargs) -> None:
|
def main(self, **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
Utilize various methods in this class to simulate the Banker's algorithm
|
Utilize various methods in this class to simulate the Banker's algorithm
|
||||||
Return: None
|
:Return: None
|
||||||
|
|
||||||
>>> BankersAlgorithm(test_claim_vector, test_allocated_res_table,
|
>>> BankersAlgorithm(test_claim_vector, test_allocated_res_table,
|
||||||
... test_maximum_claim_table).main(describe=True)
|
... test_maximum_claim_table).main(describe=True)
|
||||||
Allocated Resource Table
|
Allocated Resource Table
|
||||||
|
|
|
@ -17,13 +17,15 @@ from collections.abc import Iterable
|
||||||
|
|
||||||
class Clause:
|
class Clause:
|
||||||
"""
|
"""
|
||||||
A clause represented in Conjunctive Normal Form.
|
| A clause represented in Conjunctive Normal Form.
|
||||||
A clause is a set of literals, either complemented or otherwise.
|
| A clause is a set of literals, either complemented or otherwise.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
{A1, A2, A3'} is the clause (A1 v A2 v A3')
|
* {A1, A2, A3'} is the clause (A1 v A2 v A3')
|
||||||
{A5', A2', A1} is the clause (A5' v A2' v A1)
|
* {A5', A2', A1} is the clause (A5' v A2' v A1)
|
||||||
|
|
||||||
Create model
|
Create model
|
||||||
|
|
||||||
>>> clause = Clause(["A1", "A2'", "A3"])
|
>>> clause = Clause(["A1", "A2'", "A3"])
|
||||||
>>> clause.evaluate({"A1": True})
|
>>> clause.evaluate({"A1": True})
|
||||||
True
|
True
|
||||||
|
@ -39,6 +41,7 @@ class Clause:
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
"""
|
"""
|
||||||
To print a clause as in Conjunctive Normal Form.
|
To print a clause as in Conjunctive Normal Form.
|
||||||
|
|
||||||
>>> str(Clause(["A1", "A2'", "A3"]))
|
>>> str(Clause(["A1", "A2'", "A3"]))
|
||||||
"{A1 , A2' , A3}"
|
"{A1 , A2' , A3}"
|
||||||
"""
|
"""
|
||||||
|
@ -47,6 +50,7 @@ class Clause:
|
||||||
def __len__(self) -> int:
|
def __len__(self) -> int:
|
||||||
"""
|
"""
|
||||||
To print a clause as in Conjunctive Normal Form.
|
To print a clause as in Conjunctive Normal Form.
|
||||||
|
|
||||||
>>> len(Clause([]))
|
>>> len(Clause([]))
|
||||||
0
|
0
|
||||||
>>> len(Clause(["A1", "A2'", "A3"]))
|
>>> len(Clause(["A1", "A2'", "A3"]))
|
||||||
|
@ -72,10 +76,12 @@ class Clause:
|
||||||
def evaluate(self, model: dict[str, bool | None]) -> bool | None:
|
def evaluate(self, model: dict[str, bool | None]) -> bool | None:
|
||||||
"""
|
"""
|
||||||
Evaluates the clause with the assignments in model.
|
Evaluates the clause with the assignments in model.
|
||||||
|
|
||||||
This has the following steps:
|
This has the following steps:
|
||||||
1. Return True if both a literal and its complement exist in the clause.
|
1. Return ``True`` if both a literal and its complement exist in the clause.
|
||||||
2. Return True if a single literal has the assignment True.
|
2. Return ``True`` if a single literal has the assignment ``True``.
|
||||||
3. Return None(unable to complete evaluation) if a literal has no assignment.
|
3. Return ``None`` (unable to complete evaluation)
|
||||||
|
if a literal has no assignment.
|
||||||
4. Compute disjunction of all values assigned in clause.
|
4. Compute disjunction of all values assigned in clause.
|
||||||
"""
|
"""
|
||||||
for literal in self.literals:
|
for literal in self.literals:
|
||||||
|
@ -92,10 +98,10 @@ class Clause:
|
||||||
|
|
||||||
class Formula:
|
class Formula:
|
||||||
"""
|
"""
|
||||||
A formula represented in Conjunctive Normal Form.
|
| A formula represented in Conjunctive Normal Form.
|
||||||
A formula is a set of clauses.
|
| A formula is a set of clauses.
|
||||||
For example,
|
| For example,
|
||||||
{{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1))
|
| {{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1))
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, clauses: Iterable[Clause]) -> None:
|
def __init__(self, clauses: Iterable[Clause]) -> None:
|
||||||
|
@ -107,7 +113,8 @@ class Formula:
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
"""
|
"""
|
||||||
To print a formula as in Conjunctive Normal Form.
|
To print a formula as in Conjunctive Normal Form.
|
||||||
str(Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])]))
|
|
||||||
|
>>> str(Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])]))
|
||||||
"{{A1 , A2' , A3} , {A5' , A2' , A1}}"
|
"{{A1 , A2' , A3} , {A5' , A2' , A1}}"
|
||||||
"""
|
"""
|
||||||
return "{" + " , ".join(str(clause) for clause in self.clauses) + "}"
|
return "{" + " , ".join(str(clause) for clause in self.clauses) + "}"
|
||||||
|
@ -115,8 +122,8 @@ class Formula:
|
||||||
|
|
||||||
def generate_clause() -> Clause:
|
def generate_clause() -> Clause:
|
||||||
"""
|
"""
|
||||||
Randomly generate a clause.
|
| Randomly generate a clause.
|
||||||
All literals have the name Ax, where x is an integer from 1 to 5.
|
| All literals have the name Ax, where x is an integer from ``1`` to ``5``.
|
||||||
"""
|
"""
|
||||||
literals = []
|
literals = []
|
||||||
no_of_literals = random.randint(1, 5)
|
no_of_literals = random.randint(1, 5)
|
||||||
|
@ -149,11 +156,12 @@ def generate_formula() -> Formula:
|
||||||
|
|
||||||
def generate_parameters(formula: Formula) -> tuple[list[Clause], list[str]]:
|
def generate_parameters(formula: Formula) -> tuple[list[Clause], list[str]]:
|
||||||
"""
|
"""
|
||||||
Return the clauses and symbols from a formula.
|
| Return the clauses and symbols from a formula.
|
||||||
A symbol is the uncomplemented form of a literal.
|
| A symbol is the uncomplemented form of a literal.
|
||||||
|
|
||||||
For example,
|
For example,
|
||||||
Symbol of A3 is A3.
|
* Symbol of A3 is A3.
|
||||||
Symbol of A5' is A5.
|
* Symbol of A5' is A5.
|
||||||
|
|
||||||
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
|
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
|
||||||
>>> clauses, symbols = generate_parameters(formula)
|
>>> clauses, symbols = generate_parameters(formula)
|
||||||
|
@ -177,21 +185,20 @@ def find_pure_symbols(
|
||||||
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
|
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
|
||||||
) -> tuple[list[str], dict[str, bool | None]]:
|
) -> tuple[list[str], dict[str, bool | None]]:
|
||||||
"""
|
"""
|
||||||
Return pure symbols and their values to satisfy clause.
|
| Return pure symbols and their values to satisfy clause.
|
||||||
Pure symbols are symbols in a formula that exist only
|
| Pure symbols are symbols in a formula that exist only in one form,
|
||||||
in one form, either complemented or otherwise.
|
| either complemented or otherwise.
|
||||||
For example,
|
| For example,
|
||||||
{ { A4 , A3 , A5' , A1 , A3' } , { A4 } , { A3 } } has
|
| {{A4 , A3 , A5' , A1 , A3'} , {A4} , {A3}} has pure symbols A4, A5' and A1.
|
||||||
pure symbols A4, A5' and A1.
|
|
||||||
This has the following steps:
|
This has the following steps:
|
||||||
1. Ignore clauses that have already evaluated to be True.
|
1. Ignore clauses that have already evaluated to be ``True``.
|
||||||
2. Find symbols that occur only in one form in the rest of the clauses.
|
2. Find symbols that occur only in one form in the rest of the clauses.
|
||||||
3. Assign value True or False depending on whether the symbols occurs
|
3. Assign value ``True`` or ``False`` depending on whether the symbols occurs
|
||||||
in normal or complemented form respectively.
|
in normal or complemented form respectively.
|
||||||
|
|
||||||
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
|
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
|
||||||
>>> clauses, symbols = generate_parameters(formula)
|
>>> clauses, symbols = generate_parameters(formula)
|
||||||
|
|
||||||
>>> pure_symbols, values = find_pure_symbols(clauses, symbols, {})
|
>>> pure_symbols, values = find_pure_symbols(clauses, symbols, {})
|
||||||
>>> pure_symbols
|
>>> pure_symbols
|
||||||
['A1', 'A2', 'A3', 'A5']
|
['A1', 'A2', 'A3', 'A5']
|
||||||
|
@ -231,20 +238,21 @@ def find_unit_clauses(
|
||||||
) -> tuple[list[str], dict[str, bool | None]]:
|
) -> tuple[list[str], dict[str, bool | None]]:
|
||||||
"""
|
"""
|
||||||
Returns the unit symbols and their values to satisfy clause.
|
Returns the unit symbols and their values to satisfy clause.
|
||||||
|
|
||||||
Unit symbols are symbols in a formula that are:
|
Unit symbols are symbols in a formula that are:
|
||||||
- Either the only symbol in a clause
|
- Either the only symbol in a clause
|
||||||
- Or all other literals in that clause have been assigned False
|
- Or all other literals in that clause have been assigned ``False``
|
||||||
|
|
||||||
This has the following steps:
|
This has the following steps:
|
||||||
1. Find symbols that are the only occurrences in a clause.
|
1. Find symbols that are the only occurrences in a clause.
|
||||||
2. Find symbols in a clause where all other literals are assigned False.
|
2. Find symbols in a clause where all other literals are assigned ``False``.
|
||||||
3. Assign True or False depending on whether the symbols occurs in
|
3. Assign ``True`` or ``False`` depending on whether the symbols occurs in
|
||||||
normal or complemented form respectively.
|
normal or complemented form respectively.
|
||||||
|
|
||||||
>>> clause1 = Clause(["A4", "A3", "A5'", "A1", "A3'"])
|
>>> clause1 = Clause(["A4", "A3", "A5'", "A1", "A3'"])
|
||||||
>>> clause2 = Clause(["A4"])
|
>>> clause2 = Clause(["A4"])
|
||||||
>>> clause3 = Clause(["A3"])
|
>>> clause3 = Clause(["A3"])
|
||||||
>>> clauses, symbols = generate_parameters(Formula([clause1, clause2, clause3]))
|
>>> clauses, symbols = generate_parameters(Formula([clause1, clause2, clause3]))
|
||||||
|
|
||||||
>>> unit_clauses, values = find_unit_clauses(clauses, {})
|
>>> unit_clauses, values = find_unit_clauses(clauses, {})
|
||||||
>>> unit_clauses
|
>>> unit_clauses
|
||||||
['A4', 'A3']
|
['A4', 'A3']
|
||||||
|
@ -278,16 +286,16 @@ def dpll_algorithm(
|
||||||
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
|
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
|
||||||
) -> tuple[bool | None, dict[str, bool | None] | None]:
|
) -> tuple[bool | None, dict[str, bool | None] | None]:
|
||||||
"""
|
"""
|
||||||
Returns the model if the formula is satisfiable, else None
|
Returns the model if the formula is satisfiable, else ``None``
|
||||||
|
|
||||||
This has the following steps:
|
This has the following steps:
|
||||||
1. If every clause in clauses is True, return True.
|
1. If every clause in clauses is ``True``, return ``True``.
|
||||||
2. If some clause in clauses is False, return False.
|
2. If some clause in clauses is ``False``, return ``False``.
|
||||||
3. Find pure symbols.
|
3. Find pure symbols.
|
||||||
4. Find unit symbols.
|
4. Find unit symbols.
|
||||||
|
|
||||||
>>> formula = Formula([Clause(["A4", "A3", "A5'", "A1", "A3'"]), Clause(["A4"])])
|
>>> formula = Formula([Clause(["A4", "A3", "A5'", "A1", "A3'"]), Clause(["A4"])])
|
||||||
>>> clauses, symbols = generate_parameters(formula)
|
>>> clauses, symbols = generate_parameters(formula)
|
||||||
|
|
||||||
>>> soln, model = dpll_algorithm(clauses, symbols, {})
|
>>> soln, model = dpll_algorithm(clauses, symbols, {})
|
||||||
>>> soln
|
>>> soln
|
||||||
True
|
True
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
"""
|
"""
|
||||||
developed by: markmelnic
|
| developed by: markmelnic
|
||||||
original repo: https://github.com/markmelnic/Scoring-Algorithm
|
| original repo: https://github.com/markmelnic/Scoring-Algorithm
|
||||||
|
|
||||||
Analyse data using a range based percentual proximity algorithm
|
Analyse data using a range based percentual proximity algorithm
|
||||||
and calculate the linear maximum likelihood estimation.
|
and calculate the linear maximum likelihood estimation.
|
||||||
The basic principle is that all values supplied will be broken
|
The basic principle is that all values supplied will be broken
|
||||||
down to a range from 0 to 1 and each column's score will be added
|
down to a range from ``0`` to ``1`` and each column's score will be added
|
||||||
up to get the total score.
|
up to get the total score.
|
||||||
|
|
||||||
==========
|
|
||||||
Example for data of vehicles
|
Example for data of vehicles
|
||||||
|
::
|
||||||
|
|
||||||
price|mileage|registration_year
|
price|mileage|registration_year
|
||||||
20k |60k |2012
|
20k |60k |2012
|
||||||
22k |50k |2011
|
22k |50k |2011
|
||||||
|
@ -19,7 +20,7 @@ price|mileage|registration_year
|
||||||
We want the vehicle with the lowest price,
|
We want the vehicle with the lowest price,
|
||||||
lowest mileage but newest registration year.
|
lowest mileage but newest registration year.
|
||||||
Thus the weights for each column are as follows:
|
Thus the weights for each column are as follows:
|
||||||
[0, 0, 1]
|
``[0, 0, 1]``
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,10 +98,11 @@ def procentual_proximity(
|
||||||
source_data: list[list[float]], weights: list[int]
|
source_data: list[list[float]], weights: list[int]
|
||||||
) -> list[list[float]]:
|
) -> list[list[float]]:
|
||||||
"""
|
"""
|
||||||
weights - int list
|
| `weights` - ``int`` list
|
||||||
possible values - 0 / 1
|
| possible values - ``0`` / ``1``
|
||||||
0 if lower values have higher weight in the data set
|
|
||||||
1 if higher values have higher weight in the data set
|
* ``0`` if lower values have higher weight in the data set
|
||||||
|
* ``1`` if higher values have higher weight in the data set
|
||||||
|
|
||||||
>>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1])
|
>>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1])
|
||||||
[[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]]
|
[[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user