-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
70 lines (61 loc) · 2.52 KB
/
task.py
File metadata and controls
70 lines (61 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from enum import Enum
import random
import time
from typing import List, Any, Optional
class TaskType(Enum):
ADDITION = "addition"
MULTIPLICATION = "multiplication"
SORTING = "sorting"
SEARCHING = "searching"
class Task:
def __init__(self, task_type: TaskType, input_size: int):
self.task_type = task_type
self.input_size = input_size
self.input_data = self._generate_input()
self.assigned_miner = None
self.cost = self._calculate_cost()
self.result = None
self.is_validated = False
self.verifiers = []
self.approvals = 0
def _generate_input(self) -> List[int]:
"""Generate random input data based on task type and size."""
return [random.randint(1, 100) for _ in range(self.input_size)]
def _calculate_cost(self) -> float:
"""
Calculate task cost based on time complexity and input size.
Per thesis Equation 1:
C(t) =
- 0.5n for Addition (linear complexity)
- 1.0n for Multiplication (linear complexity)
- n² for Sorting (quadratic approximation of O(n log n))
- 2.0n for Searching (linear with comparison overhead)
Where n = input_size ∈ [10, 100]
"""
complexity_map = {
TaskType.ADDITION: 0.5 * self.input_size, # 0.5n
TaskType.MULTIPLICATION: 1.0 * self.input_size, # 1.0n
TaskType.SORTING: self.input_size * self.input_size, # n²
TaskType.SEARCHING: 2.0 * self.input_size, # 2.0n
}
return complexity_map[self.task_type]
def execute(self) -> Any:
"""Execute the task based on its type."""
if self.task_type == TaskType.ADDITION:
return sum(self.input_data)
elif self.task_type == TaskType.MULTIPLICATION:
result = 1
for num in self.input_data:
result *= num
return result
elif self.task_type == TaskType.SORTING:
return sorted(self.input_data)
elif self.task_type == TaskType.SEARCHING:
target = random.choice(self.input_data)
return target in self.input_data
def verify_solution(self, solution: Any) -> bool:
"""Verify if the provided solution is correct."""
correct_solution = self.execute()
return solution == correct_solution
def __str__(self) -> str:
return f"Task(type={self.task_type.value}, size={self.input_size}, cost={self.cost})"