Skip to content

Commit

Permalink
alala
Browse files Browse the repository at this point in the history
  • Loading branch information
1999AZZAR committed Nov 12, 2024
0 parents commit 7770004
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raw/*
23 changes: 23 additions & 0 deletions Advance/Fuzzy Logic Control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def fuzzy_logic_control(voltage, current):
# Initialize membership functions for error and change in error
error = current - fuzzy_logic_control.prev_current
delta_error = error - fuzzy_logic_control.prev_error

# Define fuzzy rules and membership function based outputs
if error > 0 and delta_error > 0:
adjustment = 0.1
elif error > 0 and delta_error < 0:
adjustment = 0.05
elif error < 0 and delta_error > 0:
adjustment = -0.05
else:
adjustment = -0.1

# Adjust voltage based on the fuzzy control
voltage += adjustment
fuzzy_logic_control.prev_current = current
fuzzy_logic_control.prev_error = error
return voltage

fuzzy_logic_control.prev_current = 0
fuzzy_logic_control.prev_error = 0
36 changes: 36 additions & 0 deletions Advance/Genetic Algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import random

def genetic_algorithm_mppt(voltage_population, current_population, generations=10, mutation_rate=0.01):
def fitness(voltage, current):
return voltage * current # Calculate power as fitness

# Evolve population over generations
for _ in range(generations):
# Select parents based on fitness
sorted_population = sorted(zip(voltage_population, current_population),
key=lambda vc: fitness(vc[0], vc[1]), reverse=True)
parents = sorted_population[:len(sorted_population)//2]

# Crossover and mutation
next_generation = []
while len(next_generation) < len(voltage_population):
parent1, parent2 = random.choice(parents), random.choice(parents)
child_voltage = (parent1[0] + parent2[0]) / 2
child_current = (parent1[1] + parent2[1]) / 2

# Apply mutation
if random.random() < mutation_rate:
child_voltage += random.uniform(-0.1, 0.1)
child_current += random.uniform(-0.1, 0.1)

next_generation.append((child_voltage, child_current))

voltage_population, current_population = zip(*next_generation)

# Return best result in the final population
best_voltage, _ = max(zip(voltage_population, current_population), key=lambda vc: fitness(vc[0], vc[1]))
return best_voltage

# Initial population (sample values)
voltage_population = [random.uniform(15, 20) for _ in range(10)]
current_population = [random.uniform(1, 5) for _ in range(10)]
10 changes: 10 additions & 0 deletions Advance/Neural Network-Based.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from sklearn.neural_network import MLPRegressor
import numpy as np

# Example pre-trained neural network model (hypothetical)
# Assuming 'model' is a trained model that predicts voltage given temperature and irradiance
def neural_network_mppt(temperature, irradiance):
# Normalize inputs if needed and pass through neural network model
inputs = np.array([[temperature, irradiance]])
voltage_prediction = model.predict(inputs)
return voltage_prediction[0]
29 changes: 29 additions & 0 deletions Advance/Particle Swarm Optimization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random

# Define particle class for PSO
class Particle:
def __init__(self, voltage, power):
self.position = voltage # Position in search space (voltage)
self.best_position = voltage
self.velocity = 0
self.best_power = power

def particle_swarm_optimization(voltage, current, particles, iterations=10):
power = voltage * current
for i in range(iterations):
for particle in particles:
# Calculate new power and compare with best power
new_power = voltage * current
if new_power > particle.best_power:
particle.best_power = new_power
particle.best_position = particle.position

# Update velocity and position
particle.velocity = (0.5 * particle.velocity +
random.uniform(0, 1) * (particle.best_position - particle.position))
particle.position += particle.velocity
voltage = particle.position
return voltage

# Initialize particles for PSO
particles = [Particle(voltage=17, power=voltage * current) for _ in range(5)]
10 changes: 10 additions & 0 deletions Advance/Sliding Mode Control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def sliding_mode_control(voltage, current, reference_voltage, step_size=0.05):
error = reference_voltage - voltage
sliding_surface = error * current

# Adjust step size based on sliding surface
if sliding_surface > 0:
voltage += step_size
else:
voltage -= step_size
return voltage
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
from sklearn.neural_network import MLPRegressor

# Assume we have a trained ANN model, here is a placeholder MLP
# Input data: irradiance and temperature
# Output data: estimated voltage near MPP

def train_ann_model():
# This is a simple, pre-trained example
# Train with sample data (in real use, train with actual irradiance/temp data)
X_train = np.array([[800, 25], [1000, 30], [900, 20]]) # (irradiance, temperature)
y_train = np.array([15.5, 17.0, 16.2]) # MPP voltage estimates
ann = MLPRegressor(hidden_layer_sizes=(10, 10), max_iter=1000)
ann.fit(X_train, y_train)
return ann

# Predict voltage near MPP based on current conditions
def ann_predict_voltage(ann_model, irradiance, temperature):
return ann_model.predict([[irradiance, temperature]])[0]

def perturb_observe(voltage, current, prev_voltage, prev_power):
power = voltage * current
delta_power = power - prev_power

if delta_power > 0:
voltage += 0.05
else:
voltage -= 0.05

prev_voltage = voltage
prev_power = power

return voltage, prev_voltage, prev_power

def hybrid_ann_po(ann_model, irradiance, temperature, voltage, current, prev_voltage, prev_power):
# Use ANN to get the starting voltage near MPP
ann_voltage = ann_predict_voltage(ann_model, irradiance, temperature)

# Apply P&O starting from the ANN-estimated voltage
voltage = ann_voltage
voltage, prev_voltage, prev_power = perturb_observe(voltage, current, prev_voltage, prev_power)

return voltage, prev_voltage, prev_power

# Train the ANN model (assuming data for demo purposes)
ann_model = train_ann_model()

# Example environmental conditions
irradiance = 900
temperature = 25
current = 2

# Run hybrid MPPT control
voltage, prev_voltage, prev_power = hybrid_ann_po(ann_model, irradiance, temperature, 17, current, 17, 17 * current)
print(f"Adjusted voltage for MPPT: {voltage}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def perturb_observe(voltage, current, prev_voltage, prev_power):
# Calculate current power
power = voltage * current
delta_power = power - prev_power

# Basic P&O logic to perturb voltage
if delta_power > 0:
voltage += 0.1 # Small increase
else:
voltage -= 0.1 # Small decrease

# Update previous values
prev_voltage = voltage
prev_power = power

return voltage, prev_voltage, prev_power

def incremental_conductance(voltage, current, prev_voltage, prev_current):
# Calculate incremental conductance values
delta_I = current - prev_current
delta_V = voltage - prev_voltage

# Apply Incremental Conductance logic
if delta_V != 0 and delta_I / delta_V == -current / voltage:
adjustment = 0 # Already at MPP
elif delta_V == 0 or delta_I / delta_V > -current / voltage:
adjustment = 0.1 # Move right on the power curve
else:
adjustment = -0.1 # Move left on the power curve

# Update previous values
prev_voltage = voltage
prev_current = current

return voltage + adjustment, prev_voltage, prev_current

def hybrid_po_inc(voltage, current, prev_voltage, prev_power, prev_current, threshold=0.01):
# Use P&O to reach near MPP
voltage, prev_voltage, prev_power = perturb_observe(voltage, current, prev_voltage, prev_power)

# Switch to Incremental Conductance for fine adjustment near MPP
if abs(prev_power - voltage * current) < threshold:
voltage, prev_voltage, prev_current = incremental_conductance(voltage, current, prev_voltage, prev_current)

return voltage, prev_voltage, prev_power, prev_current

# Initialize previous values
voltage = 17
current = 2
prev_voltage = voltage
prev_power = voltage * current
prev_current = current

# Run hybrid MPPT control
voltage, prev_voltage, prev_power, prev_current = hybrid_po_inc(voltage, current, prev_voltage, prev_power, prev_current)
print(f"Adjusted voltage for MPPT: {voltage}")
73 changes: 73 additions & 0 deletions Advance/hybrid/hybrid PSO-FLC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import random

class Particle:
def __init__(self, voltage, power):
self.position = voltage
self.best_position = voltage
self.velocity = 0
self.best_power = power

def fuzzy_logic_adjustment(current, prev_current, prev_error):
# Calculate error and change in error
error = current - prev_current
delta_error = error - prev_error

# Define simple fuzzy rules for adjustment based on error trends
if error > 0 and delta_error > 0:
adjustment = 0.1 # Increase voltage slightly
elif error > 0 and delta_error < 0:
adjustment = 0.05 # Smaller increase
elif error < 0 and delta_error > 0:
adjustment = -0.05 # Small decrease
else:
adjustment = -0.1 # Larger decrease

return adjustment, error

def hybrid_pso_fuzzy_logic(voltage, current, particles, prev_voltage, prev_current, prev_error, iterations=10):
# Initialize power and best power variables
best_global_voltage = voltage
best_global_power = voltage * current
power = best_global_power

# Particle Swarm Optimization phase
for i in range(iterations):
for particle in particles:
# Calculate current power for each particle
particle_power = particle.position * current
if particle_power > particle.best_power:
particle.best_power = particle_power
particle.best_position = particle.position

# Update global best position if needed
if particle.best_power > best_global_power:
best_global_power = particle.best_power
best_global_voltage = particle.best_position

# Update velocity and position for each particle
particle.velocity = (0.5 * particle.velocity +
random.uniform(0, 1) * (particle.best_position - particle.position) +
random.uniform(0, 1) * (best_global_voltage - particle.position))
particle.position += particle.velocity

# Use PSO result as initial position for Fuzzy Logic fine-tuning
adjusted_voltage = best_global_voltage
fuzzy_adjustment, new_error = fuzzy_logic_adjustment(current, prev_current, prev_error)
adjusted_voltage += fuzzy_adjustment

# Update previous values for the next cycle
prev_voltage = adjusted_voltage
prev_current = current
prev_error = new_error

return adjusted_voltage, prev_voltage, prev_current, prev_error

# Initialize particles for PSO phase
particles = [Particle(voltage=17, power=17 * 2) for _ in range(5)]
prev_voltage = 17 # Initial voltage setting
prev_current = 2 # Initial current measurement
prev_error = 0 # Initial error setting

# Run hybrid MPPT control
voltage, prev_voltage, prev_current, prev_error = hybrid_pso_fuzzy_logic(prev_voltage, prev_current, particles, prev_voltage, prev_current, prev_error)
print(f"Adjusted voltage for MPPT: {voltage}")
24 changes: 24 additions & 0 deletions Direct/Incremental Conductance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def incremental_conductance(voltage, current, step_size=0.1):
dV = voltage - incremental_conductance.prev_voltage
dI = current - incremental_conductance.prev_current

if dV == 0:
return voltage # No change in voltage, return the current voltage

# Incremental conductance
dP_dV = current / voltage
inc_conductance = dI / dV

if abs(dP_dV - inc_conductance) < 1e-3:
return voltage
elif dP_dV > inc_conductance:
voltage += step_size
else:
voltage -= step_size

incremental_conductance.prev_voltage = voltage
incremental_conductance.prev_current = current
return voltage

incremental_conductance.prev_voltage = 0
incremental_conductance.prev_current = 0
22 changes: 22 additions & 0 deletions Direct/Peturb and Observe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def perturb_and_observe(voltage, current, step_size=0.1):
# Power calculation
power = voltage * current

# Change in power and voltage for next iteration
delta_power = power - perturb_and_observe.prev_power
delta_voltage = voltage - perturb_and_observe.prev_voltage

# Adjust step size based on direction
if delta_power > 0:
voltage += step_size if delta_voltage > 0 else -step_size
else:
voltage -= step_size if delta_voltage > 0 else -step_size

# Update previous values
perturb_and_observe.prev_power = power
perturb_and_observe.prev_voltage = voltage

return voltage

perturb_and_observe.prev_power = 0
perturb_and_observe.prev_voltage = 0
13 changes: 13 additions & 0 deletions Direct/Ripple Correlation Control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def ripple_correlation_control(voltage, current, duty_cycle, step_size=0.1):
power = voltage * current
delta_power = power - ripple_correlation_control.prev_power

if delta_power > 0:
duty_cycle += step_size
else:
duty_cycle -= step_size

ripple_correlation_control.prev_power = power
return duty_cycle

ripple_correlation_control.prev_power = 0
25 changes: 25 additions & 0 deletions Hybrid/IC with Adaptive Step Adjustment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def hybrid_ic_adaptive(voltage, current, step_size=0.05, min_step=0.01):
dV = voltage - hybrid_ic_adaptive.prev_voltage
dI = current - hybrid_ic_adaptive.prev_current

if dV == 0:
return voltage # No change in voltage

dP_dV = current / voltage
inc_conductance = dI / dV

if abs(dP_dV - inc_conductance) < 1e-3:
return voltage # MPP is reached
elif dP_dV > inc_conductance:
step_size = max(step_size / 2, min_step)
voltage += step_size
else:
step_size = max(step_size / 2, min_step)
voltage -= step_size

hybrid_ic_adaptive.prev_voltage = voltage
hybrid_ic_adaptive.prev_current = current
return voltage

hybrid_ic_adaptive.prev_voltage = 0
hybrid_ic_adaptive.prev_current = 0
Loading

0 comments on commit 7770004

Please sign in to comment.