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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
---
sidebar_position: 5
---

# Chapter 4: Advanced AI-Robot Brain Techniques

## Learning Objectives

- Understand advanced AI techniques for humanoid robot perception and decision making
- Learn about reinforcement learning for humanoid locomotion
- Explore deep learning integration with Isaac ROS perception
- Implement adaptive control systems using AI
- Understand neural architecture search for robotics applications

## Introduction to Advanced AI Techniques

Humanoid robots require sophisticated AI systems to perceive, reason, and act in complex environments. This chapter explores advanced techniques that go beyond basic perception and navigation, focusing on systems that can learn, adapt, and make complex decisions.

### Key Areas of Advanced AI for Humanoid Robotics

1. **Learning-based Locomotion**: Using AI to develop adaptive walking patterns
2. **Perception-Action Integration**: Deep learning systems that connect perception to action
3. **Adaptive Control**: AI systems that adjust control parameters in real-time
4. **Hierarchical Decision Making**: Multi-level AI systems for complex tasks
5. **Sim-to-Real Transfer**: Techniques to transfer learned behaviors from simulation to real robots

## Reinforcement Learning for Humanoid Locomotion

Reinforcement Learning (RL) has shown remarkable success in developing robust humanoid locomotion controllers. Unlike traditional control methods, RL can learn complex gait patterns and adapt to various terrains.

### Deep Reinforcement Learning Framework

```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from collections import deque
import random

class ActorNetwork(nn.Module):
    """Actor network for humanoid control policy"""
    def __init__(self, state_dim, action_dim, hidden_dim=256):
        super(ActorNetwork, self).__init__()
        
        self.network = nn.Sequential(
            nn.Linear(state_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, action_dim),
            nn.Tanh()  # Actions are clamped to [-1, 1]
        )
    
    def forward(self, state):
        return self.network(state)

class CriticNetwork(nn.Module):
    """Critic network for value estimation"""
    def __init__(self, state_dim, action_dim, hidden_dim=256):
        super(CriticNetwork, self).__init__()
        
        self.network = nn.Sequential(
            nn.Linear(state_dim + action_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, 1)
        )
    
    def forward(self, state, action):
        x = torch.cat([state, action], dim=1)
        return self.network(x)

class HumanoidRLAgent:
    def __init__(self, state_dim, action_dim, lr=3e-4, gamma=0.99, tau=0.005):
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        
        # Networks
        self.actor = ActorNetwork(state_dim, action_dim).to(self.device)
        self.critic = CriticNetwork(state_dim, action_dim).to(self.device)
        self.target_actor = ActorNetwork(state_dim, action_dim).to(self.device)
        self.target_critic = CriticNetwork(state_dim, action_dim).to(self.device)
        
        # Optimizers
        self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=lr)
        self.critic_optimizer = optim.Adam(self.critic.parameters(), lr=lr)
        
        # Hyperparameters
        self.gamma = gamma  # Discount factor
        self.tau = tau      # Soft update parameter
        self.action_dim = action_dim
        
        # Initialize target networks
        self.hard_update(self.target_actor, self.actor)
        self.hard_update(self.target_critic, self.critic)
    
    def hard_update(self, target, source):
        """Hard update target network with source parameters"""
        for target_param, param in zip(target.parameters(), source.parameters()):
            target_param.data.copy_(param.data)
    
    def soft_update(self, target, source):
        """Soft update target network with source parameters"""
        for target_param, param in zip(target.parameters(), source.parameters()):
            target_param.data.copy_(self.tau * param.data + (1.0 - self.tau) * target_param.data)
    
    def select_action(self, state, add_noise=False, noise_scale=0.1):
        """Select action with exploration noise"""
        state_tensor = torch.FloatTensor(state).unsqueeze(0).to(self.device)
        
        with torch.no_grad():
            action = self.actor(state_tensor)
        
        if add_noise:
            noise = torch.randn_like(action) * noise_scale
            action = torch.clamp(action + noise, -1, 1)
        
        return action.cpu().numpy()[0]
    
    def update(self, replay_buffer, batch_size=100):
        """Update networks with experiences from replay buffer"""
        if len(replay_buffer) < batch_size:
            return
        
        # Sample batch
        states, actions, rewards, next_states, dones = replay_buffer.sample(batch_size)
        
        states = torch.FloatTensor(states).to(self.device)
        actions = torch.FloatTensor(actions).to(self.device)
        rewards = torch.FloatTensor(rewards).unsqueeze(1).to(self.device)
        next_states = torch.FloatTensor(next_states).to(self.device)
        dones = torch.BoolTensor(dones).unsqueeze(1).to(self.device)
        
        # Update critic
        with torch.no_grad():
            next_actions = self.target_actor(next_states)
            next_q_values = self.target_critic(next_states, next_actions)
            target_q_values = rewards + (self.gamma * next_q_values * ~dones)
        
        current_q_values = self.critic(states, actions)
        critic_loss = nn.MSELoss()(current_q_values, target_q_values)
        
        self.critic_optimizer.zero_grad()
        critic_loss.backward()
        self.critic_optimizer.step()
        
        # Update actor
        predicted_actions = self.actor(states)
        actor_loss = -self.critic(states, predicted_actions).mean()
        
        self.actor_optimizer.zero_grad()
        actor_loss.backward()
        self.actor_optimizer.step()
        
        # Soft update target networks
        self.soft_update(self.target_actor, self.actor)
        self.soft_update(self.target_critic, self.critic)

class ReplayBuffer:
    """Experience replay buffer for RL training"""
    def __init__(self, capacity=1000000):
        self.buffer = deque(maxlen=capacity)
    
    def push(self, state, action, reward, next_state, done):
        """Add experience to buffer"""
        self.buffer.append((state, action, reward, next_state, done))
    
    def sample(self, batch_size):
        """Sample batch from buffer"""
        batch = random.sample(self.buffer, batch_size)
        state, action, reward, next_state, done = map(np.stack, zip(*batch))
        return state, action, reward, next_state, done
    
    def __len__(self):
        return len(self.buffer)
```

### Humanoid Environment for RL Training

```python
import gym
from gym import spaces
import numpy as np

class HumanoidLocomotionEnv(gym.Env):
    """Custom environment for humanoid locomotion training"""
    def __init__(self):
        super(HumanoidLocomotionEnv, self).__init__()
        
        # Define action and observation spaces
        # This is a simplified example - real environments would have more complex spaces
        self.action_space = spaces.Box(
            low=-1.0, high=1.0, shape=(19,), dtype=np.float32  # 19 joints
        )
        
        # Observation space: joint positions, velocities, IMU readings
        obs_dim = 48  # Example dimension
        self.observation_space = spaces.Box(
            low=-np.inf, high=np.inf, shape=(obs_dim,), dtype=np.float32
        )
        
        # Humanoid robot interface (simplified)
        self.robot = None  # Would interface with Gazebo, PyBullet, etc.
        
        # Episode parameters
        self.max_episode_steps = 1000
        self.current_step = 0
        self.target_velocity = 0.5  # m/s
        
    def reset(self):
        """Reset the environment"""
        # Reset robot to initial pose
        self._reset_robot()
        self.current_step = 0
        
        # Return initial observation
        return self._get_observation()
    
    def step(self, action):
        """Execute one step with given action"""
        # Apply action to robot
        self._apply_action(action)
        
        # Step simulation
        self._step_simulation()
        
        # Get new observation
        observation = self._get_observation()
        
        # Calculate reward
        reward = self._calculate_reward()
        
        # Check termination
        done = self._is_done()
        info = {}
        
        self.current_step += 1
        
        return observation, reward, done, info
    
    def _get_observation(self):
        """Get current observation from robot"""
        # This would interface with robot's sensors
        # Example observations: joint angles, velocities, IMU data, etc.
        observation = np.zeros(48, dtype=np.float32)  # Simplified
        return observation
    
    def _calculate_reward(self):
        """Calculate reward based on current state"""
        # Reward for forward velocity
        forward_vel_reward = self._get_forward_velocity() * 0.1
        
        # Penalty for energy consumption
        energy_penalty = self._get_energy_consumption() * 0.01
        
        # Reward for maintaining balance
        balance_reward = self._get_balance_score() * 0.5
        
        # Penalty for joint limits violations
        joint_limit_penalty = self._get_joint_limit_violations() * 1.0
        
        total_reward = forward_vel_reward - energy_penalty + balance_reward - joint_limit_penalty
        return max(total_reward, -10.0)  # Clamp reward
    
    def _get_forward_velocity(self):
        """Get forward velocity of the robot"""
        # Would interface with robot's odometry
        return 0.0  # Simplified
    
    def _get_energy_consumption(self):
        """Get energy consumption"""
        # Would calculate based on joint torques and velocities
        return 0.0  # Simplified
    
    def _get_balance_score(self):
        """Get balance score (higher is better)"""
        # Calculate based on COM position, IMU readings, etc.
        return 0.0  # Simplified
    
    def _get_joint_limit_violations(self):
        """Count joint limit violations"""
        # Check current joint positions against limits
        return 0.0  # Simplified
    
    def _is_done(self):
        """Check if episode is done"""
        # Done if fallen, exceeded max steps, or other failure conditions
        return self.current_step >= self.max_episode_steps
    
    def _apply_action(self, action):
        """Apply action to robot"""
        # Convert normalized action to joint commands
        # This would interface with robot controller
        pass
    
    def _step_simulation(self):
        """Step the physics simulation"""
        # This would interface with physics engine
        pass
    
    def _reset_robot(self):
        """Reset robot to initial configuration"""
        # Reset robot pose, velocities, etc.
        pass
```

### Training Loop for Humanoid Locomotion

```python
def train_humanoid_locomotion():
    """Training loop for humanoid locomotion policy"""
    env = HumanoidLocomotionEnv()
    
    # Initialize agent
    state_dim = env.observation_space.shape[0]
    action_dim = env.action_space.shape[0]
    agent = HumanoidRLAgent(state_dim, action_dim)
    
    # Initialize replay buffer
    replay_buffer = ReplayBuffer(capacity=1000000)
    
    # Training parameters
    num_episodes = 2000
    max_steps_per_episode = 1000
    batch_size = 256
    update_every = 50
    
    scores = []
    avg_scores = []
    
    for episode in range(num_episodes):
        state = env.reset()
        episode_reward = 0
        episode_steps = 0
        
        for step in range(max_steps_per_episode):
            # Select action with exploration
            action = agent.select_action(state, add_noise=True, noise_scale=0.1)
            
            # Take action
            next_state, reward, done, info = env.step(action)
            
            # Store experience
            replay_buffer.push(state, action, reward, next_state, done)
            
            # Update agent
            if len(replay_buffer) > batch_size and step % update_every == 0:
                agent.update(replay_buffer, batch_size)
            
            state = next_state
            episode_reward += reward
            episode_steps += 1
            
            if done:
                break
        
        scores.append(episode_reward)
        
        # Calculate average score over last 100 episodes
        if len(scores) >= 100:
            avg_score = sum(scores[-100:]) / 100
            avg_scores.append(avg_score)
        else:
            avg_scores.append(sum(scores) / len(scores))
        
        print(f"Episode {episode}, Score: {episode_reward:.2f}, "
              f"Avg Score: {avg_scores[-1]:.2f}")
    
    # Save trained model
    torch.save(agent.actor.state_dict(), "humanoid_locomotion_actor.pth")
    torch.save(agent.critic.state_dict(), "humanoid_locomotion_critic.pth")
    
    return agent, scores, avg_scores
```

## Isaac ROS Deep Learning Integration

Isaac ROS provides GPU-accelerated deep learning capabilities that can be integrated with humanoid control systems:

### Perception-Action Integration

```python
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image, CompressedImage, Imu
from geometry_msgs.msg import Twist
import torch
import torchvision.transforms as T
from PIL import Image as PILImage
import io
import cv2

class PerceptionActionNode(Node):
    def __init__(self):
        super().__init__('perception_action_node')
        
        # Subscriptions
        self.image_sub = self.create_subscription(
            Image, '/camera/color/image_raw', self.image_callback, 10)
        self.depth_sub = self.create_subscription(
            Image, '/camera/depth/image_rect_raw', self.depth_callback, 10)
        self.imu_sub = self.create_subscription(
            Imu, '/imu/data', self.imu_callback, 10)
        
        # Publisher for action commands
        self.cmd_vel_pub = self.create_publisher(Twist, '/cmd_vel', 10)
        
        # Load pretrained models
        self.perception_model = self.load_perception_model()
        self.action_model = self.load_action_model()
        
        # Transformation for images
        self.transform = T.Compose([
            T.Resize((224, 224)),
            T.ToTensor(),
            T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        
        # State buffers
        self.current_img = None
        self.current_depth = None
        self.current_imu = None
        
        self.get_logger().info('Perception-Action Node initialized')
    
    def load_perception_model(self):
        """Load pretrained perception model"""
        # In practice, this would load a model from Isaac ROS or other source
        # For example, an object detection or segmentation model
        import torchvision.models as models
        model = models.resnet18(pretrained=True)
        model.eval()
        return model
    
    def load_action_model(self):
        """Load action selection model"""
        # This would be a model that maps perceptions to actions
        # Could be the RL policy trained above
        import torch.nn as nn
        
        class ActionModel(nn.Module):
            def __init__(self, perception_features_dim, action_dim):
                super(ActionModel, self).__init__()
                self.fc1 = nn.Linear(perception_features_dim, 256)
                self.relu = nn.ReLU()
                self.fc2 = nn.Linear(256, 128)
                self.action_head = nn.Linear(128, action_dim)
            
            def forward(self, perception_features):
                x = self.relu(self.fc1(perception_features))
                x = self.relu(self.fc2(x))
                action = torch.tanh(self.action_head(x))
                return action
        
        model = ActionModel(512, 2)  # 512 features, 2D action (vx, wz)
        model.eval()
        return model
    
    def image_callback(self, msg):
        """Process incoming image"""
        try:
            # Convert ROS Image to PIL Image
            img_data = np.frombuffer(msg.data, dtype=np.uint8).reshape(
                msg.height, msg.width, -1)
            pil_img = PILImage.fromarray(img_data)
            
            # Process image with perception model
            with torch.no_grad():
                transformed_img = self.transform(pil_img).unsqueeze(0)
                features = self.extract_features(transformed_img)
                
                # Determine action based on perception
                action = self.action_model(features)
                
                # Execute action
                self.publish_action(action)
                    
        except Exception as e:
            self.get_logger().error(f'Error processing image: {str(e)}')
    
    def extract_features(self, img_tensor):
        """Extract features using perception model"""
        # Get intermediate layer features (example)
        # This would depend on the specific model architecture
        with torch.no_grad():
            # Run through convolutional layers to extract features
            x = self.perception_model.conv1(img_tensor)
            x = self.perception_model.bn1(x)
            x = self.perception_model.relu(x)
            x = self.perception_model.maxpool(x)
            
            x = self.perception_model.layer1(x)
            x = self.perception_model.layer2(x)
            x = self.perception_model.layer3(x)
            x = self.perception_model.layer4(x)
            
            # Global average pooling
            x = torch.nn.functional.adaptive_avg_pool2d(x, (1, 1))
            features = torch.flatten(x, 1)
            
            return features
    
    def depth_callback(self, msg):
        """Process depth information"""
        # Process depth data for navigation
        pass
    
    def imu_callback(self, msg):
        """Process IMU data for balance"""
        # Process IMU for balance awareness
        pass
    
    def publish_action(self, action_tensor):
        """Execute action by publishing to robot"""
        cmd_msg = Twist()
        cmd_msg.linear.x = float(action_tensor[0, 0]) * 0.5  # Scale to reasonable velocity
        cmd_msg.angular.z = float(action_tensor[0, 1]) * 0.5  # Scale to reasonable angular velocity
        
        self.cmd_vel_pub.publish(cmd_msg)
```

## Adaptive Control Systems

Adaptive control systems can modify their behavior based on changing conditions or performance:

### Model Reference Adaptive Control (MRAC)

```python
class MRACController:
    """Model Reference Adaptive Controller for humanoid robots"""
    def __init__(self, reference_model_params, plant_params):
        self.reference_model = self.initialize_reference_model(reference_model_params)
        self.plant_params = plant_params
        
        # Adaptive parameters
        self.theta = np.zeros(plant_params.size)  # Controller parameters
        self.P = np.eye(plant_params.size) * 100  # Covariance matrix
        self.gamma = 1.0  # Adaptation gain
        
        # State tracking
        self.error = 0.0
        self.prev_error = 0.0
        self.integral_error = 0.0
    
    def initialize_reference_model(self, params):
        """Initialize reference model for desired behavior"""
        # This would create a reference dynamic system
        # For humanoid, this might represent ideal walking dynamics
        class ReferenceModel:
            def __init__(self, params):
                self.params = params
                self.state = 0.0  # Simplified state
            
            def update(self, input_signal):
                # Update reference model state
                # This implements the desired dynamics
                self.state = self.state * 0.9 + input_signal * 0.1  # Simplified
                return self.state
        
        return ReferenceModel(params)
    
    def update(self, measured_output, reference_input):
        """Update controller with new measurements"""
        # Get reference output
        reference_output = self.reference_model.update(reference_input)
        
        # Calculate tracking error
        self.error = reference_output - measured_output
        
        # Compute parameter adjustment
        phi = self.get_regression_vector(measured_output, reference_input)
        adjustment = self.gamma * np.outer(self.P, phi) * self.error
        self.theta += adjustment.flatten()
        
        # Update covariance matrix
        denom = 1 + np.dot(phi, np.dot(self.P, phi))
        self.P = self.P - (np.outer(np.dot(self.P, phi), np.dot(phi, self.P))) / denom
        
        # Compute control signal
        control_signal = np.dot(self.theta, phi)
        
        # Update for next iteration
        self.prev_error = self.error
        
        return control_signal
    
    def get_regression_vector(self, y, r):
        """Get regression vector for adaptation law"""
        # This would depend on the specific plant model
        # For humanoid, this might relate to joint kinematics/dynamics
        phi = np.array([y, r, y*r, y**2, r**2])  # Example features
        return phi[:self.theta.size]  # Trim to match parameter size
```

### Neural Adaptive Control

```python
import torch.nn as nn

class NeuralAdaptiveController(nn.Module):
    """Neural adaptive controller for complex robotic systems"""
    def __init__(self, state_dim, action_dim, hidden_dim=128):
        super(NeuralAdaptiveController, self).__init__()
        
        # Controller network
        self.controller_network = nn.Sequential(
            nn.Linear(state_dim + action_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, action_dim),
        )
        
        # Adaptation network (learns to adjust controller parameters)
        self.adaptation_network = nn.Sequential(
            nn.Linear(state_dim + action_dim + hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
        )
        
        # Error prediction network
        self.error_predictor = nn.Sequential(
            nn.Linear(state_dim + action_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, 1),
        )
        
        self.state_dim = state_dim
        self.action_dim = action_dim
        
    def forward(self, state, action, internal_state=None):
        """Forward pass with potential adaptation"""
        if internal_state is None:
            internal_state = torch.zeros(state.size(0), self.hidden_dim)
        
        # Controller output
        controller_output = self.controller_network(torch.cat([state, action], dim=1))
        
        # Adaptation
        adaptation_input = torch.cat([state, action, internal_state], dim=1)
        adaptation = self.adaptation_network(adaptation_input)
        
        # Combined output
        adapted_output = controller_output + 0.1 * adaptation  # Small adaptation influence
        
        # Predict error for learning
        error_prediction = self.error_predictor(torch.cat([state, action], dim=1))
        
        return adapted_output, adaptation, error_prediction

class AdaptiveControlSystem:
    """Complete adaptive control system for humanoid robots"""
    def __init__(self, state_dim, action_dim):
        self.neural_controller = NeuralAdaptiveController(state_dim, action_dim)
        self.optimizer = optim.Adam(self.neural_controller.parameters(), lr=1e-4)
        
        # Performance metrics
        self.performance_history = deque(maxlen=100)
        self.adaptation_activity = 0.0
        
    def compute_control(self, state, desired_action):
        """Compute control action with adaptation"""
        state_tensor = torch.FloatTensor(state).unsqueeze(0)
        action_tensor = torch.FloatTensor(desired_action).unsqueeze(0)
        
        with torch.no_grad():
            control_output, adaptation, error_pred = self.neural_controller(
                state_tensor, action_tensor)
        
        return control_output.numpy()[0]
    
    def update_adaptation(self, state, action, desired_output, actual_output):
        """Update neural adaptation based on performance"""
        state_tensor = torch.FloatTensor(state).unsqueeze(0)
        action_tensor = torch.FloatTensor(action).unsqueeze(0)
        desired_tensor = torch.FloatTensor(desired_output).unsqueeze(0)
        actual_tensor = torch.FloatTensor(actual_output).unsqueeze(0)
        
        # Compute loss
        tracking_error = desired_tensor - actual_tensor
        error_loss = torch.mean(tracking_error ** 2)
        
        # Also train on error prediction
        error_pred = self.neural_controller.error_predictor(
            torch.cat([state_tensor, actual_tensor], dim=1))
        prediction_loss = torch.nn.functional.mse_loss(error_pred, tracking_error)
        
        total_loss = error_loss + 0.1 * prediction_loss
        
        # Update
        self.optimizer.zero_grad()
        total_loss.backward()
        self.optimizer.step()
        
        # Track performance
        self.performance_history.append(error_loss.item())
        
        return error_loss.item()
```

## Hierarchical Decision Making

Complex humanoid tasks often require hierarchical decision-making systems:

### Task and Motion Planning (TAMP)

```python
class TaskAndMotionPlanner:
    """Hierarchical planner for complex humanoid tasks"""
    def __init__(self):
        self.task_planner = SymbolicTaskPlanner()
        self.motion_planner = BiPedalMotionPlanner()
        self.high_level_reasoner = HighLevelReasoner()
        
    def plan_task(self, goal_description):
        """Plan high-level task decomposition"""
        # Parse goal description
        goal = self.parse_goal(goal_description)
        
        # Decompose into subtasks
        task_plan = self.task_planner.decompose_task(goal)
        
        # For each task, generate motion plans
        complete_plan = []
        for task in task_plan:
            if task.type == "navigate":
                motion_plan = self.motion_planner.plan_navigate(task.destination)
            elif task.type == "manipulate":
                motion_plan = self.motion_planner.plan_manipulate(
                    task.object, task.destination)
            elif task.type == "communicate":
                motion_plan = self.motion_planner.plan_communicate(task.message)
            
            complete_plan.append({
                'task': task,
                'motion_plan': motion_plan
            })
        
        return complete_plan
    
    def parse_goal(self, goal_description):
        """Parse natural language goal into structured format"""
        # This would use NLP to parse goals
        # Example: "Go to kitchen and bring me a water bottle"
        # Would be parsed into navigate → find_object → grasp → navigate → place
        pass

class SymbolicTaskPlanner:
    """Symbolic task planner using STRIPS-like formalism"""
    def __init__(self):
        # Define operators (actions) and predicates (states)
        self.operators = self.define_operators()
        self.predicates = self.define_predicates()
    
    def define_operators(self):
        """Define available operators for task planning"""
        return {
            'navigate': {
                'preconditions': ['at(X)', 'accessible(Y)'],
                'effects': ['at(Y)', '!at(X)'],
                'cost': 1.0
            },
            'grasp': {
                'preconditions': ['at(X)', 'reachable(X)', 'free_hand()'],
                'effects': ['holding(X)', '!free_hand()'],
                'cost': 0.5
            },
            'place': {
                'preconditions': ['holding(X)', 'at(Y)'],
                'effects': ['!holding(X)', 'placed(X, Y)', 'free_hand()'],
                'cost': 0.5
            }
        }
    
    def decompose_task(self, goal):
        """Decompose goal into sequence of operators"""
        # Implement forward/backward chaining search
        # or use classical planning algorithms
        task_plan = []
        # Simplified implementation
        return task_plan

class BiPedalMotionPlanner:
    """Motion planner specialized for bipedal robots"""
    def __init__(self):
        self.footstep_planner = FootstepPlanner()
        self.balance_controller = BalanceController()
        self.manipulation_planner = ManipulationPlanner()
    
    def plan_navigate(self, destination):
        """Plan navigation motion for bipedal robot"""
        return self.footstep_planner.plan_path(destination)
    
    def plan_manipulate(self, obj, destination):
        """Plan manipulation motion for object"""
        return self.manipulation_planner.plan_grasp_transport_place(obj, destination)
    
    def plan_communicate(self, message):
        """Plan motion for communication (e.g., gestures)"""
        return [{'type': 'gesture', 'motion': 'wave', 'duration': 2.0}]
```

## Sim-to-Real Transfer Techniques

Transferring learned behaviors from simulation to real robots requires special consideration:

### Domain Randomization

```python
class DomainRandomization:
    """Domain randomization for sim-to-real transfer"""
    def __init__(self):
        self.randomization_params = {
            'visual': {
                'lighting': (0.5, 2.0),  # Intensity range
                'textures': ['concrete', 'wood', 'carpet', 'grass'],
                'colors': [(0.2, 0.2, 0.2), (0.8, 0.8, 0.8)],  # Dark to light
                'materials': ['matte', 'glossy', 'rough']
            },
            'dynamics': {
                'friction': (0.3, 1.0),
                'mass_variation': (0.8, 1.2),
                'inertia_scaling': (0.9, 1.1),
                'actuator_noise': (0.0, 0.05)
            },
            'sensor': {
                'camera_noise': (0.0, 0.02),
                'imu_drift': (0.0, 0.01),
                'delay_range': (0.01, 0.05)
            }
        }
    
    def randomize_domain(self, sim_env):
        """Randomize simulation domain parameters"""
        # Visual randomization
        lighting_mult = np.random.uniform(
            self.randomization_params['visual']['lighting'][0],
            self.randomization_params['visual']['lighting'][1]
        )
        sim_env.set_lighting_multiplier(lighting_mult)
        
        # Dynamics randomization
        friction = np.random.uniform(
            self.randomization_params['dynamics']['friction'][0],
            self.randomization_params['dynamics']['friction'][1]
        )
        sim_env.set_friction(friction)
        
        # Add sensor noise
        camera_noise = np.random.uniform(
            self.randomization_params['sensor']['camera_noise'][0],
            self.randomization_params['sensor']['camera_noise'][1]
        )
        sim_env.add_camera_noise(camera_noise)
        
        return sim_env
```

### Curriculum Learning

```python
class CurriculumLearning:
    """Curriculum learning for gradual skill acquisition"""
    def __init__(self):
        self.curriculum_levels = [
            {
                'name': 'stationary_balance',
                'difficulty': 0.1,
                'tasks': ['maintain_balance'],
                'rewards': {'balance_time': 1.0, 'fall_penalty': -10.0}
            },
            {
                'name': 'simple_stepping',
                'difficulty': 0.3,
                'tasks': ['step_forward', 'step_backward'],
                'rewards': {'balance_time': 1.0, 'reach_target': 5.0, 'fall_penalty': -10.0}
            },
            {
                'name': 'straight_line_walking',
                'difficulty': 0.5,
                'tasks': ['walk_forward', 'walk_backward'],
                'rewards': {'forward_vel': 1.0, 'energy_efficiency': 0.5, 'balance_time': 0.5, 'fall_penalty': -10.0}
            },
            {
                'name': 'turning',
                'difficulty': 0.7,
                'tasks': ['turn_left', 'turn_right'],
                'rewards': {'heading_accuracy': 2.0, 'balance_time': 0.5, 'energy_efficiency': 0.3, 'fall_penalty': -10.0}
            },
            {
                'name': 'complex_maneuvers',
                'difficulty': 1.0,
                'tasks': ['sidestep', 'walk_over_small_obstacles'],
                'rewards': {'task_completion': 10.0, 'smoothness': 1.0, 'balance_time': 0.5, 'fall_penalty': -10.0}
            }
        ]
        
        self.current_level = 0
        self.level_progress_threshold = 0.8  # 80% success rate to advance
    
    def get_current_tasks(self):
        """Get tasks for current curriculum level"""
        return self.curriculum_levels[self.current_level]['tasks']
    
    def evaluate_performance(self, episode_results):
        """Evaluate agent performance on current level"""
        # Calculate performance metrics based on episode results
        success_rate = self.calculate_success_rate(episode_results)
        
        if success_rate >= self.level_progress_threshold and self.current_level < len(self.curriculum_levels) - 1:
            self.current_level += 1
            print(f"Advancing to curriculum level: {self.curriculum_levels[self.current_level]['name']}")
        
        return success_rate
    
    def calculate_success_rate(self, results):
        """Calculate success rate from episode results"""
        if not results:
            return 0.0
        
        successful_episodes = sum(1 for r in results if r.success)
        return successful_episodes / len(results)
```

## Neural Architecture Search for Robotics

Neural Architecture Search (NAS) can optimize neural networks specifically for robotic tasks:

```python
class RobotNASCandidate:
    """Represents a candidate neural architecture for robotics"""
    def __init__(self, layers_config):
        self.layers_config = layers_config  # List of layer specifications
        self.fitness_score = 0.0
        self.computation_cost = 0.0  # FLOPs or inference time
        
    def build_network(self):
        """Build the neural network from configuration"""
        layers = []
        for layer_config in self.layers_config:
            layer_type = layer_config['type']
            if layer_type == 'conv':
                layers.append(nn.Conv2d(layer_config['in_channels'], 
                                      layer_config['out_channels'],
                                      layer_config['kernel_size']))
            elif layer_type == 'linear':
                layers.append(nn.Linear(layer_config['in_size'], 
                                      layer_config['out_size']))
            elif layer_type == 'residual':
                layers.append(ResidualBlock(layer_config['channels']))
            # Add activation functions
            layers.append(nn.ReLU())
        
        return nn.Sequential(*layers)

class RobotNeuralArchitectureSearch:
    """Neural Architecture Search specialized for robotics applications"""
    def __init__(self, search_space, population_size=50, generations=20):
        self.search_space = search_space
        self.population_size = population_size
        self.generations = generations
        self.population = []
        
        # Robot-specific objectives
        self.objectives = {
            'accuracy': 0.5,
            'latency': 0.3,
            'power_efficiency': 0.2
        }
    
    def initialize_population(self):
        """Initialize random population of architectures"""
        for _ in range(self.population_size):
            config = self.generate_random_architecture()
            candidate = RobotNASCandidate(config)
            self.population.append(candidate)
    
    def generate_random_architecture(self):
        """Generate a random architecture within search space"""
        layers = []
        
        # Generate random sequence of layers
        num_layers = np.random.randint(3, 8)  # 3-8 layers
        
        for _ in range(num_layers):
            layer_type = np.random.choice(['conv', 'linear', 'residual'])
            layer_config = self.sample_layer_configuration(layer_type)
            layers.append(layer_config)
        
        return layers
    
    def sample_layer_configuration(self, layer_type):
        """Sample configuration for a layer type"""
        if layer_type == 'conv':
            return {
                'type': 'conv',
                'in_channels': np.random.choice([16, 32, 64, 128]),
                'out_channels': np.random.choice([32, 64, 128, 256]),
                'kernel_size': np.random.choice([3, 5, 7])
            }
        elif layer_type == 'linear':
            return {
                'type': 'linear',
                'in_size': np.random.choice([64, 128, 256, 512]),
                'out_size': np.random.choice([32, 64, 128, 256])
            }
        elif layer_type == 'residual':
            return {
                'type': 'residual',
                'channels': np.random.choice([64, 128, 256])
            }
    
    def evaluate_candidate(self, candidate, eval_env):
        """Evaluate a candidate architecture"""
        try:
            network = candidate.build_network()
            
            # Test accuracy on evaluation environment
            accuracy = self.evaluate_accuracy(network, eval_env)
            
            # Estimate computational cost
            latency = self.estimate_latency(network)
            power = self.estimate_power_usage(network)
            
            # Combined fitness score
            fitness = (self.objectives['accuracy'] * accuracy - 
                      self.objectives['latency'] * latency - 
                      self.objectives['power_efficiency'] * power)
            
            candidate.fitness_score = fitness
            candidate.computation_cost = latency
            
            return fitness
            
        except Exception as e:
            # Penalize invalid architectures
            candidate.fitness_score = -1.0
            return -1.0
    
    def evolve_population(self):
        """Evolve population using genetic operators"""
        # Sort by fitness
        self.population.sort(key=lambda x: x.fitness_score, reverse=True)
        
        # Keep top performers
        survivors = self.population[:int(0.2 * self.population_size)]
        
        # Generate offspring through mutation and crossover
        offspring = []
        for _ in range(self.population_size - len(survivors)):
            parent1 = np.random.choice(survivors)
            if np.random.rand() < 0.8:  # Crossover 80% of the time
                parent2 = np.random.choice(survivors)
                child_config = self.crossover(parent1.layers_config, parent2.layers_config)
            else:  # Mutation otherwise
                child_config = self.mutate(parent1.layers_config)
            
            offspring.append(RobotNASCandidate(child_config))
        
        self.population = survivors + offspring
    
    def crossover(self, config1, config2):
        """Combine two architectures"""
        # Simplified crossover: take half from each
        mid_point = len(config1) // 2
        child_config = config1[:mid_point] + config2[mid_point:]
        return child_config
    
    def mutate(self, config):
        """Mutate an architecture"""
        mutated_config = config.copy()
        # Randomly modify ~20% of layers
        for i in range(len(mutated_config)):
            if np.random.rand() < 0.2:
                mutated_config[i] = self.sample_layer_configuration(
                    mutated_config[i]['type'])
        return mutated_config
    
    def search(self, eval_env):
        """Run the entire NAS process"""
        self.initialize_population()
        
        for generation in range(self.generations):
            print(f"Evaluating generation {generation + 1}/{self.generations}")
            
            for candidate in self.population:
                self.evaluate_candidate(candidate, eval_env)
            
            # Report best from generation
            best = max(self.population, key=lambda x: x.fitness_score)
            print(f"Best fitness: {best.fitness_score:.4f}")
            
            # Evolve for next generation
            self.evolve_population()
        
        # Return best architecture
        best_final = max(self.population, key=lambda x: x.fitness_score)
        return best_final
```

## Summary

This chapter covered advanced AI techniques for humanoid robot brains:

- Reinforcement learning for locomotion and control
- Isaac ROS integration for perception-action systems
- Adaptive control systems that adjust in real-time
- Hierarchical decision-making for complex tasks
- Sim-to-real transfer techniques including domain randomization
- Curriculum learning for gradual skill acquisition
- Neural architecture search for optimizing robot neural networks

These techniques enable humanoid robots to learn complex behaviors, adapt to changing conditions, and execute sophisticated tasks that require both perception and action capabilities.

## Exercises

1. Implement a simple DDPG agent for basic humanoid control
2. Create a domain randomization scheme for your simulation environment
3. Design a hierarchical task planner for a specific humanoid task

## Next Steps

With advanced AI techniques for humanoid robots covered, the next module will focus on Vision-Language-Action systems that integrate perception, cognition, and action in unified frameworks for natural human-robot interaction.