Skip to content

Commit

Permalink
Fix sphinx/build_docs warnings for other (#12482)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
MaximSmolskiy and pre-commit-ci[bot] authored Dec 29, 2024
1 parent ce036db commit 3622e94
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 63 deletions.
16 changes: 10 additions & 6 deletions other/bankers_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
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,
before deciding whether allocation should be allowed to continue.
[Source] Wikipedia
[Credit] Rosetta Code C implementation helped very much.
(https://rosettacode.org/wiki/Banker%27s_algorithm)
| [Source] Wikipedia
| [Credit] Rosetta Code C implementation helped very much.
| (https://rosettacode.org/wiki/Banker%27s_algorithm)
"""

from __future__ import annotations
Expand Down Expand Up @@ -75,7 +76,7 @@ def __available_resources(self) -> list[int]:
def __need(self) -> list[list[int]]:
"""
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 [
list(np.array(self.__maximum_claim_table[i]) - np.array(allocated_resource))
Expand All @@ -86,7 +87,9 @@ def __need_index_manager(self) -> dict[int, list[int]]:
"""
This function builds an index control dictionary to track original ids/indices
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(
... test_claim_vector, test_allocated_res_table, test_maximum_claim_table
... )._BankersAlgorithm__need_index_manager()
Expand All @@ -100,7 +103,8 @@ def __need_index_manager(self) -> dict[int, list[int]]:
def main(self, **kwargs) -> None:
"""
Utilize various methods in this class to simulate the Banker's algorithm
Return: None
:Return: None
>>> BankersAlgorithm(test_claim_vector, test_allocated_res_table,
... test_maximum_claim_table).main(describe=True)
Allocated Resource Table
Expand Down
94 changes: 51 additions & 43 deletions other/davis_putnam_logemann_loveland.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

class Clause:
"""
A clause represented in Conjunctive Normal Form.
A clause is a set of literals, either complemented or otherwise.
| A clause represented in Conjunctive Normal Form.
| A clause is a set of literals, either complemented or otherwise.
For example:
{A1, A2, A3'} is the clause (A1 v A2 v A3')
{A5', A2', A1} is the clause (A5' v A2' v A1)
* {A1, A2, A3'} is the clause (A1 v A2 v A3')
* {A5', A2', A1} is the clause (A5' v A2' v A1)
Create model
>>> clause = Clause(["A1", "A2'", "A3"])
>>> clause.evaluate({"A1": True})
True
Expand All @@ -39,6 +41,7 @@ def __init__(self, literals: list[str]) -> None:
def __str__(self) -> str:
"""
To print a clause as in Conjunctive Normal Form.
>>> str(Clause(["A1", "A2'", "A3"]))
"{A1 , A2' , A3}"
"""
Expand All @@ -47,6 +50,7 @@ def __str__(self) -> str:
def __len__(self) -> int:
"""
To print a clause as in Conjunctive Normal Form.
>>> len(Clause([]))
0
>>> len(Clause(["A1", "A2'", "A3"]))
Expand All @@ -72,11 +76,13 @@ def assign(self, model: dict[str, bool | None]) -> None:
def evaluate(self, model: dict[str, bool | None]) -> bool | None:
"""
Evaluates the clause with the assignments in model.
This has the following steps:
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.
3. Return None(unable to complete evaluation) if a literal has no assignment.
4. Compute disjunction of all values assigned in 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``.
3. Return ``None`` (unable to complete evaluation)
if a literal has no assignment.
4. Compute disjunction of all values assigned in clause.
"""
for literal in self.literals:
symbol = literal.rstrip("'") if literal.endswith("'") else literal + "'"
Expand All @@ -92,10 +98,10 @@ def evaluate(self, model: dict[str, bool | None]) -> bool | None:

class Formula:
"""
A formula represented in Conjunctive Normal Form.
A formula is a set of clauses.
For example,
{{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1))
| A formula represented in Conjunctive Normal Form.
| A formula is a set of clauses.
| For example,
| {{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:
Expand All @@ -107,16 +113,17 @@ def __init__(self, clauses: Iterable[Clause]) -> None:
def __str__(self) -> str:
"""
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}}"
"""
return "{" + " , ".join(str(clause) for clause in self.clauses) + "}"


def generate_clause() -> Clause:
"""
Randomly generate a clause.
All literals have the name Ax, where x is an integer from 1 to 5.
| Randomly generate a clause.
| All literals have the name Ax, where x is an integer from ``1`` to ``5``.
"""
literals = []
no_of_literals = random.randint(1, 5)
Expand Down Expand Up @@ -149,11 +156,12 @@ def generate_formula() -> Formula:

def generate_parameters(formula: Formula) -> tuple[list[Clause], list[str]]:
"""
Return the clauses and symbols from a formula.
A symbol is the uncomplemented form of a literal.
| Return the clauses and symbols from a formula.
| A symbol is the uncomplemented form of a literal.
For example,
Symbol of A3 is A3.
Symbol of A5' is A5.
* Symbol of A3 is A3.
* Symbol of A5' is A5.
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
>>> clauses, symbols = generate_parameters(formula)
Expand All @@ -177,21 +185,20 @@ def find_pure_symbols(
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
) -> tuple[list[str], dict[str, bool | None]]:
"""
Return pure symbols and their values to satisfy clause.
Pure symbols are symbols in a formula that exist only
in one form, either complemented or otherwise.
For example,
{ { A4 , A3 , A5' , A1 , A3' } , { A4 } , { A3 } } has
pure symbols A4, A5' and A1.
| Return pure symbols and their values to satisfy clause.
| Pure symbols are symbols in a formula that exist only in one form,
| either complemented or otherwise.
| For example,
| {{A4 , A3 , A5' , A1 , A3'} , {A4} , {A3}} has pure symbols A4, A5' and A1.
This has the following steps:
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.
3. Assign value True or False depending on whether the symbols occurs
in normal or complemented form respectively.
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.
3. Assign value ``True`` or ``False`` depending on whether the symbols occurs
in normal or complemented form respectively.
>>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])
>>> clauses, symbols = generate_parameters(formula)
>>> pure_symbols, values = find_pure_symbols(clauses, symbols, {})
>>> pure_symbols
['A1', 'A2', 'A3', 'A5']
Expand Down Expand Up @@ -231,20 +238,21 @@ def find_unit_clauses(
) -> tuple[list[str], dict[str, bool | None]]:
"""
Returns the unit symbols and their values to satisfy clause.
Unit symbols are symbols in a formula that are:
- Either the only symbol in a clause
- Or all other literals in that clause have been assigned False
- Either the only symbol in a clause
- Or all other literals in that clause have been assigned ``False``
This has the following steps:
1. Find symbols that are the only occurrences in a clause.
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
normal or complemented form respectively.
1. Find symbols that are the only occurrences in a clause.
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
normal or complemented form respectively.
>>> clause1 = Clause(["A4", "A3", "A5'", "A1", "A3'"])
>>> clause2 = Clause(["A4"])
>>> clause3 = Clause(["A3"])
>>> clauses, symbols = generate_parameters(Formula([clause1, clause2, clause3]))
>>> unit_clauses, values = find_unit_clauses(clauses, {})
>>> unit_clauses
['A4', 'A3']
Expand Down Expand Up @@ -278,16 +286,16 @@ def dpll_algorithm(
clauses: list[Clause], symbols: list[str], model: dict[str, bool | 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:
1. If every clause in clauses is True, return True.
2. If some clause in clauses is False, return False.
3. Find pure symbols.
4. Find unit symbols.
1. If every clause in clauses is ``True``, return ``True``.
2. If some clause in clauses is ``False``, return ``False``.
3. Find pure symbols.
4. Find unit symbols.
>>> formula = Formula([Clause(["A4", "A3", "A5'", "A1", "A3'"]), Clause(["A4"])])
>>> clauses, symbols = generate_parameters(formula)
>>> soln, model = dpll_algorithm(clauses, symbols, {})
>>> soln
True
Expand Down
30 changes: 16 additions & 14 deletions other/scoring_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
"""
developed by: markmelnic
original repo: https://github.com/markmelnic/Scoring-Algorithm
| developed by: markmelnic
| original repo: https://github.com/markmelnic/Scoring-Algorithm
Analyse data using a range based percentual proximity algorithm
and calculate the linear maximum likelihood estimation.
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.
==========
Example for data of vehicles
price|mileage|registration_year
20k |60k |2012
22k |50k |2011
23k |90k |2015
16k |210k |2010
::
price|mileage|registration_year
20k |60k |2012
22k |50k |2011
23k |90k |2015
16k |210k |2010
We want the vehicle with the lowest price,
lowest mileage but newest registration year.
Thus the weights for each column are as follows:
[0, 0, 1]
``[0, 0, 1]``
"""


Expand Down Expand Up @@ -97,10 +98,11 @@ def procentual_proximity(
source_data: list[list[float]], weights: list[int]
) -> list[list[float]]:
"""
weights - int list
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
| `weights` - ``int`` list
| 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
>>> 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]]
Expand Down

0 comments on commit 3622e94

Please sign in to comment.