ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
layout_optimizer.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
17#include <vector>
18#include <unordered_map>
19#include <memory>
20#include <functional>
21#include <random>
22
23namespace zlayout {
24namespace optimization {
25
29struct Component {
30 std::string name;
33 std::vector<std::string> input_pins;
34 std::vector<std::string> output_pins;
35
36 // Physical properties
39 bool is_fixed; // Cannot be moved (e.g., I/O pads)
40
41 Component(const std::string& name, const geometry::Rectangle& shape)
43 thermal_coefficient(1.0), is_fixed(false) {}
44};
45
49struct Net {
50 std::string name;
51 std::string driver_component; // Component that drives this net
52 std::string driver_pin; // Pin that drives this net
53 std::vector<std::pair<std::string, std::string>> sinks; // (component, pin) pairs
54
55 double criticality; // Timing criticality (0.0 = non-critical, 1.0 = critical path)
56 double weight; // Optimization weight
57
58 Net(const std::string& name) : name(name), criticality(0.0), weight(1.0) {}
59};
60
65 // Objectives (weights should sum to 1.0)
66 double area_weight = 0.3;
67 double wirelength_weight = 0.4;
68 double timing_weight = 0.2;
69 double power_weight = 0.1;
70
71 // Constraints
72 double max_utilization = 0.8; // Maximum area utilization
73 double min_spacing = 0.15; // Minimum spacing between components
74 double max_aspect_ratio = 2.0; // Maximum chip aspect ratio
75
76 // Algorithm parameters
77 double initial_temperature = 1000.0;
78 double cooling_rate = 0.95;
79 double final_temperature = 0.1;
80 size_t max_iterations = 100000;
81
82 // Multi-level optimization
85};
86
90struct CostResult {
91 double total_cost;
92 double area_cost;
95 double power_cost;
97
98 bool is_feasible() const {
99 return constraint_violations < 1e-6;
100 }
101};
102
110private:
111 std::vector<Component*> components;
112 std::vector<Net> nets;
113 geometry::Rectangle placement_area;
114
115 // Force calculation parameters
116 double spring_constant = 1.0;
117 double repulsion_constant = 1000.0;
118 double damping_factor = 0.9;
119 double time_step = 0.01;
120
121public:
122 ForceDirectedPlacer(const geometry::Rectangle& area) : placement_area(area) {}
123
124 void add_component(Component* comp) { components.push_back(comp); }
125 void add_net(const Net& net) { nets.push_back(net); }
126
130 bool optimize(size_t max_iterations = 1000);
131
132private:
133 geometry::Point calculate_net_force(const Component& comp) const;
134 geometry::Point calculate_repulsion_force(const Component& comp) const;
135 geometry::Point calculate_boundary_force(const Component& comp) const;
136 bool is_converged() const;
137};
138
146private:
147 std::vector<Component> components;
148 std::vector<Net> nets;
149 std::unordered_map<std::string, size_t> component_index;
150
151 geometry::Rectangle placement_area;
152 OptimizationConfig config;
153
154 // State management
155 std::mt19937 rng;
156 double current_temperature;
157 CostResult current_cost;
158 CostResult best_cost;
159 std::vector<geometry::Point> best_positions;
160
161 // Statistics
162 size_t total_moves = 0;
163 size_t accepted_moves = 0;
164 size_t improved_moves = 0;
165
166public:
168 const OptimizationConfig& config = OptimizationConfig{})
169 : placement_area(area), config(config), rng(std::random_device{}()) {}
170
171 void add_component(const Component& comp);
172 void add_net(const Net& net);
173
177 CostResult optimize();
178
182 std::vector<geometry::Point> get_positions() const { return best_positions; }
183
195
197
198private:
199 CostResult evaluate_cost() const;
200 double calculate_wirelength_cost() const;
201 double calculate_timing_cost() const;
202 double calculate_area_cost() const;
203 double calculate_power_cost() const;
204 double calculate_constraint_violations() const;
205
206 bool make_random_move();
207 void accept_move();
208 void reject_move();
209 bool accept_probability(double delta_cost) const;
210
211 geometry::Point generate_random_position() const;
212 bool is_position_valid(size_t comp_idx, const geometry::Point& pos) const;
213};
214
222private:
223 struct IPBlock {
224 std::string name;
225 std::vector<Component> components;
226 std::vector<Net> internal_nets;
227 geometry::Rectangle boundary;
228 geometry::Point position;
229
230 // Block-level optimization result
231 CostResult optimization_result;
232 bool is_optimized = false;
233 };
234
235 std::vector<IPBlock> ip_blocks;
236 std::vector<Net> inter_block_nets;
237 geometry::Rectangle chip_area;
238 OptimizationConfig config;
239
240public:
242 const OptimizationConfig& config = OptimizationConfig{})
243 : chip_area(chip_area), config(config) {}
244
248 void create_ip_block(const std::string& name, const geometry::Rectangle& boundary);
249
253 void add_component_to_block(const std::string& block_name, const Component& comp);
254
258 void add_net(const Net& net);
259
267
271 std::vector<std::pair<Component, geometry::Point>> get_final_layout() const;
272
273private:
274 void optimize_ip_block(IPBlock& block);
275 void optimize_block_placement();
276 void global_refinement();
277
278 std::vector<Net> extract_inter_block_nets() const;
279 bool components_in_same_block(const std::string& comp1, const std::string& comp2) const;
280};
281
289private:
290 std::vector<Component> components;
291 std::vector<Net> nets;
292 geometry::Rectangle placement_area;
293
294public:
295 AnalyticalPlacer(const geometry::Rectangle& area) : placement_area(area) {}
296
297 void add_component(const Component& comp) { components.push_back(comp); }
298 void add_net(const Net& net) { nets.push_back(net); }
299
303 std::vector<geometry::Point> generate_initial_placement();
304
305private:
306 void solve_quadratic_system();
307 void legalize_positions(std::vector<geometry::Point>& positions);
308};
309
316private:
317 std::vector<Component> components;
318 std::vector<Net> nets;
319 std::unordered_map<std::string, double> component_delays;
320 std::unordered_map<std::string, double> net_delays;
321
322public:
323 void add_component(const Component& comp, double delay);
324 void add_net(const Net& net);
325
330
335
336private:
337 double calculate_path_delay(const std::vector<std::string>& path) const;
338 std::vector<std::string> find_critical_path() const;
339 void update_net_weights_by_criticality();
340};
341
346public:
354
355 static std::unique_ptr<SimulatedAnnealingOptimizer> create_sa_optimizer(
356 const geometry::Rectangle& area,
357 const OptimizationConfig& config = OptimizationConfig{});
358
359 static std::unique_ptr<HierarchicalOptimizer> create_hierarchical_optimizer(
360 const geometry::Rectangle& area,
361 const OptimizationConfig& config = OptimizationConfig{});
362
363 static std::unique_ptr<ForceDirectedPlacer> create_force_directed_placer(
364 const geometry::Rectangle& area);
365
369 static AlgorithmType recommend_algorithm(size_t component_count,
370 size_t net_count,
371 bool timing_critical = false);
372};
373
374} // namespace optimization
375} // namespace zlayout
Advanced spatial indexing for ultra-large scale EDA layouts.
2D point with high-precision coordinates and utility methods
Definition point.hpp:23
Axis-aligned rectangle for bounding boxes and simple EDA components.
Definition rectangle.hpp:26
void add_component(const Component &comp)
std::vector< geometry::Point > generate_initial_placement()
Generate initial placement using quadratic optimization.
AnalyticalPlacer(const geometry::Rectangle &area)
ForceDirectedPlacer(const geometry::Rectangle &area)
bool optimize(size_t max_iterations=1000)
Run force-directed placement.
void add_component_to_block(const std::string &block_name, const Component &comp)
Add component to an IP block.
void add_net(const Net &net)
Add net (automatically determines if inter-block or intra-block)
CostResult optimize()
Run hierarchical optimization.
std::vector< std::pair< Component, geometry::Point > > get_final_layout() const
Get final layout result.
void create_ip_block(const std::string &name, const geometry::Rectangle &boundary)
Create an IP block.
HierarchicalOptimizer(const geometry::Rectangle &chip_area, const OptimizationConfig &config=OptimizationConfig{})
static std::unique_ptr< HierarchicalOptimizer > create_hierarchical_optimizer(const geometry::Rectangle &area, const OptimizationConfig &config=OptimizationConfig{})
static std::unique_ptr< SimulatedAnnealingOptimizer > create_sa_optimizer(const geometry::Rectangle &area, const OptimizationConfig &config=OptimizationConfig{})
static std::unique_ptr< ForceDirectedPlacer > create_force_directed_placer(const geometry::Rectangle &area)
static AlgorithmType recommend_algorithm(size_t component_count, size_t net_count, bool timing_critical=false)
Choose optimal algorithm based on problem characteristics.
SimulatedAnnealingOptimizer(const geometry::Rectangle &area, const OptimizationConfig &config=OptimizationConfig{})
std::vector< geometry::Point > get_positions() const
Get optimized component positions.
CostResult optimize()
Run simulated annealing optimization.
void update_timing_criticality()
Calculate critical path and update net criticalities.
CostResult optimize_for_timing()
Timing-driven placement optimization.
void add_component(const Component &comp, double delay)
Main namespace for ZLayout library.
Definition component.hpp:20
2D Point class for geometric calculations
Axis-aligned rectangle class for bounding boxes and simple components.
Circuit component with connectivity information.
std::vector< std::string > output_pins
std::vector< std::string > input_pins
Component(const std::string &name, const geometry::Rectangle &shape)
Net (electrical connection) between components.
std::vector< std::pair< std::string, std::string > > sinks
Net(const std::string &name)
Layout optimization objectives and constraints.