turash/models/transport/transport.go
Damir Mukimov 4a2fda96cd
Initial commit: Repository setup with .gitignore, golangci-lint v2.6.0, and code quality checks
- Initialize git repository
- Add comprehensive .gitignore for Go projects
- Install golangci-lint v2.6.0 (latest v2) globally
- Configure .golangci.yml with appropriate linters and formatters
- Fix all formatting issues (gofmt)
- Fix all errcheck issues (unchecked errors)
- Adjust complexity threshold for validation functions
- All checks passing: build, test, vet, lint
2025-11-01 07:36:22 +01:00

330 lines
12 KiB
Go

package transport
import (
"fmt"
)
// SymbiosisType represents different types of industrial symbiosis exchanges
type SymbiosisType string
const (
// Resource Exchange Symbioses
SymbiosisWasteToResource SymbiosisType = "waste_to_resource"
SymbiosisUtilitySharing SymbiosisType = "utility_sharing"
SymbiosisEnergyCascading SymbiosisType = "energy_cascading"
SymbiosisMaterialRecycling SymbiosisType = "material_recycling"
// Infrastructure Sharing Symbioses
SymbiosisPhysicalInfra SymbiosisType = "physical_infrastructure"
SymbiosisEquipmentSharing SymbiosisType = "equipment_sharing"
SymbiosisLogisticsNetwork SymbiosisType = "logistics_network"
SymbiosisUtilityInfra SymbiosisType = "utility_infrastructure"
// Service-Based Symbioses
SymbiosisKnowledgeSharing SymbiosisType = "knowledge_sharing"
SymbiosisWorkforceSharing SymbiosisType = "workforce_sharing"
SymbiosisMaintenanceSvc SymbiosisType = "maintenance_services"
SymbiosisProcurement SymbiosisType = "procurement_cooperative"
// Digital Symbioses
SymbiosisDataSharing SymbiosisType = "data_sharing"
SymbiosisPlatformCoop SymbiosisType = "platform_cooperative"
SymbiosisIoTNetwork SymbiosisType = "iot_network_sharing"
SymbiosisSoftwareLicenses SymbiosisType = "software_licenses"
)
// ExchangeParams contains parameters for symbiosis exchange cost calculations
type ExchangeParams struct {
DistanceKm float64 `json:"distance_km"` // Distance in kilometers (for physical exchanges)
Value float64 `json:"value"` // Exchange value (€)
Volume float64 `json:"volume"` // Exchange volume (units)
SymbiosisType SymbiosisType `json:"symbiosis_type"` // Type of symbiosis exchange
Complexity string `json:"complexity"` // "low", "medium", "high" implementation complexity
RiskLevel string `json:"risk_level"` // "low", "medium", "high" risk level
OperatingHours int `json:"operating_hours"` // Annual operating hours (default: 8000)
}
// ExchangeCost represents the cost breakdown for enabling a symbiosis exchange
type ExchangeCost struct {
CapitalCost float64 `json:"capital_cost"` // One-time setup cost (€)
AnnualOpexCost float64 `json:"annual_opex_cost"` // Annual operating cost (€)
PlatformFee float64 `json:"platform_fee"` // Platform transaction fee (€)
RegulatoryCost float64 `json:"regulatory_cost"` // Compliance/regulatory cost (€)
RiskMitigationCost float64 `json:"risk_mitigation_cost"` // Insurance/liability cost (€)
TotalAnnualCost float64 `json:"total_annual_cost"` // Total annual cost (€)
CostPerUnit float64 `json:"cost_per_unit"` // Cost per unit exchanged
Feasibility string `json:"feasibility"` // "low", "medium", "high"
ROIYears float64 `json:"roi_years"` // Years to ROI
}
// DefaultExchangeParams returns sensible defaults for exchange calculations
func DefaultExchangeParams() ExchangeParams {
return ExchangeParams{
OperatingHours: 8000, // 8000 hours/year = ~90% uptime
Complexity: "medium",
RiskLevel: "medium",
}
}
// CalculateExchangeCost calculates total costs for enabling a symbiosis exchange
func CalculateExchangeCost(params ExchangeParams) (ExchangeCost, error) {
// Validate parameters first
if err := ValidateExchangeParams(params); err != nil {
return ExchangeCost{}, err
}
// Calculate base costs based on symbiosis type
baseCosts, err := calculateBaseExchangeCosts(params)
if err != nil {
return ExchangeCost{}, err
}
// Add complexity multiplier
complexityMultiplier := getComplexityMultiplier(params.Complexity)
// Add risk mitigation costs
riskCost := calculateRiskMitigationCost(params.Value, params.RiskLevel)
// Platform fee (percentage of transaction value)
platformFee := params.Value * 0.05 // 5% platform fee
// Regulatory compliance cost
regulatoryCost := calculateRegulatoryCost(params.SymbiosisType, params.Value)
// Total annual costs
totalAnnual := (baseCosts.AnnualOpexCost + riskCost + regulatoryCost) * complexityMultiplier
totalCapital := baseCosts.CapitalCost * complexityMultiplier
// Cost per unit exchanged
costPerUnit := 0.0
if params.Volume > 0 {
costPerUnit = totalAnnual / params.Volume
}
// ROI calculation (simple payback period)
annualBenefit := params.Value * 0.15 // Assume 15% annual benefit from exchange
roiYears := totalCapital / (annualBenefit - totalAnnual)
if roiYears < 0 {
roiYears = 999 // Never reaches ROI
}
feasibility := assessExchangeFeasibility(params)
return ExchangeCost{
CapitalCost: totalCapital,
AnnualOpexCost: baseCosts.AnnualOpexCost * complexityMultiplier,
PlatformFee: platformFee,
RegulatoryCost: regulatoryCost,
RiskMitigationCost: riskCost,
TotalAnnualCost: totalAnnual,
CostPerUnit: costPerUnit,
Feasibility: feasibility,
ROIYears: roiYears,
}, nil
}
// calculateBaseExchangeCosts calculates costs based on symbiosis type
func calculateBaseExchangeCosts(params ExchangeParams) (ExchangeCost, error) {
switch params.SymbiosisType {
case SymbiosisWasteToResource, SymbiosisUtilitySharing, SymbiosisEnergyCascading, SymbiosisMaterialRecycling:
return calculateResourceExchangeCosts(params)
case SymbiosisPhysicalInfra, SymbiosisEquipmentSharing, SymbiosisLogisticsNetwork, SymbiosisUtilityInfra:
return calculateInfrastructureSharingCosts(params)
case SymbiosisKnowledgeSharing, SymbiosisWorkforceSharing, SymbiosisMaintenanceSvc, SymbiosisProcurement:
return calculateServiceExchangeCosts(params)
case SymbiosisDataSharing, SymbiosisPlatformCoop, SymbiosisIoTNetwork, SymbiosisSoftwareLicenses:
return calculateDigitalExchangeCosts(params)
default:
return ExchangeCost{}, fmt.Errorf("unsupported symbiosis type: %s", params.SymbiosisType)
}
}
// calculateResourceExchangeCosts handles physical resource exchanges (waste, utilities, energy, materials)
func calculateResourceExchangeCosts(params ExchangeParams) (ExchangeCost, error) {
// Base costs depend on resource type and physical transport needs
var capitalCost, annualOpex float64
switch params.SymbiosisType {
case SymbiosisWasteToResource, SymbiosisMaterialRecycling:
// Material transport costs
capitalCost = 50000.0 + (50.0 * params.DistanceKm) // Collection/processing equipment
annualOpex = params.Volume * 0.10 // €0.10 per unit for handling/transport
case SymbiosisUtilitySharing, SymbiosisEnergyCascading:
// Energy/utility sharing costs
capitalCost = 25000.0 + (25.0 * params.DistanceKm) // Connection infrastructure
annualOpex = params.Value * 0.03 // 3% of exchange value for maintenance
}
return ExchangeCost{
CapitalCost: capitalCost,
AnnualOpexCost: annualOpex,
}, nil
}
// calculateInfrastructureSharingCosts handles shared physical infrastructure
func calculateInfrastructureSharingCosts(params ExchangeParams) (ExchangeCost, error) {
// Infrastructure sharing has lower costs as assets already exist
capitalCost := params.Value * 0.05 // 5% of value for integration/setup
annualOpex := params.Value * 0.02 // 2% of value for coordination/maintenance
return ExchangeCost{
CapitalCost: capitalCost,
AnnualOpexCost: annualOpex,
}, nil
}
// calculateServiceExchangeCosts handles knowledge, workforce, maintenance, procurement exchanges
func calculateServiceExchangeCosts(params ExchangeParams) (ExchangeCost, error) {
// Service exchanges are primarily about coordination and expertise
capitalCost := 10000.0 // Training/coordination setup
annualOpex := params.Value * 0.08 // 8% for ongoing coordination and quality assurance
return ExchangeCost{
CapitalCost: capitalCost,
AnnualOpexCost: annualOpex,
}, nil
}
// calculateDigitalExchangeCosts handles data, IoT, software, platform exchanges
func calculateDigitalExchangeCosts(params ExchangeParams) (ExchangeCost, error) {
// Digital exchanges have minimal physical costs, focus on integration
capitalCost := 5000.0 // API integration, security setup
annualOpex := params.Value * 0.04 // 4% for data management and security
return ExchangeCost{
CapitalCost: capitalCost,
AnnualOpexCost: annualOpex,
}, nil
}
// getComplexityMultiplier returns cost multiplier based on implementation complexity
func getComplexityMultiplier(complexity string) float64 {
switch complexity {
case "low":
return 1.0
case "medium":
return 1.3
case "high":
return 1.8
default:
return 1.3 // Default to medium
}
}
// calculateRiskMitigationCost calculates insurance and liability costs
func calculateRiskMitigationCost(value float64, riskLevel string) float64 {
var baseRate float64
switch riskLevel {
case "low":
baseRate = 0.01 // 1% of value
case "medium":
baseRate = 0.025 // 2.5% of value
case "high":
baseRate = 0.05 // 5% of value
default:
baseRate = 0.025
}
return value * baseRate
}
// calculateRegulatoryCost calculates compliance and regulatory costs
func calculateRegulatoryCost(symbiosisType SymbiosisType, value float64) float64 {
var baseRate float64
// Different regulatory requirements by symbiosis type
switch symbiosisType {
case SymbiosisWasteToResource, SymbiosisMaterialRecycling:
baseRate = 0.03 // 3% - high regulatory burden for waste/material handling
case SymbiosisEnergyCascading, SymbiosisUtilitySharing:
baseRate = 0.02 // 2% - moderate regulatory burden for energy/utilities
case SymbiosisPhysicalInfra, SymbiosisEquipmentSharing:
baseRate = 0.015 // 1.5% - infrastructure sharing regulations
case SymbiosisKnowledgeSharing, SymbiosisWorkforceSharing:
baseRate = 0.01 // 1% - lighter regulations for service exchanges
case SymbiosisDataSharing, SymbiosisIoTNetwork, SymbiosisSoftwareLicenses:
baseRate = 0.0005 // 0.05% - data protection and IP regulations
default:
baseRate = 0.02 // Default 2%
}
return value * baseRate
}
// assessExchangeFeasibility evaluates overall feasibility of the symbiosis exchange
func assessExchangeFeasibility(params ExchangeParams) string {
score := 0.0
// Distance factor (closer = better)
if params.DistanceKm <= 1.0 {
score += 1.0
} else if params.DistanceKm <= 5.0 {
score += 0.7
} else if params.DistanceKm <= 25.0 {
score += 0.4
} else {
score += 0.1
}
// Complexity factor (lower = better)
switch params.Complexity {
case "low":
score += 1.0
case "medium":
score += 0.6
case "high":
score += 0.3
}
// Risk factor (lower = better)
switch params.RiskLevel {
case "low":
score += 1.0
case "medium":
score += 0.6
case "high":
score += 0.3
}
// Symbiosis type factor (some types are naturally easier)
switch params.SymbiosisType {
case SymbiosisDataSharing, SymbiosisSoftwareLicenses:
score += 0.8 // Digital exchanges are easier
case SymbiosisKnowledgeSharing, SymbiosisWorkforceSharing:
score += 0.6 // Service exchanges are moderately easy
case SymbiosisWasteToResource, SymbiosisMaterialRecycling:
score += 0.4 // Physical material exchanges are challenging
default:
score += 0.5
}
// Normalize score to 0-1 and convert to feasibility level
normalizedScore := score / 4.0 // Max possible score is 4.0
if normalizedScore >= 0.7 {
return "high"
} else if normalizedScore >= 0.4 {
return "medium"
}
return "low"
}
// ValidateExchangeParams checks if exchange parameters are valid
func ValidateExchangeParams(params ExchangeParams) error {
if params.Value < 0 {
return fmt.Errorf("exchange value cannot be negative")
}
if params.Volume < 0 {
return fmt.Errorf("exchange volume cannot be negative")
}
if params.DistanceKm < 0 {
return fmt.Errorf("distance cannot be negative")
}
if params.SymbiosisType == "" {
return fmt.Errorf("symbiosis type is required")
}
if params.OperatingHours <= 0 {
params.OperatingHours = 8000 // Default
}
return nil
}